题目传送门:http://codeforces.com/contest/1020/problem/B
【Badge|Badge - Codeforces Round #503 (by SIS, Div. 2)-B】题目意思比较简单,就是一步一步走,注意这里要用递归(可以省去判断的麻烦),否则会超时
AC code:
#include
#include
#include
#include
#include
#define INF1e5
using namespace std;
const int maxn = 1000 + 100;
int n = 0;
struct Node{
int biao;
int num;
};
Node data[maxn];
void dfs(int p){
++data[p].num;
if(data[p].num >= 2){
printf("%d ",p);
return ;
}
dfs(data[p].biao);
}
int main(){
scanf("%d",&n);
for(int i = 1;
i <= n;
++i){
//cin >> data[i].biao;
scanf("%d",&data[i].biao);
}
for(int i = 1;
i <= n;
++i){
dfs(i);
for(int j = 1;
j <= n;
++j){
data[j].num = 0;
}
}return 0;
}