aboutsummaryrefslogtreecommitdiff
path: root/2018/day2/sweetgrass.cpp
blob: 96380bf48e77a664d7f30a195aaa9ffa440cf7da (plain)
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
#include <bits/stdc++.h>
using namespace std;

int N, idx[100001];
bitset<100001> vis, ans;
vector<int> path;
unordered_set<int> G[100001];

void solve() {
	vis.reset();
	for (int i = 0; i < path.size() - 1; ++i) {
		G[path[i]].erase(path[i + 1]);
	}
	memset(idx, -1, sizeof idx);
	for (int i = 0; i < path.size(); ++i) idx[path[i]] = i;
	int m = 0;
	ans.set();
	for (int i = 0; i < path.size() - 1; ++i) {
		m = max(i + 1, m);
		queue<int> q;
		q.push(path[i]);
		while (q.size()) {
			int u = q.front();
			q.pop();
			if (vis[u]) continue;
			vis[u] = 1;
			if (u != path[i] && idx[u] != -1) {
				for (int j = m; j < idx[u]; ++j)
					ans[j] = 0;
				m = max(idx[u], m);
				continue;
			}			
			for (auto& v : G[u]) {
				if (!vis[v]) q.push(v);
			}
		}
	}
	vector<int> out;
	for (int i = 1; i < path.size() - 1; ++i)
		if (ans[i]) out.push_back(path[i]);
	sort(begin(out), end(out));
	cout << out.size() << '\n';
	for (auto& x : out) cout << x << '\n';
}

void dfs(int u) {
	vis[u] = 1;
	path.push_back(u);
	if (u == N) {
		solve();
		exit(0);
	}	
	for (auto& v : G[u]) {
		if (!vis[v]) dfs(v);
	}
	path.pop_back();
}

int main() {
	int M; cin >> N >> M;
	while (M--) {
		int a, b; cin >> a >> b;
		G[a].insert(b);
	}
	dfs(1);
}