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

水过100,感谢楼主的算法,在这里帮楼主贴下具体的实现代码。

Posted by lithum at 2009-08-04 15:29:07 on Problem 1674
In Reply To:算法!查了老长时间的资料才查出来的!不容易呀! Posted by:OIer_nk2008 at 2007-07-08 11:33:34
#include<iostream>
using namespace std;

int num[10010];
bool visited[10010];

int main()
{
	int i;
	int n,cycle;
	int temp;
	int cases;
	cin>>cases;
	while(cases--)
	{
		memset(num,0,sizeof(num));
		cin>>n;
		for(i=1;i<=n;i++)
		{
			scanf("%d",&temp);
			num[temp]=i;
		}
		memset(visited,0,sizeof(visited));
		cycle=0;
		for(i=1;i<=n;i++)
		{
			if(!visited[i])
			{
				temp=i;
				do{
					visited[temp]=true;
					temp=num[temp];
				}while(!visited[temp]);
				cycle++;
			}//end if
		}//end for
		cout<<n-cycle<<endl;
	}//end while
	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