讨论 / 求解这道并查集的题我错在哪里?谢谢
ruilianglv 2010-08-04 06:18:00
点我顶贴 收藏 删除
没发现什么错,但是全WA答案错误。。

谢谢

#include "stdio.h"

int M,N,P;

int father[5001];

int findfather(int i)

{

if(father[i]!=i)

return findfather(father[i]);

else

return i;

}

int main()

{

int i,a,b,fa,fb;

scanf("%d %d %d",&N,&M,&P);

for(i=1;i<=N;i++)

father[i]=i;

for(i=1;i<=M;i++)

{

scanf("%d %d",&a,&b);

fa=findfather(a);

fb=findfather(b);

if(fa!=fb)

father[fa]=fb;

}

for(i=1;i<=P;i++)

{

scanf("%d %d",&a,&b);

fa=findfather(a);

fb=findfather(b);

if(fa!=fb)

printf("NO\n");

else

printf("YES\n");

}

return 0;

}

#1 ruilianglv@2010-08-02 20:20:00
回复 删除
我知道了,原来要求输出的是Yes/No,我写的是YES/NO,晕

测试结果1: 通过本测试点|有效耗时62ms

测试结果2: 通过本测试点|有效耗时47ms

测试结果3: 通过本测试点|有效耗时47ms

测试结果4: 通过本测试点|有效耗时47ms

测试结果5: 通过本测试点|有效耗时47ms

测试结果6: 通过本测试点|有效耗时62ms

测试结果7: 通过本测试点|有效耗时63ms

测试结果8: 通过本测试点|有效耗时47ms

测试结果9: 通过本测试点|有效耗时78ms

测试结果10: 通过本测试点|有效耗时62ms

#2 sideman@2010-08-02 21:00:00
回复 删除
ORZ
#3 CK、@2010-08-03 00:24:00
回复 删除
看不懂C- -
#4 892611452@2010-08-04 06:17:00
回复 删除
直接给我的AC程序吧

#include<iostream>

using namespace std;

int father[5001],a,b,n,m,p;

int FindR(int x)

{ if(father[x])return father[x]=FindR(father[x]);

return x;}

void Merge(int x,int y)

{ int a=FindR(x);

int b=FindR(y);

if(a!=b)

father[a]=b;}

int main(void)

{ cin>>n>>m>>p;

for(int i=1;i<=m;i++)

{ cin>>a>>b;

Merge(a,b);

}

for(int i=1;i<=p;i++)

{ cin>>a>>b;

if(FindR(a)==FindR(b))cout<<"Yes"<<endl;

else cout<<"No"<<endl;}

return 0;}

#5 892611452@2010-08-04 06:18:00
回复 删除
用了路径压缩

用了路径压缩

查看更多回复
提交回复