Submission #79879

# Submission time Handle Problem Language Result Execution time Memory
79879 2018-10-17T03:15:06 Z win11905 Pipes (BOI13_pipes) C++11
83.1481 / 100
177 ms 38200 KB
#include <bits/stdc++.h>
using namespace std;

const int N = 1e5+5;

int n, m, s[N], t[N];
int A[N], deg[N];
int ans[N];

set<int> g[N];

void answer() {
    for(int i = 0; i < m; ++i) printf("%d\n", ans[i]);
    exit(0);
}

queue<int> Q;

void solve() {
    while(!Q.empty()) {
        int u = Q.front(); Q.pop();
        for(int x : g[u]) {
            int v = u ^ s[x] ^ t[x]; 
            g[v].erase(x);
            A[v] -= A[u];
            ans[x] = A[u] << 1;
            if(g[v].size() == 1) Q.emplace(v);
        }
        g[u].clear();
    }
}

int cnt = 0;

void dfs(int u, int p) {
    static int idx = 0;
    static int sum = 0;
    idx++;
    if(idx & 1) sum += A[u];
    else sum -= A[u];
    int z = -1;
    for(auto i : g[u]) {
        int v = u ^ s[i] ^ t[i];
        if(v != p) {
            if(cnt == idx) {
                ans[i] = sum;
                A[u] -= sum / 2, A[v] -= sum / 2;
                z = i;
                break;
            }
            dfs(v, u);
        }
    }
    if(cnt == idx) {
        int v = u ^ s[z] ^ t[z];
        g[v].erase(z), g[u].erase(z);
        Q.emplace(u);
    } 
}

int main() {
    scanf("%d %d", &n, &m);
    for(int i = 1; i <= n; ++i) scanf("%d", A+i);
    for(int i = 0; i < m; ++i) {
        scanf("%d %d", s+i, t+i);
        g[s[i]].emplace(i), g[t[i]].emplace(i);
        deg[s[i]]++, deg[t[i]]++;
    }
    if(n < m) return puts("0"), 0;
    for(int i = 1; i <= n; ++i) if(deg[i] == 1) Q.emplace(i);
    solve(); 
    if(n == m+1) answer();
    for(int i = 1; i <= n; ++i) if(g[i].size()) cnt++;
    if(cnt % 2 == 0) return puts("0"), 0;
    for(int i = 1; i <= n; ++i) {
        if(g[i].size()) dfs(i, i);
        break;
    }
    solve();
    answer();
}

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:38: 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:65:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d", s+i, t+i);
         ~~~~~^~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 4984 KB Output is correct
2 Correct 6 ms 4984 KB Output is correct
3 Correct 7 ms 5128 KB Output is correct
4 Correct 164 ms 17060 KB Output is correct
5 Correct 6 ms 17060 KB Output is correct
6 Correct 6 ms 17060 KB Output is correct
7 Correct 6 ms 17060 KB Output is correct
8 Correct 7 ms 17060 KB Output is correct
9 Correct 6 ms 17060 KB Output is correct
10 Correct 7 ms 17060 KB Output is correct
11 Correct 7 ms 17060 KB Output is correct
12 Correct 7 ms 17060 KB Output is correct
13 Correct 104 ms 17060 KB Output is correct
14 Correct 177 ms 17060 KB Output is correct
15 Correct 127 ms 17248 KB Output is correct
16 Correct 140 ms 17248 KB Output is correct
17 Correct 139 ms 17248 KB Output is correct
18 Correct 135 ms 17264 KB Output is correct
19 Correct 147 ms 17264 KB Output is correct
20 Correct 6 ms 17264 KB Output is correct
21 Correct 6 ms 17264 KB Output is correct
22 Correct 156 ms 17272 KB Output is correct
23 Correct 114 ms 17272 KB Output is correct
24 Correct 144 ms 17304 KB Output is correct
25 Correct 109 ms 17304 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 6 ms 17304 KB Output isn't correct
2 Correct 7 ms 17304 KB Output is correct
3 Correct 116 ms 17304 KB Output is correct
4 Correct 83 ms 17304 KB Output is correct
5 Correct 80 ms 17304 KB Output is correct
6 Incorrect 116 ms 19644 KB Output isn't correct
7 Incorrect 6 ms 19644 KB Output isn't correct
8 Correct 6 ms 19644 KB Output is correct
9 Correct 6 ms 19644 KB Output is correct
10 Correct 6 ms 19644 KB Output is correct
11 Correct 6 ms 19644 KB Output is correct
12 Correct 6 ms 19644 KB Output is correct
13 Correct 6 ms 19644 KB Output is correct
14 Incorrect 7 ms 19644 KB Output isn't correct
15 Correct 7 ms 19644 KB Output is correct
16 Correct 6 ms 19644 KB Output is correct
17 Correct 6 ms 19644 KB Output is correct
18 Correct 6 ms 19644 KB Output is correct
19 Correct 6 ms 19644 KB Output is correct
20 Correct 8 ms 19644 KB Output is correct
21 Correct 7 ms 19644 KB Output is correct
22 Incorrect 6 ms 19644 KB Output isn't correct
23 Correct 116 ms 19644 KB Output is correct
24 Correct 158 ms 21436 KB Output is correct
25 Correct 92 ms 21436 KB Output is correct
26 Correct 79 ms 21436 KB Output is correct
27 Correct 78 ms 21436 KB Output is correct
28 Correct 118 ms 21436 KB Output is correct
29 Correct 114 ms 21436 KB Output is correct
30 Incorrect 120 ms 21436 KB Output isn't correct
31 Correct 152 ms 24168 KB Output is correct
32 Correct 129 ms 24168 KB Output is correct
33 Correct 98 ms 24168 KB Output is correct
34 Correct 77 ms 24168 KB Output is correct
35 Correct 76 ms 24168 KB Output is correct
36 Correct 100 ms 24168 KB Output is correct
37 Incorrect 111 ms 24168 KB Output isn't correct
38 Correct 140 ms 24168 KB Output is correct
39 Incorrect 121 ms 24168 KB Output isn't correct
40 Incorrect 123 ms 24168 KB Output isn't correct
41 Correct 102 ms 24168 KB Output is correct
42 Correct 82 ms 24168 KB Output is correct
43 Correct 77 ms 24168 KB Output is correct
44 Correct 76 ms 24168 KB Output is correct
45 Incorrect 140 ms 24168 KB Output isn't correct
46 Correct 149 ms 24168 KB Output is correct
47 Incorrect 116 ms 24168 KB Output isn't correct
48 Correct 165 ms 24168 KB Output is correct
49 Correct 108 ms 24168 KB Output is correct
50 Correct 90 ms 24168 KB Output is correct
51 Correct 79 ms 24168 KB Output is correct
52 Correct 72 ms 24168 KB Output is correct
53 Runtime error 125 ms 38200 KB Execution killed with signal 11 (could be triggered by violating memory limits)
54 Incorrect 138 ms 38200 KB Output isn't correct