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

请哪位大牛帮我看下程序错哪了

Posted by 030501619 at 2007-06-12 10:54:33 on Problem 1988
#include<iostream>
using namespace std;
class UnionFind{
public:
	UnionFind(int n);
	~UnionFind(){delete [] parent;delete [] root;}
	int Find(int e);
	void Union(int i,int j);
    bool *root;
	int *parent;
	int *up;
	int  *sum;
};
UnionFind::UnionFind(int n)
{
	root=new bool[n+1];
	parent=new int[n+1];
	up=new int [n+1];
	sum=new int [n+1];
	for(int e=1;e<=n;e++){
		parent[e]=1;
		root[e]=true;
	    up[e]=0;
	    sum[e]=1;}
}
int UnionFind::Find(int e)
{
	up[e]=0;
	int j=e;
	while(!root[j]) j=parent[j];
	int f=e;
	while(f!=j){
		int pf=parent[f];
		parent[f]=j;
		f=pf;
		up[e]++;
		//sum[j]++;
	}
	return j;
}
void UnionFind::Union(int i,int j)
{
	if(parent[i]<parent[j]){
		up[i]=sum[j];
		parent[j]+=parent[i];
		sum[j]+=sum[i];
		root[i]=false;
		parent[i]=j;}
	else {
		up[j]=sum[i];
		parent[i]+=parent[j];
		sum[i]+=sum[j];
		root[j]=false;
		parent[j]=i;}
}
int main()
{
	int n,i;
	cin>>n;
	UnionFind u(n);
	char c;
	int a,b;
	for(i=0;i<n;i++)
	{
		cin>>c;
		switch(c)
		{
		case'M':
		cin>>a>>b;
	    u.Union(u.Find(a),u.Find(b));
		break;
		case'C':
			cin>>a;
			u.Find(a);
			cout<<u.parent[u.Find(a)]-u.up[a]-1<<endl;
			break;
		}
	}
	return 0;
}
如题,谢了,实在找不出错了

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