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

Most View Post

Recent post

Codeforces Round 971 (Div. 4) 2009C. The Legend of Freya the Frog Solution

  Problem Link    https://codeforces.com/contest/2009/problem/C S olution in C++: /// Author : AH_Tonmoy #include < bits / stdc ++. h ...

Powered by Blogger.