-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathE.cpp
More file actions
67 lines (58 loc) · 1.2 KB
/
Copy pathE.cpp
File metadata and controls
67 lines (58 loc) · 1.2 KB
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
#include <bits/stdc++.h>
using namespace std;
#define cerr cerr << "DEBUG "
constexpr int INF = 1e9;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
vector<set<pair<int, int>>> st(n);
for (int i = 0; i < m; ++i) {
int v, u, x;
cin >> v >> u >> x;
--v, --u;
if (st[u].count({v, 2})) {
continue;
}
if (st[u].count({v, !x})) {
st[u].erase({v, !x});
st[u].insert({v, 2});
} else {
st[u].insert({v, x});
}
}
vector<int> x(n, -1);
vector<int> dist(n, INF);
dist[n - 1] = 0;
x[n - 1] = 0;
queue<int> q;
q.push(n - 1);
while (!q.empty()) {
int v = q.front();
q.pop();
for (auto &p : st[v]) {
if (dist[p.first] < INF) {
continue;
}
if (p.second != 2) {
if (~x[p.first] && (!p.second) != x[p.first]) {
dist[p.first] = dist[v] + 1;
q.push(p.first);
} else {
x[p.first] = !p.second;
}
} else {
if (x[p.first] == -1) {
x[p.first] = 0;
}
dist[p.first] = dist[v] + 1;
q.push(p.first);
}
}
}
cout << (dist[0] >= INF ? -1 : dist[0]) << '\n';
for (int i = 0; i < n; ++i) {
cout << (x[i] < 0 ? 0 : x[i]);
}
}