版权声明:原创,未经作者允许禁止转载 https://blog.csdn.net/Mr_wuyongcong/article/details/83541563
前言
依旧想去德育基地…
成绩
正题
林下风气 【树形 】
博客链接:
https://blog.csdn.net/Mr_wuyongcong/article/details/83541098
轻功 【 分层图】
博客链接:
https://blog.csdn.net/Mr_wuyongcong/article/details/83541258
盟主的忧虑 【并查集】
博客链接:
https://blog.csdn.net/Mr_wuyongcong/article/details/83541476
T2 20分code
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 4000
#define XJQ 19260817
using namespace std;
struct node{
int to,next;
}a[N];
int ls[N],w[N],maxs,mins,ans,n,k,tot,x,y,in[N];
void addl(int x,int y)
{
a[++tot].to=y;
a[tot].next=ls[x];
ls[x]=tot;
}
void dp1()
{
for(int i=1;i<=n;i++)
{
maxs=0;
mins=2147483647;
for(int j=i;j<=n;j++)
{
maxs=max(maxs,w[j]);
mins=min(mins,w[j]);
if(maxs-mins==k) ans++;
}
}
printf("%d",ans);
}
int main()
{
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++)
scanf("%d",&w[i]);
for(int i=1;i<n;i++)
{
scanf("%d%d",&x,&y);
addl(x,y);addl(y,x);
in[x]++;in[y]++;
maxs=max(maxs,max(in[x],in[y]));
}
if(maxs<=2)
dp1();
else printf("0");
}
T3 70分code
#include<cstdio>
#include<algorithm>
#define N 100010
using namespace std;
struct node{
int to,w,next,from;
}a[N];
int ls[N],tot,x,y,w,ans,dep[N],n,m,S,dfn[N],end[N],cnt;
void addl(int x,int y,int w)
{
a[++tot].to=y;a[tot].w=w;
a[tot].next=ls[x];ls[x]=tot;
a[tot].from=x;
}
void dfs(int x,int fa)
{
dfn[x]=++cnt;
dep[x]=dep[fa]+1;
for(int i=ls[x];i;i=a[i].next)
if(i<S)
{
int y=a[i].to;
if(y==fa) continue;
dfs(y,x);
}
end[x]=cnt;
}
bool cmp(node x,node y)
{return x.from<y.from;}
int next(int x)
{
while(dep[a[x].from]>dep[a[x].to]) x++;
return x;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<n;i++)
{
scanf("%d%d",&x,&y);
addl(x,y,0);
addl(y,x,0);
}
S=tot+1;
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&x,&y,&w);
addl(x,y,w);
}
dfs(1,0);
for(int i=1;i<S;i+=2)
{
ans=2147483647;
int x=a[i].to;
if(dep[a[i].to]<dep[a[i+1].to]) x=a[i+1].to;
for(int j=S;j<=tot;j++)
if(dfn[a[j].to]>=dfn[x]&&dfn[a[j].to]<=end[x]&&
(dfn[a[j].from]<dfn[x]||dfn[a[j].from]>end[x])
||
dfn[a[j].from]>=dfn[x]&&dfn[a[j].from]<=end[x]&&
(dfn[a[j].to]<dfn[x]||dfn[a[j].to]>end[x]))
ans=min(ans,a[j].w);
if(ans==2147483647) printf("-1\n");
else printf("%d\n",ans);
}
}
尾声
话说题目又ABC。老师改题面了