-
Notifications
You must be signed in to change notification settings - Fork 28
/
maketree.cpp
75 lines (63 loc) · 1.42 KB
/
maketree.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define inf 1000000000
#define MAXN 100001
using namespace std;
queue<int> susie;
vector<int> nodes[MAXN];
bool visited[MAXN];
void topological_sort(int cur) {
for (vector<int>::iterator it = nodes[cur].begin(); it != nodes[cur].end(); ++it) {
if (!visited[*it]) {
visited[*it] = true;
topological_sort(*it);
susie.push(*it);
}
}
}
int main() {
int n, k;
scanf("%d%d", &n, &k);
int w;
for (int i = 1; i <= k; ++i) {
scanf("%d", &w);
int node;
for (int j = 0; j < w; ++j) {
scanf("%d", &node);
nodes[node].push_back(i);
}
}
memset(visited, 0, sizeof visited);
for (int i = 1; i <= n; ++i) {
if (!visited[i]) {
visited[i] = true;
topological_sort(i);
susie.push(i);
}
}
int prev = 0;
int parent[MAXN];
while (!susie.empty()) {
int node = susie.front(); susie.pop();
parent[node] = prev;
prev = node;
}
for (int i = 1; i <= n; ++i) {
printf("%d\n", parent[i]);
}
return 0;
}