include iostream include iomanip include cstdio include cstring includ

  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
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <algorithm>
#include <queue>
#include <ctime>
#include <set>
#include <unordered_map>
#include <deque>
using namespace std;
typedef long long ll;
typedef long double ld;
#define forn(i, n) for (int i = 0; i < int(n); ++i)
#define PATH "D:\\QtProjects\\olymp\\"
template <class T> void mx(T& a, const T& b) { if (a < b) a = b; }
template <class T> void mn(T& a, const T& b) { if (a > b) a = b; }
template <class T> int sz(const T& a) { return (int)a.size(); }
const int N = 2010;
vector<int> g[N], gr[N];
bool used[N];
inline int dfs(int v){
used[v] = true;
int ans = 1;
for (int u : g[v]) if (!used[u])
ans += dfs(u);
return ans;
}
vector<int> ord;
inline void dfs1(int v){
used[v] = true;
for (int u : g[v]) if (!used[u])
dfs1(u);
ord.push_back(v);
}
int color[N];
inline void dfs2(int v, int col){
used[v] = true;
for (int u : gr[v]) if (!used[u])
dfs2(u, col);
color[v] = col;
}
int main(){
ios_base::sync_with_stdio(false);
int n;
cin >> n;
fill(color, color + n, -2);
forn(i, n){
int t;
cin >> t;
while (t != 0){
--t;
g[i].push_back(t);
gr[t].push_back(i);
cin >> t;
}
}
fill(used, used + n, false);
forn(i, n) if (!used[i])
dfs1(i);
reverse(ord.begin(), ord.end());
fill(used, used + n, false);
for (int x : ord) if (!used[x])
dfs2(x, x);
forn(i, n) if (color[i] < 0) {
cout << 0 << endl;
return 0;
}
vector<int> cnt(n, 0);
for (int i = 0; i < n; ++i){
for (int x : g[i]) if (color[i] != color[x])
++cnt[color[x]];
}
int id = -1;
forn(i, n) if (cnt[i] == 0 && i == color[i]) id = i;
fill(used, used + n, false);
set<int> ans;
if (id != -1 && dfs(id) == n)
forn(i, n)
if (color[i] == color[id])
ans.insert(i);
for (int x : ans) cout << x + 1 << ' ';
cout << 0 << endl;
return 0;
}