-
Notifications
You must be signed in to change notification settings - Fork 184
/
Copy pathe.cc
62 lines (58 loc) · 1.21 KB
/
e.cc
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
// https://codeforces.com/contest/1407/problem/E
#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using qi = queue<int>;
using si = set<int>;
using misi = map<int, si>;
using graph = vector<misi>;
const int INF = 1000000;
int main() {
cin.tie(0), ios::sync_with_stdio(0);
int n, m, u, v, t;
cin >> n >> m;
graph g(n);
for (int i = 0; i < m; i++) {
cin >> u >> v >> t;
u--, v--;
g[v][u].insert(t);
}
vi c(n);
vi d(n, INF);
vi s(n, 0);
d[n - 1] = 0;
qi q;
q.push(n - 1);
while (!q.empty()) {
int u = q.front();
q.pop();
if (u == 0) break;
vi todelete;
for (auto [v, colors] : g[u]) {
if (d[v] != INF) continue;
if (s[v]) {
for (int x : colors)
if (x == c[v]) {
d[v] = d[u] + 1;
q.push(v);
}
todelete.push_back(v);
continue;
}
if (colors.size() != 2) {
todelete.push_back(v);
c[v] = !*colors.begin();
s[v] = 1;
} else {
d[v] = d[u] + 1;
q.push(v);
}
}
for (int v : todelete) g[u].erase(v);
}
if (d[0] == INF) cout << "-1\n";
else
cout << d[0] << '\n';
for (int x : c) cout << x;
cout << '\n';
}