#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
/*
observatii:
tot sistemul se poate scrie pe o componenta conexa in functie de o singura variabila
deci toate nodurile se vor putea scrie ca functii f(x) = ax + b
cu a din {-1, 0, 1} si b din R
atunci cand un nod are doua functii distince asociate, se poate identifica x / o contradictie
daca sistemul este (simplu) nedeterminat, trebuie aflat x pt care suma de abs(ax + b) este minima
conditia se pune ca sum(abs(x - (-b/a))) minima
functia atinge minimul pt x mediana (-b/a)
se considera (-b/a)_k <= x <= x + d <= (-b/a)_(k + 1)
si se studiaza monotonia functiei pe intervale
*/
struct node {
int index, type;
node(int a = 0, int t = 0) : index(a), type(t) {}
};
struct function {
double a, b;
function(double x = 1, double y = 0) : a(x), b(y) {}
};
const int nmax = 1e5;
vector<node> edges[nmax + 1];
function nodes[nmax + 1];
bool processed[nmax + 1];
bool not_isolated[nmax + 1];
queue<int> dfs_processed;
bool dfs(int curr_node = 1) {
processed[curr_node] = 1;
dfs_processed.push(curr_node);
for (node next : edges[curr_node]) {
double curr_a = -nodes[curr_node].a,
curr_b = next.type - nodes[curr_node].b;
if (!processed[next.index]) {
nodes[next.index].a = curr_a;
nodes[next.index].b = curr_b;
if (!dfs(next.index))
return false;
}
else {
if (nodes[next.index].a == curr_a) {
if (nodes[next.index].b != curr_b)
return false;
}
else {
double x = (nodes[next.index].b - curr_b) / (curr_a - nodes[next.index].a);
while (!dfs_processed.empty()) {
int top = dfs_processed.front();
dfs_processed.pop();
nodes[top].b = nodes[top].a * x + nodes[top].b;
nodes[top].a = 0;
}
}
}
}
return true;
}
bool cmp(const int& x, const int& y) {
return (-1 * nodes[x].b / nodes[x].a) < (-1 * nodes[y].b / nodes[y].a);
}
int main() {
int n, m;
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int a, b, t;
cin >> a >> b >> t;
not_isolated[a] = not_isolated[b] = 1;
if (a == b) {
double curr_b = (double)t / 2;
if (processed[a] && nodes[a].b != curr_b) {
cout << "NO";
return 0;
}
nodes[a].a = 0;
nodes[a].b = curr_b;
processed[a] = 1;
}
bool skip = false;
for (node next : edges[a]) {
if (next.index == b) {
skip = true;
if (next.type != t) {
cout << "NO";
return 0;
}
break;
}
}
if (skip)
continue;
edges[a].push_back(node(b, t));
edges[b].push_back(node(a, t));
}
for (int i = 1; i <= n; i++) {
if (processed[i])
continue;
if (!not_isolated[i]) {
processed[i] = 1;
nodes[i].a = nodes[i].b = 0;
continue;
}
if (!dfs(i)) {
cout << "NO";
return 0;
}
vector<int> to_sort;
while (!dfs_processed.empty()) {
int top = dfs_processed.front();
dfs_processed.pop();
if (nodes[top].a)
to_sort.push_back(top);
}
sort(to_sort.begin(), to_sort.end(), cmp);
if (!to_sort.empty()) {
int median = (to_sort.size() + 1) / 2;
double x = -1 * nodes[to_sort[median - 1]].b / nodes[to_sort[median - 1]].a;
for (int elem : to_sort) {
nodes[elem].b = nodes[elem].a * x + nodes[elem].b;
nodes[elem].a = 0;
}
}
}
cout << "YES\n";
for (int i = 1; i <= n; i++)
cout << nodes[i].b << " ";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4184 KB |
answer = YES |
2 |
Correct |
2 ms |
4184 KB |
answer = YES |
3 |
Correct |
2 ms |
4188 KB |
answer = YES |
4 |
Correct |
2 ms |
4188 KB |
answer = NO |
5 |
Correct |
3 ms |
4188 KB |
answer = YES |
6 |
Correct |
2 ms |
4376 KB |
answer = YES |
7 |
Correct |
3 ms |
4188 KB |
answer = YES |
8 |
Correct |
2 ms |
4188 KB |
answer = YES |
9 |
Correct |
2 ms |
4188 KB |
answer = NO |
10 |
Correct |
2 ms |
4188 KB |
answer = YES |
11 |
Correct |
2 ms |
4188 KB |
answer = YES |
12 |
Correct |
2 ms |
4188 KB |
answer = NO |
13 |
Correct |
2 ms |
4188 KB |
answer = YES |
14 |
Correct |
2 ms |
4376 KB |
answer = YES |
15 |
Correct |
2 ms |
4184 KB |
answer = YES |
16 |
Correct |
2 ms |
4188 KB |
answer = YES |
17 |
Correct |
2 ms |
4188 KB |
answer = YES |
18 |
Correct |
2 ms |
4188 KB |
answer = YES |
19 |
Correct |
2 ms |
4188 KB |
answer = YES |
20 |
Correct |
2 ms |
4188 KB |
answer = YES |
21 |
Correct |
2 ms |
4188 KB |
answer = YES |
22 |
Correct |
1 ms |
4188 KB |
answer = NO |
23 |
Correct |
2 ms |
4188 KB |
answer = NO |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4184 KB |
answer = YES |
2 |
Correct |
2 ms |
4184 KB |
answer = YES |
3 |
Correct |
2 ms |
4188 KB |
answer = YES |
4 |
Correct |
2 ms |
4188 KB |
answer = NO |
5 |
Correct |
3 ms |
4188 KB |
answer = YES |
6 |
Correct |
2 ms |
4376 KB |
answer = YES |
7 |
Correct |
3 ms |
4188 KB |
answer = YES |
8 |
Correct |
2 ms |
4188 KB |
answer = YES |
9 |
Correct |
2 ms |
4188 KB |
answer = NO |
10 |
Correct |
2 ms |
4188 KB |
answer = YES |
11 |
Correct |
2 ms |
4188 KB |
answer = YES |
12 |
Correct |
2 ms |
4188 KB |
answer = NO |
13 |
Correct |
2 ms |
4188 KB |
answer = YES |
14 |
Correct |
2 ms |
4376 KB |
answer = YES |
15 |
Correct |
2 ms |
4184 KB |
answer = YES |
16 |
Correct |
2 ms |
4188 KB |
answer = YES |
17 |
Correct |
2 ms |
4188 KB |
answer = YES |
18 |
Correct |
2 ms |
4188 KB |
answer = YES |
19 |
Correct |
2 ms |
4188 KB |
answer = YES |
20 |
Correct |
2 ms |
4188 KB |
answer = YES |
21 |
Correct |
2 ms |
4188 KB |
answer = YES |
22 |
Correct |
1 ms |
4188 KB |
answer = NO |
23 |
Correct |
2 ms |
4188 KB |
answer = NO |
24 |
Correct |
2 ms |
4188 KB |
answer = YES |
25 |
Correct |
2 ms |
4184 KB |
answer = YES |
26 |
Correct |
2 ms |
4188 KB |
answer = YES |
27 |
Correct |
3 ms |
4188 KB |
answer = YES |
28 |
Correct |
2 ms |
4188 KB |
answer = YES |
29 |
Correct |
2 ms |
4188 KB |
answer = YES |
30 |
Correct |
2 ms |
4188 KB |
answer = NO |
31 |
Correct |
2 ms |
4184 KB |
answer = YES |
32 |
Correct |
2 ms |
4188 KB |
answer = YES |
33 |
Correct |
2 ms |
4188 KB |
answer = YES |
34 |
Correct |
2 ms |
4188 KB |
answer = YES |
35 |
Correct |
2 ms |
4188 KB |
answer = YES |
36 |
Correct |
2 ms |
4372 KB |
answer = YES |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4184 KB |
answer = YES |
2 |
Correct |
2 ms |
4184 KB |
answer = YES |
3 |
Correct |
2 ms |
4188 KB |
answer = YES |
4 |
Correct |
2 ms |
4188 KB |
answer = NO |
5 |
Correct |
3 ms |
4188 KB |
answer = YES |
6 |
Correct |
2 ms |
4376 KB |
answer = YES |
7 |
Correct |
3 ms |
4188 KB |
answer = YES |
8 |
Correct |
2 ms |
4188 KB |
answer = YES |
9 |
Correct |
2 ms |
4188 KB |
answer = NO |
10 |
Correct |
2 ms |
4188 KB |
answer = YES |
11 |
Correct |
2 ms |
4188 KB |
answer = YES |
12 |
Correct |
2 ms |
4188 KB |
answer = NO |
13 |
Correct |
2 ms |
4188 KB |
answer = YES |
14 |
Correct |
2 ms |
4376 KB |
answer = YES |
15 |
Correct |
2 ms |
4184 KB |
answer = YES |
16 |
Correct |
2 ms |
4188 KB |
answer = YES |
17 |
Correct |
2 ms |
4188 KB |
answer = YES |
18 |
Correct |
2 ms |
4188 KB |
answer = YES |
19 |
Correct |
2 ms |
4188 KB |
answer = YES |
20 |
Correct |
2 ms |
4188 KB |
answer = YES |
21 |
Correct |
2 ms |
4188 KB |
answer = YES |
22 |
Correct |
1 ms |
4188 KB |
answer = NO |
23 |
Correct |
2 ms |
4188 KB |
answer = NO |
24 |
Correct |
2 ms |
4188 KB |
answer = YES |
25 |
Correct |
2 ms |
4184 KB |
answer = YES |
26 |
Correct |
2 ms |
4188 KB |
answer = YES |
27 |
Correct |
3 ms |
4188 KB |
answer = YES |
28 |
Correct |
2 ms |
4188 KB |
answer = YES |
29 |
Correct |
2 ms |
4188 KB |
answer = YES |
30 |
Correct |
2 ms |
4188 KB |
answer = NO |
31 |
Correct |
2 ms |
4184 KB |
answer = YES |
32 |
Correct |
2 ms |
4188 KB |
answer = YES |
33 |
Correct |
2 ms |
4188 KB |
answer = YES |
34 |
Correct |
2 ms |
4188 KB |
answer = YES |
35 |
Correct |
2 ms |
4188 KB |
answer = YES |
36 |
Correct |
2 ms |
4372 KB |
answer = YES |
37 |
Correct |
2 ms |
4184 KB |
answer = YES |
38 |
Correct |
2 ms |
4184 KB |
answer = YES |
39 |
Correct |
2 ms |
4188 KB |
answer = YES |
40 |
Correct |
2 ms |
4444 KB |
answer = YES |
41 |
Correct |
3 ms |
4444 KB |
answer = NO |
42 |
Correct |
3 ms |
4444 KB |
answer = YES |
43 |
Correct |
3 ms |
4188 KB |
answer = YES |
44 |
Correct |
3 ms |
4188 KB |
answer = YES |
45 |
Correct |
3 ms |
4188 KB |
answer = YES |
46 |
Correct |
2 ms |
4188 KB |
answer = YES |
47 |
Correct |
3 ms |
4188 KB |
answer = YES |
48 |
Correct |
3 ms |
4444 KB |
answer = YES |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4184 KB |
answer = YES |
2 |
Correct |
2 ms |
4184 KB |
answer = YES |
3 |
Correct |
2 ms |
4188 KB |
answer = YES |
4 |
Correct |
2 ms |
4188 KB |
answer = NO |
5 |
Correct |
3 ms |
4188 KB |
answer = YES |
6 |
Correct |
2 ms |
4376 KB |
answer = YES |
7 |
Correct |
3 ms |
4188 KB |
answer = YES |
8 |
Correct |
2 ms |
4188 KB |
answer = YES |
9 |
Correct |
2 ms |
4188 KB |
answer = NO |
10 |
Correct |
2 ms |
4188 KB |
answer = YES |
11 |
Correct |
2 ms |
4188 KB |
answer = YES |
12 |
Correct |
2 ms |
4188 KB |
answer = NO |
13 |
Correct |
2 ms |
4188 KB |
answer = YES |
14 |
Correct |
2 ms |
4376 KB |
answer = YES |
15 |
Correct |
2 ms |
4184 KB |
answer = YES |
16 |
Correct |
2 ms |
4188 KB |
answer = YES |
17 |
Correct |
2 ms |
4188 KB |
answer = YES |
18 |
Correct |
2 ms |
4188 KB |
answer = YES |
19 |
Correct |
2 ms |
4188 KB |
answer = YES |
20 |
Correct |
2 ms |
4188 KB |
answer = YES |
21 |
Correct |
2 ms |
4188 KB |
answer = YES |
22 |
Correct |
1 ms |
4188 KB |
answer = NO |
23 |
Correct |
2 ms |
4188 KB |
answer = NO |
24 |
Correct |
2 ms |
4188 KB |
answer = YES |
25 |
Correct |
2 ms |
4184 KB |
answer = YES |
26 |
Correct |
2 ms |
4188 KB |
answer = YES |
27 |
Correct |
3 ms |
4188 KB |
answer = YES |
28 |
Correct |
2 ms |
4188 KB |
answer = YES |
29 |
Correct |
2 ms |
4188 KB |
answer = YES |
30 |
Correct |
2 ms |
4188 KB |
answer = NO |
31 |
Correct |
2 ms |
4184 KB |
answer = YES |
32 |
Correct |
2 ms |
4188 KB |
answer = YES |
33 |
Correct |
2 ms |
4188 KB |
answer = YES |
34 |
Correct |
2 ms |
4188 KB |
answer = YES |
35 |
Correct |
2 ms |
4188 KB |
answer = YES |
36 |
Correct |
2 ms |
4372 KB |
answer = YES |
37 |
Correct |
2 ms |
4184 KB |
answer = YES |
38 |
Correct |
2 ms |
4184 KB |
answer = YES |
39 |
Correct |
2 ms |
4188 KB |
answer = YES |
40 |
Correct |
2 ms |
4444 KB |
answer = YES |
41 |
Correct |
3 ms |
4444 KB |
answer = NO |
42 |
Correct |
3 ms |
4444 KB |
answer = YES |
43 |
Correct |
3 ms |
4188 KB |
answer = YES |
44 |
Correct |
3 ms |
4188 KB |
answer = YES |
45 |
Correct |
3 ms |
4188 KB |
answer = YES |
46 |
Correct |
2 ms |
4188 KB |
answer = YES |
47 |
Correct |
3 ms |
4188 KB |
answer = YES |
48 |
Correct |
3 ms |
4444 KB |
answer = YES |
49 |
Correct |
13 ms |
4956 KB |
answer = YES |
50 |
Correct |
11 ms |
5212 KB |
answer = YES |
51 |
Correct |
12 ms |
5212 KB |
answer = YES |
52 |
Correct |
7 ms |
4700 KB |
answer = NO |
53 |
Correct |
3 ms |
4188 KB |
answer = YES |
54 |
Correct |
4 ms |
4440 KB |
answer = YES |
55 |
Correct |
7 ms |
4696 KB |
answer = YES |
56 |
Correct |
15 ms |
5044 KB |
answer = YES |
57 |
Correct |
11 ms |
4700 KB |
answer = YES |
58 |
Correct |
11 ms |
4792 KB |
answer = YES |
59 |
Correct |
10 ms |
4696 KB |
answer = YES |
60 |
Correct |
11 ms |
4952 KB |
answer = YES |
61 |
Correct |
6 ms |
4444 KB |
answer = YES |
62 |
Correct |
9 ms |
5120 KB |
answer = NO |
63 |
Correct |
128 ms |
11948 KB |
answer = YES |
64 |
Correct |
105 ms |
11700 KB |
answer = NO |
65 |
Correct |
117 ms |
11736 KB |
answer = YES |
66 |
Correct |
4 ms |
4440 KB |
answer = YES |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4184 KB |
answer = YES |
2 |
Correct |
2 ms |
4184 KB |
answer = YES |
3 |
Correct |
2 ms |
4188 KB |
answer = YES |
4 |
Correct |
2 ms |
4188 KB |
answer = NO |
5 |
Correct |
3 ms |
4188 KB |
answer = YES |
6 |
Correct |
2 ms |
4376 KB |
answer = YES |
7 |
Correct |
3 ms |
4188 KB |
answer = YES |
8 |
Correct |
2 ms |
4188 KB |
answer = YES |
9 |
Correct |
2 ms |
4188 KB |
answer = NO |
10 |
Correct |
2 ms |
4188 KB |
answer = YES |
11 |
Correct |
2 ms |
4188 KB |
answer = YES |
12 |
Correct |
2 ms |
4188 KB |
answer = NO |
13 |
Correct |
2 ms |
4188 KB |
answer = YES |
14 |
Correct |
2 ms |
4376 KB |
answer = YES |
15 |
Correct |
2 ms |
4184 KB |
answer = YES |
16 |
Correct |
2 ms |
4188 KB |
answer = YES |
17 |
Correct |
2 ms |
4188 KB |
answer = YES |
18 |
Correct |
2 ms |
4188 KB |
answer = YES |
19 |
Correct |
2 ms |
4188 KB |
answer = YES |
20 |
Correct |
2 ms |
4188 KB |
answer = YES |
21 |
Correct |
2 ms |
4188 KB |
answer = YES |
22 |
Correct |
1 ms |
4188 KB |
answer = NO |
23 |
Correct |
2 ms |
4188 KB |
answer = NO |
24 |
Correct |
2 ms |
4188 KB |
answer = YES |
25 |
Correct |
2 ms |
4184 KB |
answer = YES |
26 |
Correct |
2 ms |
4188 KB |
answer = YES |
27 |
Correct |
3 ms |
4188 KB |
answer = YES |
28 |
Correct |
2 ms |
4188 KB |
answer = YES |
29 |
Correct |
2 ms |
4188 KB |
answer = YES |
30 |
Correct |
2 ms |
4188 KB |
answer = NO |
31 |
Correct |
2 ms |
4184 KB |
answer = YES |
32 |
Correct |
2 ms |
4188 KB |
answer = YES |
33 |
Correct |
2 ms |
4188 KB |
answer = YES |
34 |
Correct |
2 ms |
4188 KB |
answer = YES |
35 |
Correct |
2 ms |
4188 KB |
answer = YES |
36 |
Correct |
2 ms |
4372 KB |
answer = YES |
37 |
Correct |
2 ms |
4184 KB |
answer = YES |
38 |
Correct |
2 ms |
4184 KB |
answer = YES |
39 |
Correct |
2 ms |
4188 KB |
answer = YES |
40 |
Correct |
2 ms |
4444 KB |
answer = YES |
41 |
Correct |
3 ms |
4444 KB |
answer = NO |
42 |
Correct |
3 ms |
4444 KB |
answer = YES |
43 |
Correct |
3 ms |
4188 KB |
answer = YES |
44 |
Correct |
3 ms |
4188 KB |
answer = YES |
45 |
Correct |
3 ms |
4188 KB |
answer = YES |
46 |
Correct |
2 ms |
4188 KB |
answer = YES |
47 |
Correct |
3 ms |
4188 KB |
answer = YES |
48 |
Correct |
3 ms |
4444 KB |
answer = YES |
49 |
Correct |
13 ms |
4956 KB |
answer = YES |
50 |
Correct |
11 ms |
5212 KB |
answer = YES |
51 |
Correct |
12 ms |
5212 KB |
answer = YES |
52 |
Correct |
7 ms |
4700 KB |
answer = NO |
53 |
Correct |
3 ms |
4188 KB |
answer = YES |
54 |
Correct |
4 ms |
4440 KB |
answer = YES |
55 |
Correct |
7 ms |
4696 KB |
answer = YES |
56 |
Correct |
15 ms |
5044 KB |
answer = YES |
57 |
Correct |
11 ms |
4700 KB |
answer = YES |
58 |
Correct |
11 ms |
4792 KB |
answer = YES |
59 |
Correct |
10 ms |
4696 KB |
answer = YES |
60 |
Correct |
11 ms |
4952 KB |
answer = YES |
61 |
Correct |
6 ms |
4444 KB |
answer = YES |
62 |
Correct |
9 ms |
5120 KB |
answer = NO |
63 |
Correct |
128 ms |
11948 KB |
answer = YES |
64 |
Correct |
105 ms |
11700 KB |
answer = NO |
65 |
Correct |
117 ms |
11736 KB |
answer = YES |
66 |
Correct |
4 ms |
4440 KB |
answer = YES |
67 |
Correct |
108 ms |
17684 KB |
answer = YES |
68 |
Correct |
106 ms |
17876 KB |
answer = YES |
69 |
Correct |
97 ms |
17308 KB |
answer = YES |
70 |
Correct |
164 ms |
22064 KB |
answer = YES |
71 |
Correct |
103 ms |
17488 KB |
answer = YES |
72 |
Correct |
115 ms |
10576 KB |
answer = YES |
73 |
Correct |
96 ms |
10056 KB |
answer = YES |
74 |
Correct |
65 ms |
11608 KB |
answer = YES |
75 |
Correct |
43 ms |
9300 KB |
answer = NO |
76 |
Correct |
13 ms |
4952 KB |
answer = YES |
77 |
Correct |
26 ms |
5980 KB |
answer = YES |
78 |
Correct |
45 ms |
7300 KB |
answer = YES |
79 |
Correct |
93 ms |
9680 KB |
answer = YES |
80 |
Correct |
73 ms |
12056 KB |
answer = YES |
81 |
Correct |
74 ms |
13196 KB |
answer = NO |
82 |
Correct |
118 ms |
16724 KB |
answer = YES |
83 |
Correct |
112 ms |
17528 KB |
answer = YES |
84 |
Correct |
124 ms |
17872 KB |
answer = YES |
85 |
Correct |
109 ms |
17872 KB |
answer = YES |
86 |
Correct |
99 ms |
17356 KB |
answer = YES |
87 |
Correct |
90 ms |
11168 KB |
answer = NO |
88 |
Correct |
114 ms |
12884 KB |
answer = YES |
89 |
Correct |
98 ms |
9300 KB |
answer = YES |
90 |
Correct |
99 ms |
9144 KB |
answer = YES |
91 |
Correct |
102 ms |
9096 KB |
answer = YES |
92 |
Correct |
54 ms |
7220 KB |
answer = YES |
93 |
Correct |
52 ms |
7156 KB |
answer = YES |
94 |
Correct |
86 ms |
17164 KB |
answer = NO |
95 |
Correct |
61 ms |
8984 KB |
answer = NO |
96 |
Correct |
189 ms |
18748 KB |
answer = YES |
97 |
Correct |
65 ms |
17156 KB |
answer = NO |