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

why wa?

Posted by trymore at 2007-05-06 21:40:21 on Problem 1674
#include<iostream.h>
int a[10001],b[10001];
int main()
{
	int t,ca,n,i,j,k,s;
	cin>>t;
	for(ca=0;ca<t;ca++)
	{
		cin>>n;
		for(i=0;i<=n;i++)a[i]=i;
		for(i=0;i<=n;i++)b[i]=0;
		for(i=1;i<=n;i++)
		{
			cin>>s;
			j=i;k=a[i];
			while(j!=k){j=k;k=a[k];}a[s]=k;
			j=i;k=a[i];while(j!=k){a[j]=a[s];j=k;k=a[k];}
		}
		for(i=1;i<=n;i++)
		{
			b[a[i]]++;
		}
		j=0;
		for(i=1;i<=n;i++)if(a[i]==i) j+=b[i]-1;
		cout<<j<<endl;
	}
	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