Submission #395347

# Submission time Handle Problem Language Result Execution time Memory
395347 2021-04-28T08:48:29 Z KoD Graph (BOI20_graph) C++17
100 / 100
345 ms 19868 KB
#include <bits/stdc++.h>

template <class T>
using Vec = std::vector<T>;

int main() {
    int N, M;
    std::cin >> N >> M;
    Vec<int> A(M), B(M), C(M);
    Vec<Vec<std::pair<int, int>>> graph(N);
    for (int i = 0; i < M; ++i) {
        std::cin >> A[i] >> B[i] >> C[i];
        A[i] -= 1;
        B[i] -= 1;
        C[i] *= 2;
        graph[A[i]].emplace_back(B[i], C[i]);
        graph[B[i]].emplace_back(A[i], C[i]);
    }
    Vec<bool> done(N);
    Vec<std::pair<int, int>> coeff(N);
    Vec<int> val(N);
    Vec<int> visited;
    auto dfs = [&](auto&& dfs, const int u) -> void {
        done[u] = true;
        visited.push_back(u);
        for (const auto [v, c]: graph[u]) {
            if (!done[v]) {
                coeff[v].first = -coeff[u].first;
                coeff[v].second = c - coeff[u].second;
                dfs(dfs, v);
            }
        }
    };
    for (int src = 0; src < N; ++src) {
        if (!done[src]) {
            coeff[src].first = 1;
            coeff[src].second = 0;
            dfs(dfs, src);
            bool sole = false;
            Vec<int> pts;
            for (const auto u: visited) {
                pts.push_back(-coeff[u].first * coeff[u].second);
                for (const auto [v, c]: graph[u]) {
                    if (coeff[u].first == coeff[v].first) {
                        val[src] = (c - coeff[u].second - coeff[v].second) / (coeff[u].first + coeff[v].first);
                        sole = true;
                    }
                }
            }
            if (!sole) {
                std::sort(pts.begin(), pts.end());
                val[src] = pts[pts.size() / 2];
            }
            for (const auto u: visited) {
                if (u != src) {
                    val[u] = val[src] * coeff[u].first + coeff[u].second;
                }
            }
            visited.clear();
        }
    }
    for (int i = 0; i < M; ++i) {
        if (val[A[i]] + val[B[i]] != C[i]) {
            std::cout << "NO\n";
            return 0;
        }
    }
    std::cout << "YES\n";
    for (int i = 0; i < N; ++i) {
        std::cout << (double) val[i] / 2 << " \n"[i + 1 == N];
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB answer = YES
2 Correct 1 ms 204 KB answer = YES
3 Correct 1 ms 204 KB answer = YES
4 Correct 1 ms 204 KB answer = NO
5 Correct 1 ms 204 KB answer = YES
6 Correct 1 ms 300 KB answer = YES
7 Correct 1 ms 296 KB answer = YES
8 Correct 1 ms 204 KB answer = YES
9 Correct 1 ms 204 KB answer = NO
10 Correct 1 ms 204 KB answer = YES
11 Correct 1 ms 296 KB answer = YES
12 Correct 1 ms 208 KB answer = NO
13 Correct 1 ms 296 KB answer = YES
14 Correct 1 ms 204 KB answer = YES
15 Correct 1 ms 204 KB answer = YES
16 Correct 1 ms 204 KB answer = YES
17 Correct 1 ms 296 KB answer = YES
18 Correct 1 ms 204 KB answer = YES
19 Correct 1 ms 296 KB answer = YES
20 Correct 1 ms 204 KB answer = YES
21 Correct 1 ms 204 KB answer = YES
22 Correct 1 ms 204 KB answer = NO
23 Correct 1 ms 204 KB answer = NO
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB answer = YES
2 Correct 1 ms 204 KB answer = YES
3 Correct 1 ms 204 KB answer = YES
4 Correct 1 ms 204 KB answer = NO
5 Correct 1 ms 204 KB answer = YES
6 Correct 1 ms 300 KB answer = YES
7 Correct 1 ms 296 KB answer = YES
8 Correct 1 ms 204 KB answer = YES
9 Correct 1 ms 204 KB answer = NO
10 Correct 1 ms 204 KB answer = YES
11 Correct 1 ms 296 KB answer = YES
12 Correct 1 ms 208 KB answer = NO
13 Correct 1 ms 296 KB answer = YES
14 Correct 1 ms 204 KB answer = YES
15 Correct 1 ms 204 KB answer = YES
16 Correct 1 ms 204 KB answer = YES
17 Correct 1 ms 296 KB answer = YES
18 Correct 1 ms 204 KB answer = YES
19 Correct 1 ms 296 KB answer = YES
20 Correct 1 ms 204 KB answer = YES
21 Correct 1 ms 204 KB answer = YES
22 Correct 1 ms 204 KB answer = NO
23 Correct 1 ms 204 KB answer = NO
24 Correct 1 ms 204 KB answer = YES
25 Correct 1 ms 204 KB answer = YES
26 Correct 1 ms 204 KB answer = YES
27 Correct 1 ms 292 KB answer = YES
28 Correct 1 ms 204 KB answer = YES
29 Correct 1 ms 204 KB answer = YES
30 Correct 1 ms 204 KB answer = NO
31 Correct 1 ms 204 KB answer = YES
32 Correct 1 ms 204 KB answer = YES
33 Correct 1 ms 204 KB answer = YES
34 Correct 1 ms 204 KB answer = YES
35 Correct 1 ms 204 KB answer = YES
36 Correct 1 ms 204 KB answer = YES
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB answer = YES
2 Correct 1 ms 204 KB answer = YES
3 Correct 1 ms 204 KB answer = YES
4 Correct 1 ms 204 KB answer = NO
5 Correct 1 ms 204 KB answer = YES
6 Correct 1 ms 300 KB answer = YES
7 Correct 1 ms 296 KB answer = YES
8 Correct 1 ms 204 KB answer = YES
9 Correct 1 ms 204 KB answer = NO
10 Correct 1 ms 204 KB answer = YES
11 Correct 1 ms 296 KB answer = YES
12 Correct 1 ms 208 KB answer = NO
13 Correct 1 ms 296 KB answer = YES
14 Correct 1 ms 204 KB answer = YES
15 Correct 1 ms 204 KB answer = YES
16 Correct 1 ms 204 KB answer = YES
17 Correct 1 ms 296 KB answer = YES
18 Correct 1 ms 204 KB answer = YES
19 Correct 1 ms 296 KB answer = YES
20 Correct 1 ms 204 KB answer = YES
21 Correct 1 ms 204 KB answer = YES
22 Correct 1 ms 204 KB answer = NO
23 Correct 1 ms 204 KB answer = NO
24 Correct 1 ms 204 KB answer = YES
25 Correct 1 ms 204 KB answer = YES
26 Correct 1 ms 204 KB answer = YES
27 Correct 1 ms 292 KB answer = YES
28 Correct 1 ms 204 KB answer = YES
29 Correct 1 ms 204 KB answer = YES
30 Correct 1 ms 204 KB answer = NO
31 Correct 1 ms 204 KB answer = YES
32 Correct 1 ms 204 KB answer = YES
33 Correct 1 ms 204 KB answer = YES
34 Correct 1 ms 204 KB answer = YES
35 Correct 1 ms 204 KB answer = YES
36 Correct 1 ms 204 KB answer = YES
37 Correct 1 ms 204 KB answer = YES
38 Correct 1 ms 204 KB answer = YES
39 Correct 1 ms 332 KB answer = YES
40 Correct 2 ms 332 KB answer = YES
41 Correct 2 ms 332 KB answer = NO
42 Correct 3 ms 332 KB answer = YES
43 Correct 2 ms 332 KB answer = YES
44 Correct 2 ms 332 KB answer = YES
45 Correct 2 ms 332 KB answer = YES
46 Correct 1 ms 332 KB answer = YES
47 Correct 2 ms 332 KB answer = YES
48 Correct 2 ms 332 KB answer = YES
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB answer = YES
2 Correct 1 ms 204 KB answer = YES
3 Correct 1 ms 204 KB answer = YES
4 Correct 1 ms 204 KB answer = NO
5 Correct 1 ms 204 KB answer = YES
6 Correct 1 ms 300 KB answer = YES
7 Correct 1 ms 296 KB answer = YES
8 Correct 1 ms 204 KB answer = YES
9 Correct 1 ms 204 KB answer = NO
10 Correct 1 ms 204 KB answer = YES
11 Correct 1 ms 296 KB answer = YES
12 Correct 1 ms 208 KB answer = NO
13 Correct 1 ms 296 KB answer = YES
14 Correct 1 ms 204 KB answer = YES
15 Correct 1 ms 204 KB answer = YES
16 Correct 1 ms 204 KB answer = YES
17 Correct 1 ms 296 KB answer = YES
18 Correct 1 ms 204 KB answer = YES
19 Correct 1 ms 296 KB answer = YES
20 Correct 1 ms 204 KB answer = YES
21 Correct 1 ms 204 KB answer = YES
22 Correct 1 ms 204 KB answer = NO
23 Correct 1 ms 204 KB answer = NO
24 Correct 1 ms 204 KB answer = YES
25 Correct 1 ms 204 KB answer = YES
26 Correct 1 ms 204 KB answer = YES
27 Correct 1 ms 292 KB answer = YES
28 Correct 1 ms 204 KB answer = YES
29 Correct 1 ms 204 KB answer = YES
30 Correct 1 ms 204 KB answer = NO
31 Correct 1 ms 204 KB answer = YES
32 Correct 1 ms 204 KB answer = YES
33 Correct 1 ms 204 KB answer = YES
34 Correct 1 ms 204 KB answer = YES
35 Correct 1 ms 204 KB answer = YES
36 Correct 1 ms 204 KB answer = YES
37 Correct 1 ms 204 KB answer = YES
38 Correct 1 ms 204 KB answer = YES
39 Correct 1 ms 332 KB answer = YES
40 Correct 2 ms 332 KB answer = YES
41 Correct 2 ms 332 KB answer = NO
42 Correct 3 ms 332 KB answer = YES
43 Correct 2 ms 332 KB answer = YES
44 Correct 2 ms 332 KB answer = YES
45 Correct 2 ms 332 KB answer = YES
46 Correct 1 ms 332 KB answer = YES
47 Correct 2 ms 332 KB answer = YES
48 Correct 2 ms 332 KB answer = YES
49 Correct 15 ms 1368 KB answer = YES
50 Correct 16 ms 1740 KB answer = YES
51 Correct 15 ms 1612 KB answer = YES
52 Correct 10 ms 1656 KB answer = NO
53 Correct 2 ms 332 KB answer = YES
54 Correct 4 ms 460 KB answer = YES
55 Correct 9 ms 844 KB answer = YES
56 Correct 14 ms 1360 KB answer = YES
57 Correct 14 ms 1228 KB answer = YES
58 Correct 16 ms 1248 KB answer = YES
59 Correct 16 ms 1284 KB answer = YES
60 Correct 20 ms 1368 KB answer = YES
61 Correct 8 ms 844 KB answer = YES
62 Correct 215 ms 8364 KB answer = NO
63 Correct 172 ms 8440 KB answer = YES
64 Correct 170 ms 8424 KB answer = NO
65 Correct 178 ms 8476 KB answer = YES
66 Correct 4 ms 468 KB answer = YES
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB answer = YES
2 Correct 1 ms 204 KB answer = YES
3 Correct 1 ms 204 KB answer = YES
4 Correct 1 ms 204 KB answer = NO
5 Correct 1 ms 204 KB answer = YES
6 Correct 1 ms 300 KB answer = YES
7 Correct 1 ms 296 KB answer = YES
8 Correct 1 ms 204 KB answer = YES
9 Correct 1 ms 204 KB answer = NO
10 Correct 1 ms 204 KB answer = YES
11 Correct 1 ms 296 KB answer = YES
12 Correct 1 ms 208 KB answer = NO
13 Correct 1 ms 296 KB answer = YES
14 Correct 1 ms 204 KB answer = YES
15 Correct 1 ms 204 KB answer = YES
16 Correct 1 ms 204 KB answer = YES
17 Correct 1 ms 296 KB answer = YES
18 Correct 1 ms 204 KB answer = YES
19 Correct 1 ms 296 KB answer = YES
20 Correct 1 ms 204 KB answer = YES
21 Correct 1 ms 204 KB answer = YES
22 Correct 1 ms 204 KB answer = NO
23 Correct 1 ms 204 KB answer = NO
24 Correct 1 ms 204 KB answer = YES
25 Correct 1 ms 204 KB answer = YES
26 Correct 1 ms 204 KB answer = YES
27 Correct 1 ms 292 KB answer = YES
28 Correct 1 ms 204 KB answer = YES
29 Correct 1 ms 204 KB answer = YES
30 Correct 1 ms 204 KB answer = NO
31 Correct 1 ms 204 KB answer = YES
32 Correct 1 ms 204 KB answer = YES
33 Correct 1 ms 204 KB answer = YES
34 Correct 1 ms 204 KB answer = YES
35 Correct 1 ms 204 KB answer = YES
36 Correct 1 ms 204 KB answer = YES
37 Correct 1 ms 204 KB answer = YES
38 Correct 1 ms 204 KB answer = YES
39 Correct 1 ms 332 KB answer = YES
40 Correct 2 ms 332 KB answer = YES
41 Correct 2 ms 332 KB answer = NO
42 Correct 3 ms 332 KB answer = YES
43 Correct 2 ms 332 KB answer = YES
44 Correct 2 ms 332 KB answer = YES
45 Correct 2 ms 332 KB answer = YES
46 Correct 1 ms 332 KB answer = YES
47 Correct 2 ms 332 KB answer = YES
48 Correct 2 ms 332 KB answer = YES
49 Correct 15 ms 1368 KB answer = YES
50 Correct 16 ms 1740 KB answer = YES
51 Correct 15 ms 1612 KB answer = YES
52 Correct 10 ms 1656 KB answer = NO
53 Correct 2 ms 332 KB answer = YES
54 Correct 4 ms 460 KB answer = YES
55 Correct 9 ms 844 KB answer = YES
56 Correct 14 ms 1360 KB answer = YES
57 Correct 14 ms 1228 KB answer = YES
58 Correct 16 ms 1248 KB answer = YES
59 Correct 16 ms 1284 KB answer = YES
60 Correct 20 ms 1368 KB answer = YES
61 Correct 8 ms 844 KB answer = YES
62 Correct 215 ms 8364 KB answer = NO
63 Correct 172 ms 8440 KB answer = YES
64 Correct 170 ms 8424 KB answer = NO
65 Correct 178 ms 8476 KB answer = YES
66 Correct 4 ms 468 KB answer = YES
67 Correct 151 ms 15796 KB answer = YES
68 Correct 146 ms 15816 KB answer = YES
69 Correct 149 ms 15540 KB answer = YES
70 Correct 257 ms 19868 KB answer = YES
71 Correct 148 ms 15556 KB answer = YES
72 Correct 177 ms 9860 KB answer = YES
73 Correct 186 ms 9828 KB answer = YES
74 Correct 104 ms 9188 KB answer = YES
75 Correct 79 ms 9188 KB answer = NO
76 Correct 18 ms 1492 KB answer = YES
77 Correct 49 ms 2640 KB answer = YES
78 Correct 70 ms 4372 KB answer = YES
79 Correct 139 ms 8508 KB answer = YES
80 Correct 105 ms 9204 KB answer = YES
81 Correct 112 ms 12440 KB answer = NO
82 Correct 161 ms 15004 KB answer = YES
83 Correct 179 ms 15436 KB answer = YES
84 Correct 178 ms 15564 KB answer = YES
85 Correct 149 ms 15504 KB answer = YES
86 Correct 147 ms 15564 KB answer = YES
87 Correct 138 ms 10704 KB answer = NO
88 Correct 188 ms 11532 KB answer = YES
89 Correct 184 ms 8396 KB answer = YES
90 Correct 208 ms 8424 KB answer = YES
91 Correct 163 ms 8392 KB answer = YES
92 Correct 94 ms 5024 KB answer = YES
93 Correct 86 ms 5000 KB answer = YES
94 Correct 154 ms 15472 KB answer = NO
95 Correct 128 ms 8184 KB answer = NO
96 Correct 345 ms 17240 KB answer = YES
97 Correct 105 ms 15516 KB answer = NO