http://acm.nyist.net/JudgeOnline/problem.php?pid=431

这一题真是纠结啊,,,一开始用c++超时,,,,,最后全改成c才行,,赤裸裸的并查集应用。。。

代码:

#include #include #include #include using namespace std; int father[10005],num[10005],d[10005]; int n,m; int Find(int x) { if(father[x]==x) return x; int f=father[x]; father[x]=Find(father[x]); d[x]+=d[f]; return father[x]; } void Union(int x,int y) { int fx=Find(x); int fy=Find(y); if(fx==fy) return ; father[fx]=fy; num[fy]+=num[fx]; d[fx]++; } int main() { int T; char c[5]; scanf("%d",&T); for(int t=1;t<=T;t++) { for(int i=0;i<10005;i++) { father[i]=i;num[i]=1;d[i]=0; } scanf("%d%d",&n,&m); printf("Case %d:\n",t); for(int i=0;i

你可能感兴趣的:(http://acm.nyist.net/JudgeOnline/problem.php?pid=431)