Codeforces Round #503 (by SIS, Div. 2) 1020B. Badge Solution
Solution in C++:
/// La ilaha illellahu muhammadur rasulullah
///******Bismillahir-Rahmanir-Rahim******///
/// Abul Hasnat Tonmoy
/// Department of CSE,23rd batch
/// Islamic University,Bangladesh
///**********ALLAH IS ALMIGHTY************///
#include <bits/stdc++.h>
using namespace std;
const int mx = 1002;
bool vis[mx];
vector<int> g[mx];
int dfs(int u)
{
if (vis[u]) return u;
vis[u] = 1;
for (int i = 0; i < g[u].size(); i++)
{
int next = g[u][i];
return dfs(next);
}
}
int main()
{
int n, i, x;
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> x;
g[i].push_back(x);
}
for (i = 1; i <= n; i++)
{
memset(vis, 0, sizeof(vis));
cout << dfs(i) << " ";
}
cout << endl;
}
No comments