#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
/*
- Values in different connected components are independent of each other.
- When DFS'ing a CC, the starting vertex has a value of x
- For every edge (a, b, c)
If b is unexplored, value[b] = c - value[a]
If b is explored, but the equation c = value[a] + value[b] gives no additional information, do nothing
If b is explored, and the equation gives additional information, use it to compute x.
Check for contradictions later.
*/
int N, M;
vector<int> edge[100001];
vector<int> color[100001];
vector<double> x_coeff(100001, 0.0);
vector<double> c_coeff(100001, 0.0);
vector<int> cc(100001, 0);
int cc_count = 0;
vector<double> xval(1);
vector<double> findx;
void dfs(int u)
{
findx.push_back(c_coeff[u] * x_coeff[u] * -1.0);
for(int i = 0; i < edge[u].size(); i++)
{
int v = edge[u][i];
int c = color[u][i];
if(!cc[v])
{
cc[v] = cc_count;
x_coeff[v] = -1.0 * x_coeff[u];
c_coeff[v] = double(c) - c_coeff[u];
dfs(v);
}
else
{
if(x_coeff[u] + x_coeff[v] == 0.0) continue;
xval.back() = (double(c) - (c_coeff[u] + c_coeff[v]))/(x_coeff[u] + x_coeff[v]);
}
}
}
int main()
{
int N, M;
cin >> N >> M;
int a, b, c;
for(int i = 1; i <= M; i++)
{
cin >> a >> b >> c;
edge[a].push_back(b);
color[a].push_back(c);
edge[b].push_back(a);
color[b].push_back(c);
}
for(int i = 1; i <= N; i++)
{
if(cc[i]) continue;
cc_count++;
cc[i] = cc_count;
x_coeff[i] = 1.0;
xval.push_back(-9999.9);
findx.clear();
dfs(i);
if(xval.back() == -9999.9)
{
sort(findx.begin(), findx.end());
xval.back() = findx[findx.size()/2];
}
}
double val[N+1];
for(int i = 1; i <= N; i++) val[i] = x_coeff[i] * xval[cc[i]] + c_coeff[i];
for(int i = 1; i <= N; i++)
{
for(int j = 0; j < edge[i].size(); j++)
{
if(val[i] + val[edge[i][j]] != color[i][j])
{
cout << "NO\n";
return 0;
}
}
}
cout << "YES\n";
for(int i = 1; i <= N; i++) cout << val[i] << ' ';
cout << '\n';
}
Compilation message
Graph.cpp: In function 'void dfs(int)':
Graph.cpp:32:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
32 | for(int i = 0; i < edge[u].size(); i++)
| ~~^~~~~~~~~~~~~~~~
Graph.cpp: In function 'int main()':
Graph.cpp:89:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
89 | for(int j = 0; j < edge[i].size(); j++)
| ~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7020 KB |
answer = YES |
2 |
Correct |
5 ms |
7020 KB |
answer = YES |
3 |
Correct |
6 ms |
7040 KB |
answer = YES |
4 |
Correct |
4 ms |
7020 KB |
answer = NO |
5 |
Correct |
5 ms |
7020 KB |
answer = YES |
6 |
Correct |
5 ms |
7040 KB |
answer = YES |
7 |
Correct |
5 ms |
7020 KB |
answer = YES |
8 |
Correct |
5 ms |
7020 KB |
answer = YES |
9 |
Correct |
5 ms |
7020 KB |
answer = NO |
10 |
Correct |
5 ms |
7020 KB |
answer = YES |
11 |
Correct |
5 ms |
7040 KB |
answer = YES |
12 |
Correct |
5 ms |
7020 KB |
answer = NO |
13 |
Correct |
5 ms |
7020 KB |
answer = YES |
14 |
Correct |
5 ms |
7020 KB |
answer = YES |
15 |
Correct |
5 ms |
6892 KB |
answer = YES |
16 |
Correct |
5 ms |
7020 KB |
answer = YES |
17 |
Correct |
5 ms |
7020 KB |
answer = YES |
18 |
Correct |
5 ms |
7020 KB |
answer = YES |
19 |
Correct |
5 ms |
7020 KB |
answer = YES |
20 |
Correct |
5 ms |
7020 KB |
answer = YES |
21 |
Correct |
5 ms |
7040 KB |
answer = YES |
22 |
Correct |
5 ms |
7020 KB |
answer = NO |
23 |
Correct |
5 ms |
7020 KB |
answer = NO |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7020 KB |
answer = YES |
2 |
Correct |
5 ms |
7020 KB |
answer = YES |
3 |
Correct |
6 ms |
7040 KB |
answer = YES |
4 |
Correct |
4 ms |
7020 KB |
answer = NO |
5 |
Correct |
5 ms |
7020 KB |
answer = YES |
6 |
Correct |
5 ms |
7040 KB |
answer = YES |
7 |
Correct |
5 ms |
7020 KB |
answer = YES |
8 |
Correct |
5 ms |
7020 KB |
answer = YES |
9 |
Correct |
5 ms |
7020 KB |
answer = NO |
10 |
Correct |
5 ms |
7020 KB |
answer = YES |
11 |
Correct |
5 ms |
7040 KB |
answer = YES |
12 |
Correct |
5 ms |
7020 KB |
answer = NO |
13 |
Correct |
5 ms |
7020 KB |
answer = YES |
14 |
Correct |
5 ms |
7020 KB |
answer = YES |
15 |
Correct |
5 ms |
6892 KB |
answer = YES |
16 |
Correct |
5 ms |
7020 KB |
answer = YES |
17 |
Correct |
5 ms |
7020 KB |
answer = YES |
18 |
Correct |
5 ms |
7020 KB |
answer = YES |
19 |
Correct |
5 ms |
7020 KB |
answer = YES |
20 |
Correct |
5 ms |
7020 KB |
answer = YES |
21 |
Correct |
5 ms |
7040 KB |
answer = YES |
22 |
Correct |
5 ms |
7020 KB |
answer = NO |
23 |
Correct |
5 ms |
7020 KB |
answer = NO |
24 |
Correct |
5 ms |
7020 KB |
answer = YES |
25 |
Correct |
5 ms |
7020 KB |
answer = YES |
26 |
Correct |
5 ms |
7020 KB |
answer = YES |
27 |
Correct |
5 ms |
7020 KB |
answer = YES |
28 |
Correct |
5 ms |
7020 KB |
answer = YES |
29 |
Correct |
5 ms |
7020 KB |
answer = YES |
30 |
Correct |
5 ms |
7020 KB |
answer = NO |
31 |
Correct |
5 ms |
7020 KB |
answer = YES |
32 |
Correct |
5 ms |
7020 KB |
answer = YES |
33 |
Correct |
5 ms |
7020 KB |
answer = YES |
34 |
Correct |
5 ms |
7020 KB |
answer = YES |
35 |
Correct |
5 ms |
7020 KB |
answer = YES |
36 |
Correct |
5 ms |
7040 KB |
answer = YES |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7020 KB |
answer = YES |
2 |
Correct |
5 ms |
7020 KB |
answer = YES |
3 |
Correct |
6 ms |
7040 KB |
answer = YES |
4 |
Correct |
4 ms |
7020 KB |
answer = NO |
5 |
Correct |
5 ms |
7020 KB |
answer = YES |
6 |
Correct |
5 ms |
7040 KB |
answer = YES |
7 |
Correct |
5 ms |
7020 KB |
answer = YES |
8 |
Correct |
5 ms |
7020 KB |
answer = YES |
9 |
Correct |
5 ms |
7020 KB |
answer = NO |
10 |
Correct |
5 ms |
7020 KB |
answer = YES |
11 |
Correct |
5 ms |
7040 KB |
answer = YES |
12 |
Correct |
5 ms |
7020 KB |
answer = NO |
13 |
Correct |
5 ms |
7020 KB |
answer = YES |
14 |
Correct |
5 ms |
7020 KB |
answer = YES |
15 |
Correct |
5 ms |
6892 KB |
answer = YES |
16 |
Correct |
5 ms |
7020 KB |
answer = YES |
17 |
Correct |
5 ms |
7020 KB |
answer = YES |
18 |
Correct |
5 ms |
7020 KB |
answer = YES |
19 |
Correct |
5 ms |
7020 KB |
answer = YES |
20 |
Correct |
5 ms |
7020 KB |
answer = YES |
21 |
Correct |
5 ms |
7040 KB |
answer = YES |
22 |
Correct |
5 ms |
7020 KB |
answer = NO |
23 |
Correct |
5 ms |
7020 KB |
answer = NO |
24 |
Correct |
5 ms |
7020 KB |
answer = YES |
25 |
Correct |
5 ms |
7020 KB |
answer = YES |
26 |
Correct |
5 ms |
7020 KB |
answer = YES |
27 |
Correct |
5 ms |
7020 KB |
answer = YES |
28 |
Correct |
5 ms |
7020 KB |
answer = YES |
29 |
Correct |
5 ms |
7020 KB |
answer = YES |
30 |
Correct |
5 ms |
7020 KB |
answer = NO |
31 |
Correct |
5 ms |
7020 KB |
answer = YES |
32 |
Correct |
5 ms |
7020 KB |
answer = YES |
33 |
Correct |
5 ms |
7020 KB |
answer = YES |
34 |
Correct |
5 ms |
7020 KB |
answer = YES |
35 |
Correct |
5 ms |
7020 KB |
answer = YES |
36 |
Correct |
5 ms |
7040 KB |
answer = YES |
37 |
Correct |
5 ms |
7020 KB |
answer = YES |
38 |
Correct |
5 ms |
7032 KB |
answer = YES |
39 |
Correct |
5 ms |
7020 KB |
answer = YES |
40 |
Correct |
6 ms |
7020 KB |
answer = YES |
41 |
Correct |
6 ms |
7032 KB |
answer = NO |
42 |
Correct |
6 ms |
7020 KB |
answer = YES |
43 |
Correct |
6 ms |
7020 KB |
answer = YES |
44 |
Correct |
6 ms |
7020 KB |
answer = YES |
45 |
Correct |
6 ms |
7020 KB |
answer = YES |
46 |
Correct |
5 ms |
7148 KB |
answer = YES |
47 |
Correct |
7 ms |
7148 KB |
answer = YES |
48 |
Correct |
6 ms |
7148 KB |
answer = YES |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7020 KB |
answer = YES |
2 |
Correct |
5 ms |
7020 KB |
answer = YES |
3 |
Correct |
6 ms |
7040 KB |
answer = YES |
4 |
Correct |
4 ms |
7020 KB |
answer = NO |
5 |
Correct |
5 ms |
7020 KB |
answer = YES |
6 |
Correct |
5 ms |
7040 KB |
answer = YES |
7 |
Correct |
5 ms |
7020 KB |
answer = YES |
8 |
Correct |
5 ms |
7020 KB |
answer = YES |
9 |
Correct |
5 ms |
7020 KB |
answer = NO |
10 |
Correct |
5 ms |
7020 KB |
answer = YES |
11 |
Correct |
5 ms |
7040 KB |
answer = YES |
12 |
Correct |
5 ms |
7020 KB |
answer = NO |
13 |
Correct |
5 ms |
7020 KB |
answer = YES |
14 |
Correct |
5 ms |
7020 KB |
answer = YES |
15 |
Correct |
5 ms |
6892 KB |
answer = YES |
16 |
Correct |
5 ms |
7020 KB |
answer = YES |
17 |
Correct |
5 ms |
7020 KB |
answer = YES |
18 |
Correct |
5 ms |
7020 KB |
answer = YES |
19 |
Correct |
5 ms |
7020 KB |
answer = YES |
20 |
Correct |
5 ms |
7020 KB |
answer = YES |
21 |
Correct |
5 ms |
7040 KB |
answer = YES |
22 |
Correct |
5 ms |
7020 KB |
answer = NO |
23 |
Correct |
5 ms |
7020 KB |
answer = NO |
24 |
Correct |
5 ms |
7020 KB |
answer = YES |
25 |
Correct |
5 ms |
7020 KB |
answer = YES |
26 |
Correct |
5 ms |
7020 KB |
answer = YES |
27 |
Correct |
5 ms |
7020 KB |
answer = YES |
28 |
Correct |
5 ms |
7020 KB |
answer = YES |
29 |
Correct |
5 ms |
7020 KB |
answer = YES |
30 |
Correct |
5 ms |
7020 KB |
answer = NO |
31 |
Correct |
5 ms |
7020 KB |
answer = YES |
32 |
Correct |
5 ms |
7020 KB |
answer = YES |
33 |
Correct |
5 ms |
7020 KB |
answer = YES |
34 |
Correct |
5 ms |
7020 KB |
answer = YES |
35 |
Correct |
5 ms |
7020 KB |
answer = YES |
36 |
Correct |
5 ms |
7040 KB |
answer = YES |
37 |
Correct |
5 ms |
7020 KB |
answer = YES |
38 |
Correct |
5 ms |
7032 KB |
answer = YES |
39 |
Correct |
5 ms |
7020 KB |
answer = YES |
40 |
Correct |
6 ms |
7020 KB |
answer = YES |
41 |
Correct |
6 ms |
7032 KB |
answer = NO |
42 |
Correct |
6 ms |
7020 KB |
answer = YES |
43 |
Correct |
6 ms |
7020 KB |
answer = YES |
44 |
Correct |
6 ms |
7020 KB |
answer = YES |
45 |
Correct |
6 ms |
7020 KB |
answer = YES |
46 |
Correct |
5 ms |
7148 KB |
answer = YES |
47 |
Correct |
7 ms |
7148 KB |
answer = YES |
48 |
Correct |
6 ms |
7148 KB |
answer = YES |
49 |
Correct |
20 ms |
8044 KB |
answer = YES |
50 |
Correct |
22 ms |
8428 KB |
answer = YES |
51 |
Correct |
20 ms |
8428 KB |
answer = YES |
52 |
Correct |
18 ms |
8428 KB |
answer = NO |
53 |
Correct |
7 ms |
7020 KB |
answer = YES |
54 |
Correct |
8 ms |
7276 KB |
answer = YES |
55 |
Correct |
12 ms |
7532 KB |
answer = YES |
56 |
Correct |
20 ms |
8044 KB |
answer = YES |
57 |
Correct |
19 ms |
7916 KB |
answer = YES |
58 |
Correct |
21 ms |
7916 KB |
answer = YES |
59 |
Correct |
18 ms |
7916 KB |
answer = YES |
60 |
Correct |
20 ms |
7916 KB |
answer = YES |
61 |
Correct |
13 ms |
7532 KB |
answer = YES |
62 |
Correct |
180 ms |
14720 KB |
answer = NO |
63 |
Correct |
192 ms |
14828 KB |
answer = YES |
64 |
Correct |
181 ms |
14700 KB |
answer = NO |
65 |
Correct |
193 ms |
14828 KB |
answer = YES |
66 |
Correct |
8 ms |
7148 KB |
answer = YES |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7020 KB |
answer = YES |
2 |
Correct |
5 ms |
7020 KB |
answer = YES |
3 |
Correct |
6 ms |
7040 KB |
answer = YES |
4 |
Correct |
4 ms |
7020 KB |
answer = NO |
5 |
Correct |
5 ms |
7020 KB |
answer = YES |
6 |
Correct |
5 ms |
7040 KB |
answer = YES |
7 |
Correct |
5 ms |
7020 KB |
answer = YES |
8 |
Correct |
5 ms |
7020 KB |
answer = YES |
9 |
Correct |
5 ms |
7020 KB |
answer = NO |
10 |
Correct |
5 ms |
7020 KB |
answer = YES |
11 |
Correct |
5 ms |
7040 KB |
answer = YES |
12 |
Correct |
5 ms |
7020 KB |
answer = NO |
13 |
Correct |
5 ms |
7020 KB |
answer = YES |
14 |
Correct |
5 ms |
7020 KB |
answer = YES |
15 |
Correct |
5 ms |
6892 KB |
answer = YES |
16 |
Correct |
5 ms |
7020 KB |
answer = YES |
17 |
Correct |
5 ms |
7020 KB |
answer = YES |
18 |
Correct |
5 ms |
7020 KB |
answer = YES |
19 |
Correct |
5 ms |
7020 KB |
answer = YES |
20 |
Correct |
5 ms |
7020 KB |
answer = YES |
21 |
Correct |
5 ms |
7040 KB |
answer = YES |
22 |
Correct |
5 ms |
7020 KB |
answer = NO |
23 |
Correct |
5 ms |
7020 KB |
answer = NO |
24 |
Correct |
5 ms |
7020 KB |
answer = YES |
25 |
Correct |
5 ms |
7020 KB |
answer = YES |
26 |
Correct |
5 ms |
7020 KB |
answer = YES |
27 |
Correct |
5 ms |
7020 KB |
answer = YES |
28 |
Correct |
5 ms |
7020 KB |
answer = YES |
29 |
Correct |
5 ms |
7020 KB |
answer = YES |
30 |
Correct |
5 ms |
7020 KB |
answer = NO |
31 |
Correct |
5 ms |
7020 KB |
answer = YES |
32 |
Correct |
5 ms |
7020 KB |
answer = YES |
33 |
Correct |
5 ms |
7020 KB |
answer = YES |
34 |
Correct |
5 ms |
7020 KB |
answer = YES |
35 |
Correct |
5 ms |
7020 KB |
answer = YES |
36 |
Correct |
5 ms |
7040 KB |
answer = YES |
37 |
Correct |
5 ms |
7020 KB |
answer = YES |
38 |
Correct |
5 ms |
7032 KB |
answer = YES |
39 |
Correct |
5 ms |
7020 KB |
answer = YES |
40 |
Correct |
6 ms |
7020 KB |
answer = YES |
41 |
Correct |
6 ms |
7032 KB |
answer = NO |
42 |
Correct |
6 ms |
7020 KB |
answer = YES |
43 |
Correct |
6 ms |
7020 KB |
answer = YES |
44 |
Correct |
6 ms |
7020 KB |
answer = YES |
45 |
Correct |
6 ms |
7020 KB |
answer = YES |
46 |
Correct |
5 ms |
7148 KB |
answer = YES |
47 |
Correct |
7 ms |
7148 KB |
answer = YES |
48 |
Correct |
6 ms |
7148 KB |
answer = YES |
49 |
Correct |
20 ms |
8044 KB |
answer = YES |
50 |
Correct |
22 ms |
8428 KB |
answer = YES |
51 |
Correct |
20 ms |
8428 KB |
answer = YES |
52 |
Correct |
18 ms |
8428 KB |
answer = NO |
53 |
Correct |
7 ms |
7020 KB |
answer = YES |
54 |
Correct |
8 ms |
7276 KB |
answer = YES |
55 |
Correct |
12 ms |
7532 KB |
answer = YES |
56 |
Correct |
20 ms |
8044 KB |
answer = YES |
57 |
Correct |
19 ms |
7916 KB |
answer = YES |
58 |
Correct |
21 ms |
7916 KB |
answer = YES |
59 |
Correct |
18 ms |
7916 KB |
answer = YES |
60 |
Correct |
20 ms |
7916 KB |
answer = YES |
61 |
Correct |
13 ms |
7532 KB |
answer = YES |
62 |
Correct |
180 ms |
14720 KB |
answer = NO |
63 |
Correct |
192 ms |
14828 KB |
answer = YES |
64 |
Correct |
181 ms |
14700 KB |
answer = NO |
65 |
Correct |
193 ms |
14828 KB |
answer = YES |
66 |
Correct |
8 ms |
7148 KB |
answer = YES |
67 |
Correct |
158 ms |
23648 KB |
answer = YES |
68 |
Correct |
152 ms |
23520 KB |
answer = YES |
69 |
Correct |
148 ms |
23648 KB |
answer = YES |
70 |
Correct |
242 ms |
27232 KB |
answer = YES |
71 |
Correct |
157 ms |
23776 KB |
answer = YES |
72 |
Correct |
223 ms |
16736 KB |
answer = YES |
73 |
Correct |
221 ms |
16736 KB |
answer = YES |
74 |
Correct |
127 ms |
16740 KB |
answer = YES |
75 |
Correct |
92 ms |
16612 KB |
answer = NO |
76 |
Correct |
24 ms |
8300 KB |
answer = YES |
77 |
Correct |
45 ms |
9448 KB |
answer = YES |
78 |
Correct |
97 ms |
11236 KB |
answer = YES |
79 |
Correct |
185 ms |
15460 KB |
answer = YES |
80 |
Correct |
128 ms |
16740 KB |
answer = YES |
81 |
Correct |
144 ms |
19552 KB |
answer = NO |
82 |
Correct |
239 ms |
23136 KB |
answer = YES |
83 |
Correct |
229 ms |
23776 KB |
answer = YES |
84 |
Correct |
231 ms |
23776 KB |
answer = YES |
85 |
Correct |
158 ms |
23648 KB |
answer = YES |
86 |
Correct |
153 ms |
23648 KB |
answer = YES |
87 |
Correct |
188 ms |
17768 KB |
answer = NO |
88 |
Correct |
265 ms |
18916 KB |
answer = YES |
89 |
Correct |
191 ms |
15976 KB |
answer = YES |
90 |
Correct |
195 ms |
15976 KB |
answer = YES |
91 |
Correct |
211 ms |
16104 KB |
answer = YES |
92 |
Correct |
99 ms |
12008 KB |
answer = YES |
93 |
Correct |
101 ms |
11880 KB |
answer = YES |
94 |
Correct |
186 ms |
23392 KB |
answer = NO |
95 |
Correct |
159 ms |
15720 KB |
answer = NO |
96 |
Correct |
421 ms |
24032 KB |
answer = YES |
97 |
Correct |
111 ms |
23392 KB |
answer = NO |