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啊!!!!!!!感觉没有错啊,那几个数据也都正确 4 3 1 2 3 4 2 3 输出1 没错啊啊~~~~ #include <iostream> #include "stdio.h" using namespace std; int p[50001]; void init(int n) { int i; for (i = 0; i < n; i++) { p[i] = -1; } } int findit(int x) { int pr = x; int t; while (p[pr]>0) pr = p[pr]; while (pr != x) { t = p[x]; p[x] = pr; x = t; } return x; } void unionit(int a, int b) { int x, y; x = findit(a); y = findit(b); if (x == y) return; if (p[y] < p[x]) { p[y] += p[x]; p[x] = y; } else { p[x] += p[y]; p[y] = x; } } int main() { int m,n; int i,k=0; int a,b; int sum; while(scanf("%d%d",&n,&m),n||m){ init(n); sum=n; k++; for(i=0;i<m;i++){ scanf("%d%d",&a,&b); if(findit(a-1)!=findit(b-1)){ sum--; unionit(a-1,b-1); } } printf("Case %d: %d\n",k,sum); } } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator