답안 #85970

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
85970 2018-11-23T14:29:51 Z wolfris Pipes (BOI13_pipes) C++14
100 / 100
394 ms 33776 KB
#include <iostream>

#include <stdio.h>

#include <vector>

#include <queue>



using namespace std;



int n, m, c[100015], adjcnt[100015], p[500000], ncycle, sum, oper = 1;

vector <pair <int, int>> a[100015];

pair <int, int> trace[100015];



bool trim_graph() {

    queue <int> q;

    for (int u = 1; u <= n; ++u)

        if (adjcnt[u] == 1)

            q.push(u);

    while (!q.empty()) {

        int u = q.front(); q.pop();

        --ncycle;

        for (auto e: a[u])

            if (adjcnt[e.first]) {

                int v = e.first;

                --adjcnt[u]; --adjcnt[v];

                p[e.second] = 2 * c[u];

                c[v] -= c[u];

                if (ncycle == 1 && c[v])

                    return false;

                if (adjcnt[v] == 1)

                    q.push(v);

            }

    }

    return true;

}



void dfs(int u) {

    sum += c[u] * oper; oper = -oper;

    for (auto e: a[u])

        if (adjcnt[e.first] == 2) {

            int v = e.first;

            --adjcnt[u]; --adjcnt[v],

            trace[u] = e; dfs(v);

            return;

        }

    for (auto e: a[u])

        if (adjcnt[e.first] == 1) {

            int v = e.first;

            p[e.second] = sum;

            c[u] -= sum / 2;

            c[v] -= sum / 2;

            return;

        }

}



int main() {

    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    cin >> n >> m;

    for (int u = 1; u <= n; ++u)

        cin >> c[u];

    for (int i = 0, u, v; i < m; ++i)

        cin >> u >> v,

        a[u].push_back({v, i}),

        a[v].push_back({u, i}),

        ++adjcnt[u], ++adjcnt[v];

    if (m > n) goto NO;

    ncycle = n;

    if (!trim_graph()) goto NO;

    if (m == n) {

        if (ncycle % 2 == 0) goto NO;

        int u;

        for (u = 1; u <= n; ++u)

            if (adjcnt[u] == 2) {

                dfs(u); break;

            }

        for (; trace[u].first; u = trace[u].first)

            p[trace[u].second] = 2 * c[u],

            c[trace[u].first] -= c[u];

    }

    for (int i = 0; i < m; ++i)

        cout << p[i] << '\n';

    return 0;

    NO: cout << 0;

}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2680 KB Output is correct
2 Correct 4 ms 2856 KB Output is correct
3 Correct 4 ms 3064 KB Output is correct
4 Correct 71 ms 9796 KB Output is correct
5 Correct 4 ms 9796 KB Output is correct
6 Correct 4 ms 9796 KB Output is correct
7 Correct 4 ms 9796 KB Output is correct
8 Correct 4 ms 9796 KB Output is correct
9 Correct 5 ms 9796 KB Output is correct
10 Correct 5 ms 9796 KB Output is correct
11 Correct 4 ms 9796 KB Output is correct
12 Correct 4 ms 9796 KB Output is correct
13 Correct 60 ms 10188 KB Output is correct
14 Correct 63 ms 12540 KB Output is correct
15 Correct 70 ms 14432 KB Output is correct
16 Correct 61 ms 14980 KB Output is correct
17 Correct 64 ms 17196 KB Output is correct
18 Correct 76 ms 18440 KB Output is correct
19 Correct 66 ms 18440 KB Output is correct
20 Correct 4 ms 18440 KB Output is correct
21 Correct 6 ms 18440 KB Output is correct
22 Correct 78 ms 18468 KB Output is correct
23 Correct 53 ms 18468 KB Output is correct
24 Correct 84 ms 18608 KB Output is correct
25 Correct 71 ms 18608 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 18608 KB Output is correct
2 Correct 4 ms 18608 KB Output is correct
3 Correct 55 ms 18608 KB Output is correct
4 Correct 53 ms 18608 KB Output is correct
5 Correct 53 ms 18608 KB Output is correct
6 Correct 220 ms 27900 KB Output is correct
7 Correct 4 ms 27900 KB Output is correct
8 Correct 4 ms 27900 KB Output is correct
9 Correct 4 ms 27900 KB Output is correct
10 Correct 4 ms 27900 KB Output is correct
11 Correct 4 ms 27900 KB Output is correct
12 Correct 4 ms 27900 KB Output is correct
13 Correct 4 ms 27900 KB Output is correct
14 Correct 4 ms 27900 KB Output is correct
15 Correct 4 ms 27900 KB Output is correct
16 Correct 5 ms 27900 KB Output is correct
17 Correct 4 ms 27900 KB Output is correct
18 Correct 4 ms 27900 KB Output is correct
19 Correct 4 ms 27900 KB Output is correct
20 Correct 4 ms 27900 KB Output is correct
21 Correct 5 ms 27900 KB Output is correct
22 Correct 4 ms 27900 KB Output is correct
23 Correct 66 ms 27900 KB Output is correct
24 Correct 70 ms 27900 KB Output is correct
25 Correct 57 ms 27900 KB Output is correct
26 Correct 55 ms 27900 KB Output is correct
27 Correct 54 ms 27900 KB Output is correct
28 Correct 58 ms 27900 KB Output is correct
29 Correct 172 ms 27900 KB Output is correct
30 Correct 67 ms 27900 KB Output is correct
31 Correct 72 ms 27900 KB Output is correct
32 Correct 71 ms 27900 KB Output is correct
33 Correct 55 ms 27900 KB Output is correct
34 Correct 52 ms 27900 KB Output is correct
35 Correct 67 ms 27900 KB Output is correct
36 Correct 54 ms 27900 KB Output is correct
37 Correct 219 ms 33440 KB Output is correct
38 Correct 69 ms 33440 KB Output is correct
39 Correct 89 ms 33440 KB Output is correct
40 Correct 69 ms 33440 KB Output is correct
41 Correct 55 ms 33440 KB Output is correct
42 Correct 64 ms 33440 KB Output is correct
43 Correct 58 ms 33440 KB Output is correct
44 Correct 53 ms 33440 KB Output is correct
45 Correct 176 ms 33720 KB Output is correct
46 Correct 74 ms 33720 KB Output is correct
47 Correct 76 ms 33720 KB Output is correct
48 Correct 82 ms 33720 KB Output is correct
49 Correct 83 ms 33720 KB Output is correct
50 Correct 66 ms 33720 KB Output is correct
51 Correct 59 ms 33720 KB Output is correct
52 Correct 92 ms 33720 KB Output is correct
53 Correct 394 ms 33776 KB Output is correct
54 Correct 96 ms 33776 KB Output is correct