Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

我要疯了,怎么一直WA啊!!!!!!!

Posted by ccyjava at 2010-07-16 00:39:17 on Problem 2524
感觉没有错啊,那几个数据也都正确

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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator