-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy path1020B. Badge.cpp
67 lines (53 loc) · 1.02 KB
/
1020B. Badge.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <bits/stdc++.h>
using namespace std;
#define sf scanf
#define pf printf
#define pb push_back
const int MAX = 1005;
vector <int> edges[MAX];
bool visited[MAX];
int n;
int BFS (int s)
{
int i,j,u,v;
queue <int> Q;
memset (visited,0,sizeof visited);
Q.push(s);
visited[s] = 1;
while (!Q.empty())
{
u = Q.front();
Q.pop();
for (i=0; i<edges[u].size(); i++)
{
v = edges[u][i];
if (!visited[v])
{
visited[v] = 1;
Q.push(v);
}
}
}
return v;
}
int main ()
{
int i,p;
while (sf ("%d",&n) != EOF)
{
for (i=1; i<=n; i++)
{
sf ("%d",&p);
edges[i].pb(p);
}
for (i=1; i<=n; i++)
{
pf ("%d",BFS(i));
if (i == n)
pf ("\n");
else
pf (" ");
}
}
return 0;
}