#include <bits/stdc++.h>
using namespace std;
#define int int64_t
#define pii pair<int, int>
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define unas mp(1e9, 1e9)
#define undef 1e15
bool contradiction = false;
struct Node {
int index;
set<pii> neigh;
pii exp = unas;
double minVal = undef;
};
bool operator==(const pii &x1, const pii &x2) {
return x1.f == x2.f && x1.s == x2.s;
}
bool operator!=(const pii &x1, const pii &x2) {
return x1.f != x2.f || x1.s != x2.s;
}
pii operator+(const pii &x1, const pii &x2) {
return mp(x1.f+x2.f, x1.s+x2.s);
}
int conv(const pii &x) {
if (x.f <= 0) return x.s;
else return -x.s;
}
void pPair(const pii &x) {
cout << x.f << " " << x.s << "\n";
}
void assignExp(vector<Node> &g, int idx) {
g[idx].exp = mp(1, 0);
// bfs
queue<int> q;
q.push(idx);
while (!q.empty()) {
int t=q.front();q.pop();
pii ex = g[t].exp;
for (pii x : g[t].neigh) {
if (g[x.f].exp != unas) continue;
g[x.f].exp = mp(-ex.f, x.s - ex.s);
q.push(x.f);
}
}
double search = 1e15;
q.push(idx);
vector<bool> v((int)g.size(), false);
v[idx]=true;
while (!q.empty()) {
int t=q.front();q.pop();
pii ex = g[t].exp;
for (pii x : g[t].neigh) {
if ((ex + g[x.f].exp).first != 0) {
// cout << t << " " << x.f << "\n";
// pPair(ex);pPair(g[x.f].exp);
pii res = ex + g[x.f].exp;
double lft = (double)x.s;
lft -= (double)res.s;
search = lft / (double)res.f;
while (!q.empty())q.pop();
break;
}
else if (!v[x.f]) {
v[x.f]=true;
q.push(x.f);
}
}
}
if (search == 1e15) {
q.push(idx);
vector<bool> visited((int)g.size(), false);
visited[idx]=true;
vector<int> coeff;
coeff.pb(conv(g[idx].exp));
while (!q.empty()) {
int t=q.front();q.pop();
for (pii x : g[t].neigh) {
if (!visited[x.f]) {
visited[x.f]=true;
coeff.pb(conv(g[x.f].exp));
q.push(x.f);
}
}
}
sort(coeff.begin(), coeff.end());
search = (double)coeff[(int)coeff.size()/2];
}
q.push(idx);
g[idx].minVal = search;
while (!q.empty()) {
int t=q.front();q.pop();
for (pii x : g[t].neigh) {
if (g[x.f].minVal == undef) {
g[x.f].minVal = (double)(g[x.f].exp.f) * search + (double)g[x.f].exp.s;
q.push(x.f);
}
}
}
q.push(idx);
vector<bool> visited((int)g.size(), false);
while (!q.empty()) {
int t=q.front();q.pop();
for (pii x : g[t].neigh) {
if (g[t].minVal + g[x.f].minVal != (double)x.s) contradiction = true;
// printf("case %ld %ld\n", t, x.f);
// printf("value of first : %lf, value of second : %lf, value of edge : %ld\n\n", g[t].minVal, g[x.f].minVal, x.s);
if (!visited[x.f]) {
visited[x.f]=true;
q.push(x.f);
}
}
}
}
signed main() {
ios::sync_with_stdio(false);cin.tie(0);
int n, m;cin>>n>>m;
vector<Node> g(n);
for (int i=0;i<n;i++) {
g[i].index = i;
}
for (int i=0;i<m;i++) {
int start, end, code;
cin >> start >> end >> code;
start--;end--;
if (g[start].neigh.find(mp(end, 3 - code)) != g[start].neigh.end()) {
cout << "NO\n";
return 0;
}
g[start].neigh.insert(mp(end, code));
g[end].neigh.insert(mp(start, code));
}
for (int i=0;i<n;i++) {
if (g[i].exp == unas) {
assignExp(g, i);
if (contradiction) {
cout << "NO\n";
return 0;
}
}
}
if (contradiction) {
cout << "NO\n";
return 0;
}
cout << "YES\n";
cout << g[0].minVal;
for (int i=1;i<n;i++) {
cout << " " << g[i].minVal;
}
cout << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
answer = YES |
2 |
Correct |
0 ms |
348 KB |
answer = YES |
3 |
Correct |
0 ms |
348 KB |
answer = YES |
4 |
Correct |
0 ms |
456 KB |
answer = NO |
5 |
Correct |
0 ms |
348 KB |
answer = YES |
6 |
Correct |
0 ms |
348 KB |
answer = YES |
7 |
Correct |
1 ms |
348 KB |
answer = YES |
8 |
Correct |
0 ms |
348 KB |
answer = YES |
9 |
Correct |
0 ms |
348 KB |
answer = NO |
10 |
Correct |
0 ms |
348 KB |
answer = YES |
11 |
Correct |
1 ms |
348 KB |
answer = YES |
12 |
Correct |
0 ms |
344 KB |
answer = NO |
13 |
Correct |
1 ms |
348 KB |
answer = YES |
14 |
Correct |
0 ms |
456 KB |
answer = YES |
15 |
Correct |
1 ms |
348 KB |
answer = YES |
16 |
Correct |
0 ms |
348 KB |
answer = YES |
17 |
Correct |
1 ms |
348 KB |
answer = YES |
18 |
Correct |
0 ms |
348 KB |
answer = YES |
19 |
Correct |
0 ms |
348 KB |
answer = YES |
20 |
Correct |
0 ms |
348 KB |
answer = YES |
21 |
Correct |
0 ms |
348 KB |
answer = YES |
22 |
Correct |
0 ms |
348 KB |
answer = NO |
23 |
Correct |
0 ms |
348 KB |
answer = NO |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
answer = YES |
2 |
Correct |
0 ms |
348 KB |
answer = YES |
3 |
Correct |
0 ms |
348 KB |
answer = YES |
4 |
Correct |
0 ms |
456 KB |
answer = NO |
5 |
Correct |
0 ms |
348 KB |
answer = YES |
6 |
Correct |
0 ms |
348 KB |
answer = YES |
7 |
Correct |
1 ms |
348 KB |
answer = YES |
8 |
Correct |
0 ms |
348 KB |
answer = YES |
9 |
Correct |
0 ms |
348 KB |
answer = NO |
10 |
Correct |
0 ms |
348 KB |
answer = YES |
11 |
Correct |
1 ms |
348 KB |
answer = YES |
12 |
Correct |
0 ms |
344 KB |
answer = NO |
13 |
Correct |
1 ms |
348 KB |
answer = YES |
14 |
Correct |
0 ms |
456 KB |
answer = YES |
15 |
Correct |
1 ms |
348 KB |
answer = YES |
16 |
Correct |
0 ms |
348 KB |
answer = YES |
17 |
Correct |
1 ms |
348 KB |
answer = YES |
18 |
Correct |
0 ms |
348 KB |
answer = YES |
19 |
Correct |
0 ms |
348 KB |
answer = YES |
20 |
Correct |
0 ms |
348 KB |
answer = YES |
21 |
Correct |
0 ms |
348 KB |
answer = YES |
22 |
Correct |
0 ms |
348 KB |
answer = NO |
23 |
Correct |
0 ms |
348 KB |
answer = NO |
24 |
Correct |
1 ms |
600 KB |
answer = YES |
25 |
Correct |
1 ms |
348 KB |
answer = YES |
26 |
Correct |
0 ms |
456 KB |
answer = YES |
27 |
Correct |
1 ms |
424 KB |
answer = YES |
28 |
Correct |
1 ms |
452 KB |
answer = YES |
29 |
Correct |
0 ms |
600 KB |
answer = YES |
30 |
Correct |
0 ms |
348 KB |
answer = NO |
31 |
Correct |
1 ms |
348 KB |
answer = YES |
32 |
Correct |
0 ms |
348 KB |
answer = YES |
33 |
Correct |
1 ms |
348 KB |
answer = YES |
34 |
Correct |
1 ms |
348 KB |
answer = YES |
35 |
Correct |
1 ms |
348 KB |
answer = YES |
36 |
Correct |
0 ms |
452 KB |
answer = YES |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
answer = YES |
2 |
Correct |
0 ms |
348 KB |
answer = YES |
3 |
Correct |
0 ms |
348 KB |
answer = YES |
4 |
Correct |
0 ms |
456 KB |
answer = NO |
5 |
Correct |
0 ms |
348 KB |
answer = YES |
6 |
Correct |
0 ms |
348 KB |
answer = YES |
7 |
Correct |
1 ms |
348 KB |
answer = YES |
8 |
Correct |
0 ms |
348 KB |
answer = YES |
9 |
Correct |
0 ms |
348 KB |
answer = NO |
10 |
Correct |
0 ms |
348 KB |
answer = YES |
11 |
Correct |
1 ms |
348 KB |
answer = YES |
12 |
Correct |
0 ms |
344 KB |
answer = NO |
13 |
Correct |
1 ms |
348 KB |
answer = YES |
14 |
Correct |
0 ms |
456 KB |
answer = YES |
15 |
Correct |
1 ms |
348 KB |
answer = YES |
16 |
Correct |
0 ms |
348 KB |
answer = YES |
17 |
Correct |
1 ms |
348 KB |
answer = YES |
18 |
Correct |
0 ms |
348 KB |
answer = YES |
19 |
Correct |
0 ms |
348 KB |
answer = YES |
20 |
Correct |
0 ms |
348 KB |
answer = YES |
21 |
Correct |
0 ms |
348 KB |
answer = YES |
22 |
Correct |
0 ms |
348 KB |
answer = NO |
23 |
Correct |
0 ms |
348 KB |
answer = NO |
24 |
Correct |
1 ms |
600 KB |
answer = YES |
25 |
Correct |
1 ms |
348 KB |
answer = YES |
26 |
Correct |
0 ms |
456 KB |
answer = YES |
27 |
Correct |
1 ms |
424 KB |
answer = YES |
28 |
Correct |
1 ms |
452 KB |
answer = YES |
29 |
Correct |
0 ms |
600 KB |
answer = YES |
30 |
Correct |
0 ms |
348 KB |
answer = NO |
31 |
Correct |
1 ms |
348 KB |
answer = YES |
32 |
Correct |
0 ms |
348 KB |
answer = YES |
33 |
Correct |
1 ms |
348 KB |
answer = YES |
34 |
Correct |
1 ms |
348 KB |
answer = YES |
35 |
Correct |
1 ms |
348 KB |
answer = YES |
36 |
Correct |
0 ms |
452 KB |
answer = YES |
37 |
Correct |
1 ms |
600 KB |
answer = YES |
38 |
Correct |
1 ms |
348 KB |
answer = YES |
39 |
Correct |
1 ms |
344 KB |
answer = YES |
40 |
Correct |
1 ms |
604 KB |
answer = YES |
41 |
Correct |
1 ms |
520 KB |
answer = NO |
42 |
Correct |
1 ms |
604 KB |
answer = YES |
43 |
Correct |
1 ms |
604 KB |
answer = YES |
44 |
Correct |
1 ms |
604 KB |
answer = YES |
45 |
Correct |
1 ms |
592 KB |
answer = YES |
46 |
Correct |
1 ms |
568 KB |
answer = YES |
47 |
Correct |
1 ms |
604 KB |
answer = YES |
48 |
Correct |
1 ms |
604 KB |
answer = YES |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
answer = YES |
2 |
Correct |
0 ms |
348 KB |
answer = YES |
3 |
Correct |
0 ms |
348 KB |
answer = YES |
4 |
Correct |
0 ms |
456 KB |
answer = NO |
5 |
Correct |
0 ms |
348 KB |
answer = YES |
6 |
Correct |
0 ms |
348 KB |
answer = YES |
7 |
Correct |
1 ms |
348 KB |
answer = YES |
8 |
Correct |
0 ms |
348 KB |
answer = YES |
9 |
Correct |
0 ms |
348 KB |
answer = NO |
10 |
Correct |
0 ms |
348 KB |
answer = YES |
11 |
Correct |
1 ms |
348 KB |
answer = YES |
12 |
Correct |
0 ms |
344 KB |
answer = NO |
13 |
Correct |
1 ms |
348 KB |
answer = YES |
14 |
Correct |
0 ms |
456 KB |
answer = YES |
15 |
Correct |
1 ms |
348 KB |
answer = YES |
16 |
Correct |
0 ms |
348 KB |
answer = YES |
17 |
Correct |
1 ms |
348 KB |
answer = YES |
18 |
Correct |
0 ms |
348 KB |
answer = YES |
19 |
Correct |
0 ms |
348 KB |
answer = YES |
20 |
Correct |
0 ms |
348 KB |
answer = YES |
21 |
Correct |
0 ms |
348 KB |
answer = YES |
22 |
Correct |
0 ms |
348 KB |
answer = NO |
23 |
Correct |
0 ms |
348 KB |
answer = NO |
24 |
Correct |
1 ms |
600 KB |
answer = YES |
25 |
Correct |
1 ms |
348 KB |
answer = YES |
26 |
Correct |
0 ms |
456 KB |
answer = YES |
27 |
Correct |
1 ms |
424 KB |
answer = YES |
28 |
Correct |
1 ms |
452 KB |
answer = YES |
29 |
Correct |
0 ms |
600 KB |
answer = YES |
30 |
Correct |
0 ms |
348 KB |
answer = NO |
31 |
Correct |
1 ms |
348 KB |
answer = YES |
32 |
Correct |
0 ms |
348 KB |
answer = YES |
33 |
Correct |
1 ms |
348 KB |
answer = YES |
34 |
Correct |
1 ms |
348 KB |
answer = YES |
35 |
Correct |
1 ms |
348 KB |
answer = YES |
36 |
Correct |
0 ms |
452 KB |
answer = YES |
37 |
Correct |
1 ms |
600 KB |
answer = YES |
38 |
Correct |
1 ms |
348 KB |
answer = YES |
39 |
Correct |
1 ms |
344 KB |
answer = YES |
40 |
Correct |
1 ms |
604 KB |
answer = YES |
41 |
Correct |
1 ms |
520 KB |
answer = NO |
42 |
Correct |
1 ms |
604 KB |
answer = YES |
43 |
Correct |
1 ms |
604 KB |
answer = YES |
44 |
Correct |
1 ms |
604 KB |
answer = YES |
45 |
Correct |
1 ms |
592 KB |
answer = YES |
46 |
Correct |
1 ms |
568 KB |
answer = YES |
47 |
Correct |
1 ms |
604 KB |
answer = YES |
48 |
Correct |
1 ms |
604 KB |
answer = YES |
49 |
Correct |
9 ms |
2652 KB |
answer = YES |
50 |
Correct |
12 ms |
2876 KB |
answer = YES |
51 |
Correct |
8 ms |
2652 KB |
answer = YES |
52 |
Correct |
5 ms |
2648 KB |
answer = NO |
53 |
Correct |
1 ms |
604 KB |
answer = YES |
54 |
Correct |
3 ms |
1044 KB |
answer = YES |
55 |
Correct |
5 ms |
1624 KB |
answer = YES |
56 |
Correct |
9 ms |
2904 KB |
answer = YES |
57 |
Correct |
9 ms |
2396 KB |
answer = YES |
58 |
Correct |
8 ms |
2396 KB |
answer = YES |
59 |
Correct |
8 ms |
2396 KB |
answer = YES |
60 |
Correct |
9 ms |
2652 KB |
answer = YES |
61 |
Correct |
4 ms |
1372 KB |
answer = YES |
62 |
Correct |
6 ms |
2904 KB |
answer = NO |
63 |
Correct |
353 ms |
28152 KB |
answer = YES |
64 |
Correct |
335 ms |
28544 KB |
answer = NO |
65 |
Correct |
339 ms |
28240 KB |
answer = YES |
66 |
Correct |
2 ms |
860 KB |
answer = YES |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
answer = YES |
2 |
Correct |
0 ms |
348 KB |
answer = YES |
3 |
Correct |
0 ms |
348 KB |
answer = YES |
4 |
Correct |
0 ms |
456 KB |
answer = NO |
5 |
Correct |
0 ms |
348 KB |
answer = YES |
6 |
Correct |
0 ms |
348 KB |
answer = YES |
7 |
Correct |
1 ms |
348 KB |
answer = YES |
8 |
Correct |
0 ms |
348 KB |
answer = YES |
9 |
Correct |
0 ms |
348 KB |
answer = NO |
10 |
Correct |
0 ms |
348 KB |
answer = YES |
11 |
Correct |
1 ms |
348 KB |
answer = YES |
12 |
Correct |
0 ms |
344 KB |
answer = NO |
13 |
Correct |
1 ms |
348 KB |
answer = YES |
14 |
Correct |
0 ms |
456 KB |
answer = YES |
15 |
Correct |
1 ms |
348 KB |
answer = YES |
16 |
Correct |
0 ms |
348 KB |
answer = YES |
17 |
Correct |
1 ms |
348 KB |
answer = YES |
18 |
Correct |
0 ms |
348 KB |
answer = YES |
19 |
Correct |
0 ms |
348 KB |
answer = YES |
20 |
Correct |
0 ms |
348 KB |
answer = YES |
21 |
Correct |
0 ms |
348 KB |
answer = YES |
22 |
Correct |
0 ms |
348 KB |
answer = NO |
23 |
Correct |
0 ms |
348 KB |
answer = NO |
24 |
Correct |
1 ms |
600 KB |
answer = YES |
25 |
Correct |
1 ms |
348 KB |
answer = YES |
26 |
Correct |
0 ms |
456 KB |
answer = YES |
27 |
Correct |
1 ms |
424 KB |
answer = YES |
28 |
Correct |
1 ms |
452 KB |
answer = YES |
29 |
Correct |
0 ms |
600 KB |
answer = YES |
30 |
Correct |
0 ms |
348 KB |
answer = NO |
31 |
Correct |
1 ms |
348 KB |
answer = YES |
32 |
Correct |
0 ms |
348 KB |
answer = YES |
33 |
Correct |
1 ms |
348 KB |
answer = YES |
34 |
Correct |
1 ms |
348 KB |
answer = YES |
35 |
Correct |
1 ms |
348 KB |
answer = YES |
36 |
Correct |
0 ms |
452 KB |
answer = YES |
37 |
Correct |
1 ms |
600 KB |
answer = YES |
38 |
Correct |
1 ms |
348 KB |
answer = YES |
39 |
Correct |
1 ms |
344 KB |
answer = YES |
40 |
Correct |
1 ms |
604 KB |
answer = YES |
41 |
Correct |
1 ms |
520 KB |
answer = NO |
42 |
Correct |
1 ms |
604 KB |
answer = YES |
43 |
Correct |
1 ms |
604 KB |
answer = YES |
44 |
Correct |
1 ms |
604 KB |
answer = YES |
45 |
Correct |
1 ms |
592 KB |
answer = YES |
46 |
Correct |
1 ms |
568 KB |
answer = YES |
47 |
Correct |
1 ms |
604 KB |
answer = YES |
48 |
Correct |
1 ms |
604 KB |
answer = YES |
49 |
Correct |
9 ms |
2652 KB |
answer = YES |
50 |
Correct |
12 ms |
2876 KB |
answer = YES |
51 |
Correct |
8 ms |
2652 KB |
answer = YES |
52 |
Correct |
5 ms |
2648 KB |
answer = NO |
53 |
Correct |
1 ms |
604 KB |
answer = YES |
54 |
Correct |
3 ms |
1044 KB |
answer = YES |
55 |
Correct |
5 ms |
1624 KB |
answer = YES |
56 |
Correct |
9 ms |
2904 KB |
answer = YES |
57 |
Correct |
9 ms |
2396 KB |
answer = YES |
58 |
Correct |
8 ms |
2396 KB |
answer = YES |
59 |
Correct |
8 ms |
2396 KB |
answer = YES |
60 |
Correct |
9 ms |
2652 KB |
answer = YES |
61 |
Correct |
4 ms |
1372 KB |
answer = YES |
62 |
Correct |
6 ms |
2904 KB |
answer = NO |
63 |
Correct |
353 ms |
28152 KB |
answer = YES |
64 |
Correct |
335 ms |
28544 KB |
answer = NO |
65 |
Correct |
339 ms |
28240 KB |
answer = YES |
66 |
Correct |
2 ms |
860 KB |
answer = YES |
67 |
Correct |
79 ms |
23248 KB |
answer = YES |
68 |
Correct |
71 ms |
23248 KB |
answer = YES |
69 |
Correct |
64 ms |
22356 KB |
answer = YES |
70 |
Correct |
101 ms |
36020 KB |
answer = YES |
71 |
Correct |
76 ms |
22348 KB |
answer = YES |
72 |
Correct |
161 ms |
23152 KB |
answer = YES |
73 |
Correct |
135 ms |
22356 KB |
answer = YES |
74 |
Correct |
52 ms |
14672 KB |
answer = YES |
75 |
Correct |
38 ms |
15068 KB |
answer = NO |
76 |
Correct |
11 ms |
3160 KB |
answer = YES |
77 |
Correct |
22 ms |
6104 KB |
answer = YES |
78 |
Correct |
42 ms |
10220 KB |
answer = YES |
79 |
Correct |
115 ms |
19936 KB |
answer = YES |
80 |
Correct |
55 ms |
15324 KB |
answer = YES |
81 |
Correct |
75 ms |
22100 KB |
answer = NO |
82 |
Correct |
98 ms |
22636 KB |
answer = YES |
83 |
Correct |
142 ms |
22356 KB |
answer = YES |
84 |
Correct |
151 ms |
23284 KB |
answer = YES |
85 |
Correct |
71 ms |
23136 KB |
answer = YES |
86 |
Correct |
68 ms |
22356 KB |
answer = YES |
87 |
Correct |
72 ms |
22488 KB |
answer = NO |
88 |
Correct |
133 ms |
22668 KB |
answer = YES |
89 |
Correct |
103 ms |
22352 KB |
answer = YES |
90 |
Correct |
100 ms |
22436 KB |
answer = YES |
91 |
Correct |
107 ms |
22356 KB |
answer = YES |
92 |
Correct |
43 ms |
11356 KB |
answer = YES |
93 |
Correct |
41 ms |
11352 KB |
answer = YES |
94 |
Correct |
107 ms |
23248 KB |
answer = NO |
95 |
Correct |
48 ms |
22000 KB |
answer = NO |
96 |
Correct |
347 ms |
36604 KB |
answer = YES |
97 |
Correct |
41 ms |
23432 KB |
answer = NO |