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

把我的search和union帖出来算了。。看看是不是有问题?

Posted by pfz1stp at 2007-06-02 16:27:44 on Problem 2236
In Reply To:那就不是DS的问题了,肯定你的算法不好了。。。 Posted by:richardxx at 2007-06-02 16:16:39
	int search(int x)
	{
		int f=x;
		while (par[f]!=-1)
		{
			f=par[f];
		}
		int ff=x;
		while (ff!=f)
		{
			int tf=ff;
			ff=par[ff];
			par[tf]=f;
		}
		return f;
	}

	bool sunion(int x, int y)
	{
		int r1=search(x);
		int r2=search(y);
		if (r1!=r2)
		{
			if (rank[r1]>rank[r2])
			{
				par[r2]=r1;
				rank[r1]++;
			}
			else
			{
				par[r1]=r2;
				rank[r2]++;
			}
			return 1;
		}
		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