func_code_string: #include <bits/stdc++.h>   #define ll long long   #define ld long double   #define vec vector   #define pii pair<int, int>    using namespace std;      const long long MAXVAL = (long long) 1e18 + 1;   const long long MOD = 1000000007ll;   //const long long MOD = 998244353ll;   const int INF = 1000000001;      long long poww(int x, long long pow) {     if (pow == 0ll ) return 1;     if (pow == 1ll) return x % MOD;     long long ans = poww(x, pow >> 1) % MOD;     ans = (ans * ans) % MOD;     if ((pow & 1) == 1) {       ans = (ans * x ) % MOD;     }     return ans % MOD;   }      ll gcd(ll x, ll y) {     if (x == 0) return y;     while (y) {       x %= y;       swap(x, y);     }     return x;   }      ll sq(int x, int y){      return x * 1ll * x + y  * 1ll * y;   }      //vector<int> pi(1000);   //vector<int> w(1000);      //int dsu_get(int x) {     //if (pi[x] == x) return x;     //int res = dsu_get(pi[x]);     //pi[x] = res;     //return res;   //}   //void dsu_merge(int x, int y) {     //if (x == y) return;     //x = dsu_get(x);     //y = dsu_get(y);     //if (x == y) return;     //if (w[x] > w[y]) swap(x, y);     //pi[y] = x;     //w[y] += w[x];   //}      //struct intr {     //int x, a, lb, rb;     //intr(int x_, int a_, int lb_, int rb_) {       //x = x_;       //a = a_;       //lb = lb_;       //rb = rb_;     //}   //};   //   //vec<int> t;      //void init (int nn) {     //n = nn;     //t.assign (n, 0);   //}      //int sum (int r) {     //int result = 0;     //for (; r >= 0; r = (r & (r+1)) - 1) result += t[r];     //return result;   //}      //void inc (int i, int delta) {     //for (; i < n; i = (i | (i+1))) t[i] += delta;   //}   //      int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int q = 1;     //cin >> q;     while (q-- > 0) {        int n,k;       cin >> n >> k;       vec<set<int>> gr(n);       vec<set<int>> gr2(n);       for (int i = 0; i < n; ++i) {         int x;         cin >> x;         if (x == 0) continue;         --x;         gr[x].insert(i);       }       vec<pii> req;       vec<int> pi(n);       for (int i = 0; i < n; ++i) pi[i] = i;       bool fail = false;       vec<int> was(n, 0);       gr2 = gr;       vec<set<int>> gr2t(n);       for (int i  =0; i < n; ++i) {         for (int j : gr2[i]) {           gr2t[j].insert(i);         }       }       //for (int i = 0; i < n; ++i)labels[i] = {i};       vec<int>was2(n, 0);       for (int j = 0; j < k; ++j) {         int x, y;         cin >> x >> y;         --x;         --y;         req.emplace_back(x, y);         //cout <<   req   << j << endl;         if (gr2[y].count(x)) {           //cout <<   y -> x   << y +1 <<     << x+1 << endl;           fail = true;         } else {           //if (gr[x].count(y)) {             //gr[x].erase(gr[x].find(y));           //}           gr2[x].insert(y);           if (pi[y] != y) {             // cannot be             //cout <<   cannot bE   << fail << endl;             fail = true;           }           int top = x;           vec<int> path;           while (top != pi[top]) {             path.push_back(top);             top = pi[top];           }           for (auto& s : path) {             pi[s] = top;           }           pi[y] = pi[x];           was[y] = 2;         }       }       if (fail) {         cout << 0 <<   n ;         continue;       }          function<void(int)>  dfs = [&] (int v) {         //cout <<   cur v   << v + 1 << endl;         was2[v] = 1;         for (int next : gr2[v]) {           if (was2[next] == 0) {             dfs(next);           } else if (was2[next] == 1) {             //cout <<   next gives cycle   << next + 1 << endl;             fail = true;             break;           }         }         was2[v] = 2;         //cout <<   OUT   << v + 1 << endl;       };       for (int i = 0; i < n; ++i) if (was2[i] == 0) dfs(i);       if (fail) {         cout <<  0 n ;         continue;       }       vec<set<int>> grnew(n);       for (int i = 0; i < n; ++i) {           vec<int> path;           int top = i;           while (top != pi[top]) {             path.push_back(top);             top = pi[top];           }           for (int s : path) {             pi[s] = top;           }         for (int out : gr[i]) {           if (pi[out] != pi[i]) {             grnew[pi[i]].insert(pi[out]);           }         }         for (int in : gr2t[i]) {           if (pi[in] != pi[i]) {             grnew[pi[in]].insert(pi[i]);           }         }       }       //for (auto r: req) {         //gr2[r.first].insert(r.second);       //}       vec<int> topo;       bool cycle = false;       vec<int> next;       vec<int> prev;       for (int i =0 ;i < n; ++i) {         prev.push_back(i);         next.push_back(i);       }       for (int i = 0; i < k; ++i) {         int x = req[i].first;         int y = req[i].second;         next[x] = y;         prev[y] = x;       }          function<void(int)>  topo_dfs = [&] (int v) {         //cout <<   cur v   << v + 1 << endl;         was[v] = 1;         for (int next : grnew[v]) {           if (was[next] == 0) {             topo_dfs(next);           } else if (was[next] == 1) {             //cout <<   next gives cycle   << next + 1 << endl;             cycle = true;           }         }         was[v] = 2;         //cout <<   OUT of   << v + 1  << endl;         topo.push_back(v);       };       for (int i = 0; i < n; ++i) {         if (was[i]) continue;         topo_dfs(i);       }       if (cycle) {         cout << 0 <<   n ;       } else {         reverse(topo.begin(), topo.end());         vec<int> ans;         vec<bool> was(n, false);         for (int i = 0 ; i < topo.size(); ++i) {           int x = topo[i];           //cout <<   Cur x   << x + 1 << endl;           if (prev[x] != x) continue;           while (x != next[x]) {             if (was[x]) {               //cout <<  was in   << x + 1 << endl;               fail = true;               break;             }             was[x] = true;             //cout <<   next x   << x + 1 << endl;             ans.push_back(x);             x = next[x];           }           ans.push_back(x);         }         if (fail || ans.size() < n) {           cout << 0 <<   n ;         } else {           for (auto l : ans) {             cout << 1 + l <<    ;           }           cout <<   n ;         }       }           }     return 0;   }   