【CF1020B】Badge(模拟)

题意:给定n个人,每个人指向第a[i]个人,要求输出从每个人开始第一个被访问到两次的人的编号

n<=1e3

思路:

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<string>
 4 #include<cmath>
 5 #include<iostream>
 6 #include<algorithm>
 7 #include<map>
 8 #include<set>
 9 #include<queue>
10 #include<vector>
11 using namespace std;
12 typedef long long ll;
13 typedef unsigned int uint;
14 typedef unsigned long long ull;
15 typedef pair<int,int> PII;
16 typedef vector<int> VI;
17 #define fi first
18 #define se second 
19 #define MP make_pair
20 #define N   1100000
21 #define MOD 1000000007
22 #define eps 1e-8 
23 #define pi acos(-1)
24 
25 int a[N],b[N],n;
26 
27 int read()
28 { 
29    int v=0,f=1;
30    char c=getchar();
31    while(c<48||57<c) {if(c=='-') f=-1; c=getchar();}
32    while(48<=c&&c<=57) v=(v<<3)+v+v+c-48,c=getchar();
33    return v*f;
34 }
35     
36 void swap(int &x,int &y)
37 {
38     int t=x;x=y;y=t;
39 }
40 
41 int main()
42 {
43     //freopen("1.in","r",stdin);
44     //freopen("1.out","w",stdout);
45     int n;
46     scanf("%d",&n);
47     for(int i=1;i<=n;i++) scanf("%d",&a[i]);
48     for(int i=1;i<=n;i++)
49     {
50         int now=i;
51         for(int j=1;j<=n;j++) b[j]=0;
52         b[now]=1;
53         int flag=0;
54         while(!flag)
55         {
56             now=a[now];
57             b[now]++;
58             if(b[now]==2) flag=1;
59         }
60         printf("%d ",now);
61     }
62 
63     return 0;     
64 }

 

posted on 2018-10-24 14:05  myx12345  阅读(139)  评论(0编辑  收藏  举报

导航