-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path20119_topology.cpp
83 lines (82 loc) · 1.47 KB
/
20119_topology.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
76
77
78
79
80
81
82
83
// 210627 #20119 Ŭ·¹¾î¿Í ¹°¾à Gold II
// topology + indexing
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define all(v) (v).begin(), (v).end()
#define press(v) (v).erase(unique(all(v)), (v).end())
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
const int MAX = 200001;
const int INF = 0x3f3f3f3f;
const ll LNF = 1e18;
const int MOD = 1e9 + 7;
int N, M, L, in[MAX], a[MAX];
vector<int> c[MAX];
vector<int> ans;
bool visit[MAX];
int main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(false);
cin >> N >> M;
fill(in, in + N, INF);
for (int i = 0; i < M; i++) {
cin >> in[i];
for (int j = 0; j < in[i]; j++) {
int n;
cin >> n;
c[n].push_back(i);
}
// same medicine recipe
// so new indexing
cin >> a[i];
}
cin >> L;
queue<int> q;
for (int i = 0; i < L; i++) {
int n;
cin >> n;
if (visit[n])
continue;
ans.push_back(n);
q.push(n);
for (auto j : c[n]) {
in[j]--;
if (!in[j]) {
ans.push_back(a[j]);
q.push(a[j]);
}
}
visit[n] = true;
}
while (!q.empty()) {
int cur = q.front();
q.pop();
if (visit[cur])
continue;
for (auto i : c[cur]) {
in[i]--;
if (!(in[i])) {
ans.push_back(a[i]);
q.push(a[i]);
}
}
visit[cur] = true;
}
sort(all(ans));
press(ans);
cout << ans.size() << "\n";
for (auto i : ans) {
cout << i << " ";
}
}