BeingErnest @ 2018-10-29 14:06:15
基本的思想可以理解,但写的跟题解的相似度有99%,还错了,实在无能为力啊
#include<cstdio>
#include<algorithm>
using namespace std;
int father[10001];
int xz(int x)
{
if(father[x]==x) return x;
return father[x]=xz(father[x]);
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
father[i]=i;
}
for(int i=1;i<=m;i++)
{
int x,y,z;
scanf("%d%d%d",&z,&x,&y);
if(z==1)
{
father[x]=xz(y);
}
else
{
int a;
a=xz(x);
int b;
b=xz(y);
if(a==b)
printf("Y\n");
else
printf("N\n");
}
}
return 0;
}
by info___tion @ 2018-10-29 14:11:04
father[x]=xz(y);
你这句话不是应该这么写吗:
father[xz(x)]=xz(y);
by 一叶知秋。 @ 2018-10-29 14:11:50
father[x]=xz(y);
是
father[xz(x)]=xz(y)
by 一叶知秋。 @ 2018-10-29 14:12:03
@铭玘
by Ghoster @ 2018-10-29 14:12:59
if(z==1)
{
father[xz(x)]=xz(y);
}
把这改一下就A了
by 只用函数 @ 2018-10-29 14:13:00
@铭玘 没错没错就是这里
by jeffqi @ 2018-10-29 14:14:18
@铭玘
if(z==1)
{
father[x]=xz(y);
}
改成
if(z==1)
{
father[xz(x)]=xz(y);
}
就能过
by foreverlasting @ 2018-10-29 14:15:40
建议写成非递归版 :``` while(x!=fa[x])x=fa[x]=fa[fa[x]]; return x;
by BeingErnest @ 2018-10-29 14:17:14
@加藤圣教_封仙 是因为容易递归层次溢出?
by foreverlasting @ 2018-10-29 14:17:58
@铭玘 而且非递归版跑得快,又不会爆栈
by BeingErnest @ 2018-10-29 14:17:59
感谢各位,我忽略了只能爸爸认爸爸,