답안 #445096

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
445096 2021-07-16T12:11:10 Z zxcvbnm Pipes (BOI13_pipes) C++14
100 / 100
315 ms 52036 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int maxN = 1e5 + 5;
vector<int> need, curr;
vector<pair<int, int>> adj[maxN];
vector<int> in;
vector<int> ans;
void tree(int n) {
    queue<int> q;
    vector<bool> vis(n, false);
    for(int i = 0; i < n; i++) {
        if (in[i] == 1) {
            q.push(i);
        }
    }

    while(!q.empty()) {
        int v = q.front();
        q.pop();
        vis[v] = true;

        for(auto u : adj[v]) {
            if (vis[u.first]) continue;

            ans[u.second] = (need[v] - curr[v]) * 2;
            curr[u.first] += ans[u.second] / 2;
            curr[v] += ans[u.second] / 2;

            in[u.first]--;
            if (in[u.first] == 1) {
                q.push(u.first);
            }
        }
        assert(curr[v] == need[v]);
    }
}
void cant() {
    cout << "0\n";
    exit(0);
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, m;
    cin >> n >> m;
    need.resize(n);
    curr.assign(n, 0);
    in.assign(n, 0);
    ans.assign(n, 0);
    for(int& i : need) {
        cin >> i;
    }

    map<pair<int, int>, int> edges;
    for(int i = 0; i < m; i++) {
        int x, y;
        cin >> x >> y;
        x--, y--;
        adj[x].push_back({y, i});
        adj[y].push_back({x, i});
        edges[{x, y}] = i;
        in[x]++;
        in[y]++;
    }

    if (m == n-1) {
        tree(n);
        for(int i = 0; i < m; i++) {
            cout << ans[i] << "\n";
        }
        exit(0);
    } else if (m != n) {
        cant();
    }

    queue<int> q;
    vector<bool> vis(n, false);
    for(int i = 0; i < n; i++) {
        if (in[i] == 1) {
            q.push(i);
        }
    }

    while(!q.empty()) {
        int v = q.front();
        q.pop();
        vis[v] = true;

        for(auto u : adj[v]) {
            if (vis[u.first]) continue;

            in[u.first]--;
            ans[u.second] = (need[v] - curr[v]) * 2;
            curr[u.first] += ans[u.second] / 2;
            curr[v] += ans[u.second] / 2;

            if (in[u.first] == 1) {
                q.push(u.first);
                vis[u.first] = true;
            }
        }
    }

    // even cycle
    if (count(vis.begin(), vis.end(), false) % 2 == 0) {
        cant();
    }

    int from = -1, to = -1;
    queue<int>().swap(q);

    for(int i = 0; i < n; i++) {
        if (!vis[i]) {
            q.push(i);
            from = i;
            vis[from] = true;
            for(auto u : adj[from]) {
                if (!vis[u.first]) {
                    to = u.first;
                    break;
                }
            }
            vis[to] = true;
            break;
        }
    }
    assert(from != -1 && to != -1);

    vector<int> cycle;
    cycle.push_back(from);
    while(!q.empty()) {
        int v = q.front();
        q.pop();

        for(auto u : adj[v]) {
            if (!vis[u.first]) {
                cycle.push_back(u.first);
                vis[u.first] = true;
                q.push(u.first);
            }
        }
    }
    cycle.push_back(to);

    // w_from->to
    int w = 0;
    for(int i = 0; i < n; i++) {
        need[i] -= curr[i];
    }

    for(int i = 0; i < (int) cycle.size(); i++) {
        w += need[cycle[i]] * (i % 2 ? -1 : +1);
    }

    ans[edges[{from, to}]] = w;
    reverse(cycle.begin(), cycle.end());

    int prev = cycle[0];
    for(int i = 1; i < (int) cycle.size(); i++) {
        int idx = edges.count({prev, cycle[i]}) == 0 ? edges[{cycle[i], prev}] : edges[{prev, cycle[i]}];
        ans[idx] = 2 * need[prev] - w;
        w = 2 * need[prev] - w;
        prev = cycle[i];
    }

    for(int i = 0; i < n; i++) {
        cout << ans[i] << "\n";
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 3 ms 2764 KB Output is correct
4 Correct 108 ms 16452 KB Output is correct
5 Correct 2 ms 2668 KB Output is correct
6 Correct 2 ms 2636 KB Output is correct
7 Correct 2 ms 2636 KB Output is correct
8 Correct 2 ms 2636 KB Output is correct
9 Correct 3 ms 2668 KB Output is correct
10 Correct 2 ms 2764 KB Output is correct
11 Correct 2 ms 2764 KB Output is correct
12 Correct 3 ms 2764 KB Output is correct
13 Correct 81 ms 13592 KB Output is correct
14 Correct 95 ms 15548 KB Output is correct
15 Correct 106 ms 16308 KB Output is correct
16 Correct 79 ms 14256 KB Output is correct
17 Correct 96 ms 16232 KB Output is correct
18 Correct 98 ms 16364 KB Output is correct
19 Correct 102 ms 15604 KB Output is correct
20 Correct 2 ms 2636 KB Output is correct
21 Correct 2 ms 2764 KB Output is correct
22 Correct 100 ms 16272 KB Output is correct
23 Correct 77 ms 13540 KB Output is correct
24 Correct 100 ms 16276 KB Output is correct
25 Correct 82 ms 14168 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 3 ms 2764 KB Output is correct
3 Correct 77 ms 14788 KB Output is correct
4 Correct 71 ms 15132 KB Output is correct
5 Correct 71 ms 15140 KB Output is correct
6 Correct 312 ms 52036 KB Output is correct
7 Correct 2 ms 2636 KB Output is correct
8 Correct 2 ms 2636 KB Output is correct
9 Correct 2 ms 2636 KB Output is correct
10 Correct 2 ms 2636 KB Output is correct
11 Correct 2 ms 2636 KB Output is correct
12 Correct 2 ms 2636 KB Output is correct
13 Correct 2 ms 2636 KB Output is correct
14 Correct 2 ms 2636 KB Output is correct
15 Correct 3 ms 2800 KB Output is correct
16 Correct 3 ms 2764 KB Output is correct
17 Correct 3 ms 2764 KB Output is correct
18 Correct 2 ms 2764 KB Output is correct
19 Correct 2 ms 2764 KB Output is correct
20 Correct 2 ms 2764 KB Output is correct
21 Correct 3 ms 2892 KB Output is correct
22 Correct 2 ms 2764 KB Output is correct
23 Correct 108 ms 13664 KB Output is correct
24 Correct 126 ms 16100 KB Output is correct
25 Correct 78 ms 14916 KB Output is correct
26 Correct 71 ms 15164 KB Output is correct
27 Correct 75 ms 15076 KB Output is correct
28 Correct 76 ms 15820 KB Output is correct
29 Correct 255 ms 42876 KB Output is correct
30 Correct 130 ms 15692 KB Output is correct
31 Correct 138 ms 15944 KB Output is correct
32 Correct 105 ms 16196 KB Output is correct
33 Correct 77 ms 15392 KB Output is correct
34 Correct 74 ms 15024 KB Output is correct
35 Correct 76 ms 15128 KB Output is correct
36 Correct 75 ms 15412 KB Output is correct
37 Correct 315 ms 51968 KB Output is correct
38 Correct 121 ms 15972 KB Output is correct
39 Correct 106 ms 16240 KB Output is correct
40 Correct 122 ms 16248 KB Output is correct
41 Correct 71 ms 15044 KB Output is correct
42 Correct 69 ms 14968 KB Output is correct
43 Correct 81 ms 15040 KB Output is correct
44 Correct 76 ms 15172 KB Output is correct
45 Correct 242 ms 45408 KB Output is correct
46 Correct 125 ms 15772 KB Output is correct
47 Correct 134 ms 16104 KB Output is correct
48 Correct 153 ms 16040 KB Output is correct
49 Correct 77 ms 15044 KB Output is correct
50 Correct 73 ms 15196 KB Output is correct
51 Correct 72 ms 15332 KB Output is correct
52 Correct 76 ms 14896 KB Output is correct
53 Correct 280 ms 46352 KB Output is correct
54 Correct 112 ms 15680 KB Output is correct