답안 #1108347

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1108347 2024-11-03T21:54:57 Z Kirill22 Pipes (BOI13_pipes) C++17
100 / 100
146 ms 35172 KB
#include "bits/stdc++.h"
 
using namespace std;
 
//#include "debug.h"
 
void solve() {
    int n, m;
    cin >> n >> m;
    vector<long long> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    vector<vector<pair<int, int>>> g(n);
    for (int i = 0, x, y; i < m; i++) {
        cin >> x >> y;
        x--, y--;
        g[x].push_back({y, i});
        g[y].push_back({x, i});
    }
    vector<pair<int, int>> cycle, par(n);
    vector<int> usd(n), h2(n);
    auto bfs = [&] (auto&& bfs, int v, int pr) -> void {
        usd[v] = 1;
        for (auto [u, i] : g[v]) {
            if (!cycle.empty()) {
                break;
            }
            if (!usd[u]) {
                par[u] = {v, i};
                h2[u] = h2[v] + 1;
                bfs(bfs, u, v);
            } else if (u != pr && h2[v] % 2 == h2[u] % 2) {
                cycle.push_back({v, i});
                while (v != u) {
                    cycle.push_back(par[v]);
                    v = par[v].first;
                }
                std::reverse(cycle.begin(), cycle.end());
            }
        }
    };
    bfs(bfs, 0, 0);
    vector<long long> have(n), ans(m);
    vector<int> used(n), h(n);
    bool ok = true;
    auto dfs = [&] (auto&& dfs, int v, int pr) -> void {
        used[v] = 1;
        for (auto [u, i] : g[v]) {
            if (!used[u]) {
                h[u] = h[v] + 1;
//                cout << v << " " << u << endl;
                dfs(dfs, u, v);
//                cout << u << " " << a[u] << " " << have[u] << endl;
                ans[i] = a[u] - have[u];
                have[u] += ans[i];
                have[v] += ans[i];
            } else if (u != pr && h[v] % 2 != h[u] % 2) {
                ok = false;
            }
        }
    };
    if (cycle.empty()) {
        dfs(dfs, 0, 0);
        if (!ok) {
            cout << 0 << '\n';
            return;
        }
        assert(a == have);
        long long mx = 0;
        for (auto& x : ans) {
            mx = max(mx, abs(x) * 2);
        }
        if (mx > (long long) 1e9) {
            assert(false);
            cout << 0 << '\n';
            return;
        }
        for (int i = 0; i < m; i++) {
            cout << 2 * ans[i] << '\n';
        }
    } else {
        int rt = cycle[0].first;
        dfs(dfs, rt, rt);
//        debug(have);
        if (have[rt] != a[rt]) {
            assert(abs(a[rt] - have[rt]) % 2 == 0);
            long long val = (a[rt] - have[rt]) / 2;
//            cout << val << endl;
            ans[cycle.back().second] += val;
            have[cycle.back().first] += val;
            have[cycle[0].first] += val;
//            debug(have, cycle);
//            for (auto [x, y] : cycle) cout << x << " " << y << endl;
            for (int i = 0; i + 1 < (int) cycle.size(); i++) {
                ans[cycle[i].second] += i % 2 == 0 ? val : -val;
                have[cycle[i].first] += i % 2 == 0 ? val : -val;
                have[cycle[i + 1].first] += i % 2 == 0 ? val : -val;
//                debug(have);
            }
        }
//        debug(have, a);
        assert(have == a);
        if (!ok || m > n) {
//            assert(false);
            cout << 0 << '\n';
            return;
        }
        long long mx = 0;
        for (auto& x : ans) {
            mx = max(mx, abs(x) * 2);
        }
        if (mx > (long long) 1e9) {
            cout << 0 << '\n';
            return;
        }
        for (int i = 0; i < m; i++) {
            cout << 2 * ans[i] << '\n';
        }
    }
}
 
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
//    cin >> t;
    while (t--) {
        solve();
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 45 ms 13112 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 2 ms 592 KB Output is correct
13 Correct 37 ms 10388 KB Output is correct
14 Correct 47 ms 12396 KB Output is correct
15 Correct 47 ms 13128 KB Output is correct
16 Correct 41 ms 11088 KB Output is correct
17 Correct 45 ms 12968 KB Output is correct
18 Correct 45 ms 13136 KB Output is correct
19 Correct 78 ms 17992 KB Output is correct
20 Correct 1 ms 592 KB Output is correct
21 Correct 1 ms 336 KB Output is correct
22 Correct 45 ms 13084 KB Output is correct
23 Correct 37 ms 10440 KB Output is correct
24 Correct 46 ms 13128 KB Output is correct
25 Correct 40 ms 11080 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 592 KB Output is correct
3 Correct 47 ms 16584 KB Output is correct
4 Correct 61 ms 20932 KB Output is correct
5 Correct 32 ms 12360 KB Output is correct
6 Correct 135 ms 35144 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 592 KB Output is correct
16 Correct 1 ms 592 KB Output is correct
17 Correct 1 ms 592 KB Output is correct
18 Correct 1 ms 592 KB Output is correct
19 Correct 1 ms 592 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
21 Correct 1 ms 592 KB Output is correct
22 Correct 1 ms 592 KB Output is correct
23 Correct 42 ms 16760 KB Output is correct
24 Correct 51 ms 19144 KB Output is correct
25 Correct 39 ms 16716 KB Output is correct
26 Correct 47 ms 19912 KB Output is correct
27 Correct 42 ms 16328 KB Output is correct
28 Correct 37 ms 12624 KB Output is correct
29 Correct 142 ms 30416 KB Output is correct
30 Correct 56 ms 22464 KB Output is correct
31 Correct 59 ms 22972 KB Output is correct
32 Correct 46 ms 15044 KB Output is correct
33 Correct 47 ms 18320 KB Output is correct
34 Correct 63 ms 18772 KB Output is correct
35 Correct 49 ms 20920 KB Output is correct
36 Correct 36 ms 12624 KB Output is correct
37 Correct 146 ms 35172 KB Output is correct
38 Correct 55 ms 18112 KB Output is correct
39 Correct 44 ms 14408 KB Output is correct
40 Correct 52 ms 18544 KB Output is correct
41 Correct 51 ms 21832 KB Output is correct
42 Correct 44 ms 16880 KB Output is correct
43 Correct 66 ms 21700 KB Output is correct
44 Correct 38 ms 12428 KB Output is correct
45 Correct 107 ms 27236 KB Output is correct
46 Correct 57 ms 23488 KB Output is correct
47 Correct 49 ms 18884 KB Output is correct
48 Correct 57 ms 22612 KB Output is correct
49 Correct 41 ms 13412 KB Output is correct
50 Correct 43 ms 18916 KB Output is correct
51 Correct 40 ms 16080 KB Output is correct
52 Correct 42 ms 13656 KB Output is correct
53 Correct 124 ms 28904 KB Output is correct
54 Correct 57 ms 20160 KB Output is correct