| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
最后忘记写了个自增,WA了四次。想死啊#include <cstdio>
using namespace std;
int people[50005];
/*查找根节点*/
int findRoot(int node){
//自身不是自己的父节点的时候
while(people[node] != node){
node = people[node];
}
return node;
}
int main(){
int n, m, i,count, var1, var2, root1, root2, count1 = 1;
while(scanf("%d %d", &n, &m) && (n!=0 || m!=0)){
//最开始假设是每个人都有自己的信仰
count = n;
//最开始每个人的父节点都是自己
for(i = 0; i < n; i++){
people[i] = i;
}
while(m--){
scanf("%d %d", &var1, &var2);
root1 = findRoot(var1);
root2 = findRoot(var2);
//两个人的父节点不同,那么就减少一种信仰。
if(root1 != root2) {
people[var1] = people[root1] = root2;
count--;
}
}
printf("Case %d: %d\n",count1++, count);
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator