#include <bits/stdc++.h>
using namespace std;
struct Node
{
vector<pair<Node*,bool>> neigh;
int x_parity = 0; // actual value = value + x*x_parity
float value = NAN;
bool seen = false; // collect_all_vi
bool seen2 = false; // set_x
variant<float,bool> assign(float val=0,int par=1) // float - unique solution for x
{ // bool - works for any/no x
if(!isnan(value))
{
if(x_parity==par) return val == value;
// solve linear equation x * x_parity + value == x*par + val
// x * (x_parity - par) == val - value
// x = val - value / (x_parity - par)
float x = ((float) val - value) / ((float) x_parity - par);
return x;
}
value = val;
x_parity = par;
float x = NAN;
for(auto nt : neigh)
{
auto n = nt.first;
auto t = nt.second;
auto a = n->assign((1+t)-val,-par);
if(a.index()==0)
{
// unique solution for x found
if (!isnan(x) && x != get<float>(a)) return false;
x = get<float>(a);
}
else if (!get<bool>(a)) return false;
}
if(isnan(x)) return true;
return x;
}
void collect_v_i(vector<int>& v_i) // v_i in abs(x-v_i)
{
if(seen) return;
seen = true;
v_i.push_back(-value*x_parity);
for(auto nt : neigh) nt.first->collect_v_i(v_i);
}
void set_x(float x)
{
if(seen2) return;
seen2 = true;
value += x*x_parity;
for(auto nt : neigh) nt.first->set_x(x);
}
// need to decide for value of x (probably small, but uncertain)
// the total weigth is a sum of elements of type abs(v_i+x)
// collect all v_i (traversing) - x must be some -v_i. nlogn given sorting.
// binary search over larger/smaller than these and these elements, gives linear relationship
};
signed main()
{
cin.tie(nullptr)->sync_with_stdio(false);
int N,M;
cin >> N >> M;
vector<Node> nodes(N);
for(int i = 0; i < M; i++)
{
int a,b,c;
cin >> a >> b >> c;
nodes[a-1].neigh.push_back({&nodes[b-1],c>1});
nodes[b-1].neigh.push_back({&nodes[a-1],c>1});
}
for(int t = 0; t < N; t++)
{
if(!isnan(nodes[t].value)) continue; // node already seen
auto a = nodes[t].assign();
if(a.index()==0) // a is a float => x is fixed
{
float x = get<float>(a);
nodes[t].set_x(x); // recursive
}
else
{
// a is a bool.
if (!get<bool>(a))
{
cout << "NO\n";
exit(0);
} // not possible.
// need to figure out optimal value for x.
vector<int> vi;
nodes[t].collect_v_i(vi);
sort(vi.begin(),vi.end());
int x = vi[0];
int cost = 0;
for(auto i : vi) cost += (i-x);
int min_cost = cost;
int min_x = x;
for(int i = 1; i < vi.size(); i++)
{
int next_x = vi[i];
cost -= (next_x-x)*(vi.size()-i);
cost += (next_x-x)*(i);
x = next_x;
if (min_cost > cost)
{
min_cost = cost;
min_x = x;
}
}
nodes[t].set_x(min_x);
}
}
cout << "YES\n" << nodes[0].value;
for(int i = 1; i < N; i++) cout << " " << nodes[i].value;
cout << "\n";
// assign "x" to some node
// continue traversing, getting either x+int or int-x for some edges
// if reencounter, might constrain x, set.
// repeat for all connected components
}
Compilation message
Graph.cpp: In function 'int main()':
Graph.cpp:105:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
105 | for(int i = 1; i < vi.size(); i++)
| ~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
answer = YES |
2 |
Correct |
0 ms |
212 KB |
answer = YES |
3 |
Correct |
1 ms |
324 KB |
answer = YES |
4 |
Correct |
1 ms |
212 KB |
answer = NO |
5 |
Correct |
0 ms |
212 KB |
answer = YES |
6 |
Correct |
0 ms |
212 KB |
answer = YES |
7 |
Correct |
0 ms |
320 KB |
answer = YES |
8 |
Correct |
0 ms |
212 KB |
answer = YES |
9 |
Correct |
1 ms |
316 KB |
answer = NO |
10 |
Correct |
0 ms |
320 KB |
answer = YES |
11 |
Correct |
1 ms |
212 KB |
answer = YES |
12 |
Correct |
1 ms |
212 KB |
answer = NO |
13 |
Correct |
1 ms |
324 KB |
answer = YES |
14 |
Correct |
1 ms |
320 KB |
answer = YES |
15 |
Correct |
0 ms |
212 KB |
answer = YES |
16 |
Correct |
1 ms |
320 KB |
answer = YES |
17 |
Correct |
1 ms |
212 KB |
answer = YES |
18 |
Correct |
1 ms |
320 KB |
answer = YES |
19 |
Correct |
1 ms |
316 KB |
answer = YES |
20 |
Correct |
0 ms |
316 KB |
answer = YES |
21 |
Correct |
1 ms |
212 KB |
answer = YES |
22 |
Correct |
1 ms |
212 KB |
answer = NO |
23 |
Correct |
1 ms |
212 KB |
answer = NO |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
answer = YES |
2 |
Correct |
0 ms |
212 KB |
answer = YES |
3 |
Correct |
1 ms |
324 KB |
answer = YES |
4 |
Correct |
1 ms |
212 KB |
answer = NO |
5 |
Correct |
0 ms |
212 KB |
answer = YES |
6 |
Correct |
0 ms |
212 KB |
answer = YES |
7 |
Correct |
0 ms |
320 KB |
answer = YES |
8 |
Correct |
0 ms |
212 KB |
answer = YES |
9 |
Correct |
1 ms |
316 KB |
answer = NO |
10 |
Correct |
0 ms |
320 KB |
answer = YES |
11 |
Correct |
1 ms |
212 KB |
answer = YES |
12 |
Correct |
1 ms |
212 KB |
answer = NO |
13 |
Correct |
1 ms |
324 KB |
answer = YES |
14 |
Correct |
1 ms |
320 KB |
answer = YES |
15 |
Correct |
0 ms |
212 KB |
answer = YES |
16 |
Correct |
1 ms |
320 KB |
answer = YES |
17 |
Correct |
1 ms |
212 KB |
answer = YES |
18 |
Correct |
1 ms |
320 KB |
answer = YES |
19 |
Correct |
1 ms |
316 KB |
answer = YES |
20 |
Correct |
0 ms |
316 KB |
answer = YES |
21 |
Correct |
1 ms |
212 KB |
answer = YES |
22 |
Correct |
1 ms |
212 KB |
answer = NO |
23 |
Correct |
1 ms |
212 KB |
answer = NO |
24 |
Correct |
1 ms |
212 KB |
answer = YES |
25 |
Correct |
1 ms |
212 KB |
answer = YES |
26 |
Correct |
1 ms |
212 KB |
answer = YES |
27 |
Correct |
1 ms |
320 KB |
answer = YES |
28 |
Correct |
1 ms |
212 KB |
answer = YES |
29 |
Correct |
1 ms |
316 KB |
answer = YES |
30 |
Correct |
1 ms |
212 KB |
answer = NO |
31 |
Correct |
1 ms |
212 KB |
answer = YES |
32 |
Correct |
1 ms |
212 KB |
answer = YES |
33 |
Correct |
1 ms |
212 KB |
answer = YES |
34 |
Correct |
1 ms |
212 KB |
answer = YES |
35 |
Correct |
1 ms |
316 KB |
answer = YES |
36 |
Correct |
1 ms |
212 KB |
answer = YES |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
answer = YES |
2 |
Correct |
0 ms |
212 KB |
answer = YES |
3 |
Correct |
1 ms |
324 KB |
answer = YES |
4 |
Correct |
1 ms |
212 KB |
answer = NO |
5 |
Correct |
0 ms |
212 KB |
answer = YES |
6 |
Correct |
0 ms |
212 KB |
answer = YES |
7 |
Correct |
0 ms |
320 KB |
answer = YES |
8 |
Correct |
0 ms |
212 KB |
answer = YES |
9 |
Correct |
1 ms |
316 KB |
answer = NO |
10 |
Correct |
0 ms |
320 KB |
answer = YES |
11 |
Correct |
1 ms |
212 KB |
answer = YES |
12 |
Correct |
1 ms |
212 KB |
answer = NO |
13 |
Correct |
1 ms |
324 KB |
answer = YES |
14 |
Correct |
1 ms |
320 KB |
answer = YES |
15 |
Correct |
0 ms |
212 KB |
answer = YES |
16 |
Correct |
1 ms |
320 KB |
answer = YES |
17 |
Correct |
1 ms |
212 KB |
answer = YES |
18 |
Correct |
1 ms |
320 KB |
answer = YES |
19 |
Correct |
1 ms |
316 KB |
answer = YES |
20 |
Correct |
0 ms |
316 KB |
answer = YES |
21 |
Correct |
1 ms |
212 KB |
answer = YES |
22 |
Correct |
1 ms |
212 KB |
answer = NO |
23 |
Correct |
1 ms |
212 KB |
answer = NO |
24 |
Correct |
1 ms |
212 KB |
answer = YES |
25 |
Correct |
1 ms |
212 KB |
answer = YES |
26 |
Correct |
1 ms |
212 KB |
answer = YES |
27 |
Correct |
1 ms |
320 KB |
answer = YES |
28 |
Correct |
1 ms |
212 KB |
answer = YES |
29 |
Correct |
1 ms |
316 KB |
answer = YES |
30 |
Correct |
1 ms |
212 KB |
answer = NO |
31 |
Correct |
1 ms |
212 KB |
answer = YES |
32 |
Correct |
1 ms |
212 KB |
answer = YES |
33 |
Correct |
1 ms |
212 KB |
answer = YES |
34 |
Correct |
1 ms |
212 KB |
answer = YES |
35 |
Correct |
1 ms |
316 KB |
answer = YES |
36 |
Correct |
1 ms |
212 KB |
answer = YES |
37 |
Correct |
1 ms |
212 KB |
answer = YES |
38 |
Correct |
1 ms |
212 KB |
answer = YES |
39 |
Correct |
1 ms |
340 KB |
answer = YES |
40 |
Correct |
1 ms |
340 KB |
answer = YES |
41 |
Correct |
1 ms |
340 KB |
answer = NO |
42 |
Correct |
1 ms |
340 KB |
answer = YES |
43 |
Correct |
1 ms |
340 KB |
answer = YES |
44 |
Correct |
1 ms |
340 KB |
answer = YES |
45 |
Correct |
1 ms |
340 KB |
answer = YES |
46 |
Correct |
1 ms |
340 KB |
answer = YES |
47 |
Correct |
1 ms |
340 KB |
answer = YES |
48 |
Correct |
1 ms |
340 KB |
answer = YES |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
answer = YES |
2 |
Correct |
0 ms |
212 KB |
answer = YES |
3 |
Correct |
1 ms |
324 KB |
answer = YES |
4 |
Correct |
1 ms |
212 KB |
answer = NO |
5 |
Correct |
0 ms |
212 KB |
answer = YES |
6 |
Correct |
0 ms |
212 KB |
answer = YES |
7 |
Correct |
0 ms |
320 KB |
answer = YES |
8 |
Correct |
0 ms |
212 KB |
answer = YES |
9 |
Correct |
1 ms |
316 KB |
answer = NO |
10 |
Correct |
0 ms |
320 KB |
answer = YES |
11 |
Correct |
1 ms |
212 KB |
answer = YES |
12 |
Correct |
1 ms |
212 KB |
answer = NO |
13 |
Correct |
1 ms |
324 KB |
answer = YES |
14 |
Correct |
1 ms |
320 KB |
answer = YES |
15 |
Correct |
0 ms |
212 KB |
answer = YES |
16 |
Correct |
1 ms |
320 KB |
answer = YES |
17 |
Correct |
1 ms |
212 KB |
answer = YES |
18 |
Correct |
1 ms |
320 KB |
answer = YES |
19 |
Correct |
1 ms |
316 KB |
answer = YES |
20 |
Correct |
0 ms |
316 KB |
answer = YES |
21 |
Correct |
1 ms |
212 KB |
answer = YES |
22 |
Correct |
1 ms |
212 KB |
answer = NO |
23 |
Correct |
1 ms |
212 KB |
answer = NO |
24 |
Correct |
1 ms |
212 KB |
answer = YES |
25 |
Correct |
1 ms |
212 KB |
answer = YES |
26 |
Correct |
1 ms |
212 KB |
answer = YES |
27 |
Correct |
1 ms |
320 KB |
answer = YES |
28 |
Correct |
1 ms |
212 KB |
answer = YES |
29 |
Correct |
1 ms |
316 KB |
answer = YES |
30 |
Correct |
1 ms |
212 KB |
answer = NO |
31 |
Correct |
1 ms |
212 KB |
answer = YES |
32 |
Correct |
1 ms |
212 KB |
answer = YES |
33 |
Correct |
1 ms |
212 KB |
answer = YES |
34 |
Correct |
1 ms |
212 KB |
answer = YES |
35 |
Correct |
1 ms |
316 KB |
answer = YES |
36 |
Correct |
1 ms |
212 KB |
answer = YES |
37 |
Correct |
1 ms |
212 KB |
answer = YES |
38 |
Correct |
1 ms |
212 KB |
answer = YES |
39 |
Correct |
1 ms |
340 KB |
answer = YES |
40 |
Correct |
1 ms |
340 KB |
answer = YES |
41 |
Correct |
1 ms |
340 KB |
answer = NO |
42 |
Correct |
1 ms |
340 KB |
answer = YES |
43 |
Correct |
1 ms |
340 KB |
answer = YES |
44 |
Correct |
1 ms |
340 KB |
answer = YES |
45 |
Correct |
1 ms |
340 KB |
answer = YES |
46 |
Correct |
1 ms |
340 KB |
answer = YES |
47 |
Correct |
1 ms |
340 KB |
answer = YES |
48 |
Correct |
1 ms |
340 KB |
answer = YES |
49 |
Correct |
7 ms |
1356 KB |
answer = YES |
50 |
Correct |
7 ms |
1492 KB |
answer = YES |
51 |
Correct |
7 ms |
1588 KB |
answer = YES |
52 |
Correct |
3 ms |
1236 KB |
answer = NO |
53 |
Correct |
1 ms |
340 KB |
answer = YES |
54 |
Correct |
2 ms |
596 KB |
answer = YES |
55 |
Correct |
4 ms |
852 KB |
answer = YES |
56 |
Correct |
7 ms |
1364 KB |
answer = YES |
57 |
Correct |
7 ms |
1236 KB |
answer = YES |
58 |
Correct |
6 ms |
1312 KB |
answer = YES |
59 |
Correct |
6 ms |
1236 KB |
answer = YES |
60 |
Correct |
7 ms |
1364 KB |
answer = YES |
61 |
Correct |
3 ms |
852 KB |
answer = YES |
62 |
Correct |
40 ms |
12084 KB |
answer = NO |
63 |
Correct |
51 ms |
12384 KB |
answer = YES |
64 |
Correct |
41 ms |
12336 KB |
answer = NO |
65 |
Correct |
65 ms |
12376 KB |
answer = YES |
66 |
Correct |
2 ms |
468 KB |
answer = YES |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
answer = YES |
2 |
Correct |
0 ms |
212 KB |
answer = YES |
3 |
Correct |
1 ms |
324 KB |
answer = YES |
4 |
Correct |
1 ms |
212 KB |
answer = NO |
5 |
Correct |
0 ms |
212 KB |
answer = YES |
6 |
Correct |
0 ms |
212 KB |
answer = YES |
7 |
Correct |
0 ms |
320 KB |
answer = YES |
8 |
Correct |
0 ms |
212 KB |
answer = YES |
9 |
Correct |
1 ms |
316 KB |
answer = NO |
10 |
Correct |
0 ms |
320 KB |
answer = YES |
11 |
Correct |
1 ms |
212 KB |
answer = YES |
12 |
Correct |
1 ms |
212 KB |
answer = NO |
13 |
Correct |
1 ms |
324 KB |
answer = YES |
14 |
Correct |
1 ms |
320 KB |
answer = YES |
15 |
Correct |
0 ms |
212 KB |
answer = YES |
16 |
Correct |
1 ms |
320 KB |
answer = YES |
17 |
Correct |
1 ms |
212 KB |
answer = YES |
18 |
Correct |
1 ms |
320 KB |
answer = YES |
19 |
Correct |
1 ms |
316 KB |
answer = YES |
20 |
Correct |
0 ms |
316 KB |
answer = YES |
21 |
Correct |
1 ms |
212 KB |
answer = YES |
22 |
Correct |
1 ms |
212 KB |
answer = NO |
23 |
Correct |
1 ms |
212 KB |
answer = NO |
24 |
Correct |
1 ms |
212 KB |
answer = YES |
25 |
Correct |
1 ms |
212 KB |
answer = YES |
26 |
Correct |
1 ms |
212 KB |
answer = YES |
27 |
Correct |
1 ms |
320 KB |
answer = YES |
28 |
Correct |
1 ms |
212 KB |
answer = YES |
29 |
Correct |
1 ms |
316 KB |
answer = YES |
30 |
Correct |
1 ms |
212 KB |
answer = NO |
31 |
Correct |
1 ms |
212 KB |
answer = YES |
32 |
Correct |
1 ms |
212 KB |
answer = YES |
33 |
Correct |
1 ms |
212 KB |
answer = YES |
34 |
Correct |
1 ms |
212 KB |
answer = YES |
35 |
Correct |
1 ms |
316 KB |
answer = YES |
36 |
Correct |
1 ms |
212 KB |
answer = YES |
37 |
Correct |
1 ms |
212 KB |
answer = YES |
38 |
Correct |
1 ms |
212 KB |
answer = YES |
39 |
Correct |
1 ms |
340 KB |
answer = YES |
40 |
Correct |
1 ms |
340 KB |
answer = YES |
41 |
Correct |
1 ms |
340 KB |
answer = NO |
42 |
Correct |
1 ms |
340 KB |
answer = YES |
43 |
Correct |
1 ms |
340 KB |
answer = YES |
44 |
Correct |
1 ms |
340 KB |
answer = YES |
45 |
Correct |
1 ms |
340 KB |
answer = YES |
46 |
Correct |
1 ms |
340 KB |
answer = YES |
47 |
Correct |
1 ms |
340 KB |
answer = YES |
48 |
Correct |
1 ms |
340 KB |
answer = YES |
49 |
Correct |
7 ms |
1356 KB |
answer = YES |
50 |
Correct |
7 ms |
1492 KB |
answer = YES |
51 |
Correct |
7 ms |
1588 KB |
answer = YES |
52 |
Correct |
3 ms |
1236 KB |
answer = NO |
53 |
Correct |
1 ms |
340 KB |
answer = YES |
54 |
Correct |
2 ms |
596 KB |
answer = YES |
55 |
Correct |
4 ms |
852 KB |
answer = YES |
56 |
Correct |
7 ms |
1364 KB |
answer = YES |
57 |
Correct |
7 ms |
1236 KB |
answer = YES |
58 |
Correct |
6 ms |
1312 KB |
answer = YES |
59 |
Correct |
6 ms |
1236 KB |
answer = YES |
60 |
Correct |
7 ms |
1364 KB |
answer = YES |
61 |
Correct |
3 ms |
852 KB |
answer = YES |
62 |
Correct |
40 ms |
12084 KB |
answer = NO |
63 |
Correct |
51 ms |
12384 KB |
answer = YES |
64 |
Correct |
41 ms |
12336 KB |
answer = NO |
65 |
Correct |
65 ms |
12376 KB |
answer = YES |
66 |
Correct |
2 ms |
468 KB |
answer = YES |
67 |
Correct |
66 ms |
14788 KB |
answer = YES |
68 |
Correct |
69 ms |
14780 KB |
answer = YES |
69 |
Correct |
60 ms |
14336 KB |
answer = YES |
70 |
Correct |
90 ms |
22004 KB |
answer = YES |
71 |
Correct |
69 ms |
14572 KB |
answer = YES |
72 |
Correct |
70 ms |
11340 KB |
answer = YES |
73 |
Correct |
68 ms |
10836 KB |
answer = YES |
74 |
Correct |
43 ms |
8980 KB |
answer = YES |
75 |
Correct |
17 ms |
7764 KB |
answer = NO |
76 |
Correct |
9 ms |
1620 KB |
answer = YES |
77 |
Correct |
17 ms |
3028 KB |
answer = YES |
78 |
Correct |
28 ms |
5076 KB |
answer = YES |
79 |
Correct |
62 ms |
9820 KB |
answer = YES |
80 |
Correct |
46 ms |
9128 KB |
answer = YES |
81 |
Correct |
29 ms |
12212 KB |
answer = NO |
82 |
Correct |
89 ms |
14372 KB |
answer = YES |
83 |
Correct |
88 ms |
15084 KB |
answer = YES |
84 |
Correct |
86 ms |
15304 KB |
answer = YES |
85 |
Correct |
81 ms |
14716 KB |
answer = YES |
86 |
Correct |
62 ms |
14460 KB |
answer = YES |
87 |
Correct |
32 ms |
11872 KB |
answer = NO |
88 |
Correct |
108 ms |
13028 KB |
answer = YES |
89 |
Correct |
68 ms |
10500 KB |
answer = YES |
90 |
Correct |
68 ms |
10572 KB |
answer = YES |
91 |
Correct |
67 ms |
11080 KB |
answer = YES |
92 |
Correct |
33 ms |
5672 KB |
answer = YES |
93 |
Correct |
32 ms |
5520 KB |
answer = YES |
94 |
Correct |
52 ms |
14668 KB |
answer = NO |
95 |
Correct |
28 ms |
10828 KB |
answer = NO |
96 |
Correct |
121 ms |
19996 KB |
answer = YES |
97 |
Correct |
27 ms |
14156 KB |
answer = NO |