| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
水过100,感谢楼主的算法,在这里帮楼主贴下具体的实现代码。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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator