银河英雄传说[NOI 2002]一题,老是错7个点啊,求各位大神看下程序,指点迷津啊!或共享数据,感激不尽! 100
#include<cstdio>#include<cstdlib>intt,i,j,x,y,fa[30005],dep[30005],num[30005];charc;i...
#include <cstdio>
#include <cstdlib>
int t,i,j,x,y,fa[30005],dep[30005],num[30005];char c;
int gf(int x)
{
if (fa[x]==x) return x;
fa[x]=gf(fa[x]);
return fa[x];
}
void updata(int x)
{
if (fa[x]==x) return;
updata(fa[x]);
dep[x]+=dep[fa[x]];
}
int main(){
scanf("%d",&t);
for (i=1;i<=30000;i++){fa[i]=i;num[i]=1;}
while (t--){
scanf("%c",&c);while (c!='C'&&c!='M') scanf("%c",&c);
if (c=='M'){
scanf("%d%d",&x,&y);
updata(x);updata(y);
dep[gf(x)]=num[gf(y)];
num[fa[y]]+=num[fa[x]];
fa[fa[x]]=fa[y];
} else{
scanf("%d%d",&x,&y);
updata(x);updata(y);
if (gf(x)!=gf(y)) printf("-1\n");else
printf("%d\n",abs(dep[x]-dep[y])-1);
}
}
return 0;
}
fa[]是父亲节点,dep[]是父节点到自己之间的战舰个数+1,num[x]是所在列的战舰个数(x为队首时才有效)
.................... 本人此题已过 展开
#include <cstdlib>
int t,i,j,x,y,fa[30005],dep[30005],num[30005];char c;
int gf(int x)
{
if (fa[x]==x) return x;
fa[x]=gf(fa[x]);
return fa[x];
}
void updata(int x)
{
if (fa[x]==x) return;
updata(fa[x]);
dep[x]+=dep[fa[x]];
}
int main(){
scanf("%d",&t);
for (i=1;i<=30000;i++){fa[i]=i;num[i]=1;}
while (t--){
scanf("%c",&c);while (c!='C'&&c!='M') scanf("%c",&c);
if (c=='M'){
scanf("%d%d",&x,&y);
updata(x);updata(y);
dep[gf(x)]=num[gf(y)];
num[fa[y]]+=num[fa[x]];
fa[fa[x]]=fa[y];
} else{
scanf("%d%d",&x,&y);
updata(x);updata(y);
if (gf(x)!=gf(y)) printf("-1\n");else
printf("%d\n",abs(dep[x]-dep[y])-1);
}
}
return 0;
}
fa[]是父亲节点,dep[]是父节点到自己之间的战舰个数+1,num[x]是所在列的战舰个数(x为队首时才有效)
.................... 本人此题已过 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询