http://acm.hdu.edu.cn/showproblem.php?pid=3314
如果形成环,则标记一下,最后统计一遍每个人得多少票,看是否唯一即可
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <iostream>
using namespace std;
const double pi=acos(-1.0);
double eps=0.000001;
int fa[123456];
int find(int x)
{
if (fa[x]==x)
return x;
else return fa[x]=find(fa[x]);
}
int num[123456];
int main()
{
int n,m;
while(scanf("%d",&n)!=EOF)
{
set<int > del;
del.clear();
for (int i=0; i<n; i++) fa[i]=i ,num[i]=0;
for (int i=0; i<n; i++)
{
int x;
scanf("%d",&x);
int fx=find(x);
int fy=find(i);
if (x==i) continue;
if (fx!=fy)
fa[fy]=x;
else
del.insert(fx);
}
int maxx=0;
int maxi=-1;
for (int i=0; i<n; i++)
{
int fx=find(i);
if (del.find(fx)!=del.end()) //找到
continue;
else
num[fx]++;
}
for (int i=0; i<n; i++)
{
if (num[i]>maxx) maxx=num[i],maxi=i;
}
int cun=0;
for (int i=0; i<n; i++)
if (num[i]==maxx) cun++;
if (cun==1)
printf("%d\n",maxi);
else
printf("Trouble\n");
}
return 0;
}