-
Notifications
You must be signed in to change notification settings - Fork 0
/
2623.cpp
54 lines (45 loc) · 937 Bytes
/
2623.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
#include<iostream>
#include<vector>
#include<queue>
#define N 1005
using namespace std;
int n, m, pre[N], check[N];
vector<int> ans, seq[N];
queue<int> q;
int main()
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
int i, j, l, a, b;
cin >> n >> m;
for(i=1; i<=m; i++){
cin >> l >> a;
for(j=2; j<=l; j++){
cin >> b;
seq[a].push_back(b);
pre[b]++;
a=b;
}
}
for(i=1; i<=n; i++){
if(pre[i]==0) q.push(i);
}
while(!q.empty())
{
int head = q.front();
check[head]=1;
q.pop();
ans.push_back(head);
for(auto i: seq[head]){
if(check[i]) continue;
pre[i]--;
if(pre[i]==0) q.push(i);
}
}
if(ans.size()<n){
cout << 0;
return 0;
}
for(auto i: ans) cout << i << endl;
return 0;
}