Submission #84364

# Submission time Handle Problem Language Result Execution time Memory
84364 2018-11-14T15:24:53 Z FutymyClone Pipes (BOI13_pipes) C++14
100 / 100
287 ms 40244 KB
#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 5;

int n, m, deg[N], a[N];
long long ans[N];
vector <pair <int, int> > g[N];
bool notcyc[N], visited[N], flag = 0;
long long sum = 0, cursum = 0;

void bfs(){
    queue <int> q;
    for (int i = 1; i <= n; i++) if (deg[i] == 1) q.push(i);
    while (!q.empty()) {
        int u = q.front(); q.pop();
        for (auto nxt: g[u]) {
            int v = nxt.first, id = nxt.second;
            if (!notcyc[v]) {
                notcyc[u] = true;
                ans[id] = a[u];
                a[u] -= ans[id];
                a[v] -= ans[id];
                deg[v]--;
                if (deg[v] == 1) q.push(v);
            }
        }
    }
}

void dfs (int u, int p, int parity) {
    if (parity) cursum += a[u];
    visited[u] = 1;
    for (auto nxt: g[u]) {
        int v = nxt.first, id = nxt.second;
        if (v == p) continue;
        if (notcyc[v]) continue;
        if (!visited[v]) {
            dfs(v, u, parity ^ 1);
            ans[id] = a[v];
            a[u] -= ans[id];
            a[v] -= ans[id];
        }
        else {
            if (!parity) {
                printf("0");
                exit(0);
            }
            else {
                if (flag) return;
                flag = true;
                ans[id] = cursum - sum;
                a[u] -= ans[id];
                a[v] -= ans[id];
            }
        }
    }
}

int main(){
    scanf("%d %d", &n, &m);
    for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
    for (int i = 1; i <= m; i++) {
        int u, v;
        scanf("%d %d", &u, &v);
        g[u].push_back({v, i});
        g[v].push_back({u, i});
        deg[u]++; deg[v]++;
    }

    if (n < m) return !printf("0");
    bfs();
    for (int i = 1; i <= n; i++) if (!notcyc[i]) sum += a[i];
    if (sum & 1) return !printf("0");

    sum /= 2;
    for (int i = 1; i <= n; i++) {
        if (!notcyc[i]) {
            dfs(i, i, 1);
            break;
        }
    }

    for (int i = 1; i <= n; i++) if (a[i]) return !printf("0");
    for (int i = 1; i <= m; i++) if (ans[i] < -5e8 || ans[i] > 5e8) return !printf("0");
    for (int i = 1; i <= m; i++) printf("%lld\n", ans[i] * 2);
    return 0;
}

Compilation message

pipes.cpp: In function 'int main()':
pipes.cpp:62:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d", &n, &m);
     ~~~~~^~~~~~~~~~~~~~~~~
pipes.cpp:63:39: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
                                  ~~~~~^~~~~~~~~~~~~
pipes.cpp:66:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d", &u, &v);
         ~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2680 KB Output is correct
2 Correct 4 ms 2840 KB Output is correct
3 Correct 5 ms 2856 KB Output is correct
4 Correct 90 ms 9996 KB Output is correct
5 Correct 5 ms 9996 KB Output is correct
6 Correct 5 ms 9996 KB Output is correct
7 Correct 4 ms 9996 KB Output is correct
8 Correct 4 ms 9996 KB Output is correct
9 Correct 4 ms 9996 KB Output is correct
10 Correct 4 ms 9996 KB Output is correct
11 Correct 5 ms 9996 KB Output is correct
12 Correct 5 ms 9996 KB Output is correct
13 Correct 76 ms 9996 KB Output is correct
14 Correct 87 ms 12292 KB Output is correct
15 Correct 83 ms 14272 KB Output is correct
16 Correct 74 ms 14756 KB Output is correct
17 Correct 84 ms 16440 KB Output is correct
18 Correct 87 ms 17424 KB Output is correct
19 Correct 93 ms 17424 KB Output is correct
20 Correct 4 ms 17424 KB Output is correct
21 Correct 4 ms 17424 KB Output is correct
22 Correct 90 ms 17424 KB Output is correct
23 Correct 67 ms 17424 KB Output is correct
24 Correct 88 ms 19712 KB Output is correct
25 Correct 81 ms 20044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 20044 KB Output is correct
2 Correct 5 ms 20044 KB Output is correct
3 Correct 70 ms 25024 KB Output is correct
4 Correct 95 ms 25024 KB Output is correct
5 Correct 68 ms 25024 KB Output is correct
6 Correct 287 ms 34780 KB Output is correct
7 Correct 4 ms 34780 KB Output is correct
8 Correct 4 ms 34780 KB Output is correct
9 Correct 4 ms 34780 KB Output is correct
10 Correct 4 ms 34780 KB Output is correct
11 Correct 4 ms 34780 KB Output is correct
12 Correct 4 ms 34780 KB Output is correct
13 Correct 4 ms 34780 KB Output is correct
14 Correct 4 ms 34780 KB Output is correct
15 Correct 5 ms 34780 KB Output is correct
16 Correct 5 ms 34780 KB Output is correct
17 Correct 5 ms 34780 KB Output is correct
18 Correct 16 ms 34780 KB Output is correct
19 Correct 7 ms 34780 KB Output is correct
20 Correct 6 ms 34780 KB Output is correct
21 Correct 5 ms 34780 KB Output is correct
22 Correct 5 ms 34780 KB Output is correct
23 Correct 91 ms 34780 KB Output is correct
24 Correct 110 ms 34780 KB Output is correct
25 Correct 77 ms 34780 KB Output is correct
26 Correct 71 ms 34780 KB Output is correct
27 Correct 72 ms 34780 KB Output is correct
28 Correct 77 ms 34780 KB Output is correct
29 Correct 212 ms 34780 KB Output is correct
30 Correct 102 ms 34780 KB Output is correct
31 Correct 113 ms 34780 KB Output is correct
32 Correct 88 ms 34780 KB Output is correct
33 Correct 85 ms 34780 KB Output is correct
34 Correct 69 ms 34780 KB Output is correct
35 Correct 69 ms 34780 KB Output is correct
36 Correct 68 ms 34780 KB Output is correct
37 Correct 272 ms 35964 KB Output is correct
38 Correct 105 ms 35964 KB Output is correct
39 Correct 93 ms 35964 KB Output is correct
40 Correct 98 ms 35964 KB Output is correct
41 Correct 84 ms 35964 KB Output is correct
42 Correct 85 ms 35964 KB Output is correct
43 Correct 70 ms 35964 KB Output is correct
44 Correct 62 ms 35964 KB Output is correct
45 Correct 219 ms 40244 KB Output is correct
46 Correct 100 ms 40244 KB Output is correct
47 Correct 109 ms 40244 KB Output is correct
48 Correct 109 ms 40244 KB Output is correct
49 Correct 78 ms 40244 KB Output is correct
50 Correct 69 ms 40244 KB Output is correct
51 Correct 73 ms 40244 KB Output is correct
52 Correct 72 ms 40244 KB Output is correct
53 Correct 227 ms 40244 KB Output is correct
54 Correct 88 ms 40244 KB Output is correct