Submission #207362

# Submission time Handle Problem Language Result Execution time Memory
207362 2020-03-07T10:21:10 Z dolphingarlic Pipes (BOI13_pipes) C++14
100 / 100
558 ms 58872 KB
#include <bits/stdc++.h>
#define FOR(i, x, y) for (int i = x; i < y; i++)
typedef long long ll;
using namespace std;

int n, m;
set<pair<int, int>> graph[100001];
int c[100001], ans[500001], visited[100001], alt_sum = 0;

void dfs1(int node, int parity = 1) {
    visited[node] = parity;
    for (pair<int, int> i : graph[node]) if (!visited[i.first]) {
        dfs1(i.first, 3 - parity);
        if (parity == 1) alt_sum += c[i.first];
        else alt_sum -= c[i.first];
    }
}

void compress() {
    queue<int> leaves;
    FOR(i, 1, n + 1) if (graph[i].size() == 1) leaves.push(i);
    while (leaves.size()) {
        int curr = leaves.front();
        leaves.pop();
        for (pair<int, int> i : graph[curr]) {
            graph[i.first].erase({curr, i.second});
            ans[i.second] = 2 * c[curr];
            c[i.first] -= c[curr];

            if (graph[i.first].size() == 1) leaves.push(i.first);
        }
        graph[curr].clear();
    }
}

int main() {
    iostream::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> m;
    FOR(i, 1, n + 1) cin >> c[i];
    FOR(i, 1, m + 1) {
        int x, y;
        cin >> x >> y;
        graph[x].insert({y, i});
        graph[y].insert({x, i});
    }

    compress();

    int cnt = 0;
    FOR(i, 1, n + 1) {
        if (graph[i].size() > 2) return cout << 0, 0;
        if (graph[i].size() == 2) cnt++;
    }
    if (cnt != 0 && !(cnt & 1)) return cout << 0, 0;

    FOR(i, 1, n + 1) if (graph[i].size() == 2) {
        dfs1(i);
        int x = (*graph[i].begin()).first, y = (*graph[i].begin()).second;
        ans[y] = c[i] + alt_sum;
        graph[i].erase({x, y});
        graph[x].erase({i, y});
        c[i] -= ans[y] / 2;
        c[x] -= ans[y] / 2;
        break;
    }

    compress();

    FOR(i, 1, m + 1) cout << ans[i] << '\n';
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 8 ms 4984 KB Output is correct
2 Correct 7 ms 4984 KB Output is correct
3 Correct 8 ms 5112 KB Output is correct
4 Correct 144 ms 16888 KB Output is correct
5 Correct 8 ms 4984 KB Output is correct
6 Correct 8 ms 4988 KB Output is correct
7 Correct 8 ms 4984 KB Output is correct
8 Correct 8 ms 4984 KB Output is correct
9 Correct 8 ms 5240 KB Output is correct
10 Correct 8 ms 5240 KB Output is correct
11 Correct 8 ms 5112 KB Output is correct
12 Correct 8 ms 5112 KB Output is correct
13 Correct 111 ms 14840 KB Output is correct
14 Correct 138 ms 16248 KB Output is correct
15 Correct 138 ms 17016 KB Output is correct
16 Correct 115 ms 15224 KB Output is correct
17 Correct 132 ms 17016 KB Output is correct
18 Correct 133 ms 17016 KB Output is correct
19 Correct 117 ms 16760 KB Output is correct
20 Correct 8 ms 4984 KB Output is correct
21 Correct 9 ms 5120 KB Output is correct
22 Correct 133 ms 17020 KB Output is correct
23 Correct 102 ms 14456 KB Output is correct
24 Correct 141 ms 17236 KB Output is correct
25 Correct 115 ms 14968 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 4984 KB Output is correct
2 Correct 8 ms 5240 KB Output is correct
3 Correct 101 ms 16248 KB Output is correct
4 Correct 89 ms 16888 KB Output is correct
5 Correct 111 ms 16704 KB Output is correct
6 Correct 550 ms 58728 KB Output is correct
7 Correct 8 ms 4984 KB Output is correct
8 Correct 7 ms 4984 KB Output is correct
9 Correct 8 ms 4984 KB Output is correct
10 Correct 7 ms 4984 KB Output is correct
11 Correct 7 ms 4984 KB Output is correct
12 Correct 8 ms 4984 KB Output is correct
13 Correct 7 ms 5112 KB Output is correct
14 Correct 8 ms 4988 KB Output is correct
15 Correct 8 ms 5240 KB Output is correct
16 Correct 8 ms 5112 KB Output is correct
17 Correct 9 ms 5112 KB Output is correct
18 Correct 8 ms 5112 KB Output is correct
19 Correct 8 ms 5240 KB Output is correct
20 Correct 8 ms 5112 KB Output is correct
21 Correct 9 ms 5368 KB Output is correct
22 Correct 8 ms 5240 KB Output is correct
23 Correct 103 ms 18552 KB Output is correct
24 Correct 139 ms 20984 KB Output is correct
25 Correct 102 ms 16248 KB Output is correct
26 Correct 94 ms 16888 KB Output is correct
27 Correct 85 ms 16760 KB Output is correct
28 Correct 123 ms 17528 KB Output is correct
29 Correct 426 ms 48632 KB Output is correct
30 Correct 122 ms 20216 KB Output is correct
31 Correct 146 ms 23288 KB Output is correct
32 Correct 139 ms 18760 KB Output is correct
33 Correct 101 ms 16888 KB Output is correct
34 Correct 98 ms 16836 KB Output is correct
35 Correct 92 ms 16888 KB Output is correct
36 Correct 111 ms 17016 KB Output is correct
37 Correct 558 ms 58872 KB Output is correct
38 Correct 132 ms 20600 KB Output is correct
39 Correct 133 ms 18364 KB Output is correct
40 Correct 137 ms 20728 KB Output is correct
41 Correct 88 ms 16760 KB Output is correct
42 Correct 80 ms 16760 KB Output is correct
43 Correct 81 ms 16764 KB Output is correct
44 Correct 116 ms 16632 KB Output is correct
45 Correct 524 ms 52216 KB Output is correct
46 Correct 126 ms 20216 KB Output is correct
47 Correct 141 ms 20856 KB Output is correct
48 Correct 135 ms 22904 KB Output is correct
49 Correct 115 ms 16248 KB Output is correct
50 Correct 97 ms 16832 KB Output is correct
51 Correct 106 ms 16888 KB Output is correct
52 Correct 97 ms 16656 KB Output is correct
53 Correct 524 ms 53368 KB Output is correct
54 Correct 128 ms 19868 KB Output is correct