Submission #79878

# Submission time Handle Problem Language Result Execution time Memory
79878 2018-10-17T03:14:12 Z win11905 Pipes (BOI13_pipes) C++11
70.1852 / 100
167 ms 38748 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 5112 KB Output is correct
2 Correct 7 ms 5248 KB Output is correct
3 Correct 7 ms 5328 KB Output is correct
4 Correct 133 ms 17644 KB Output is correct
5 Correct 6 ms 17644 KB Output is correct
6 Correct 6 ms 17644 KB Output is correct
7 Correct 6 ms 17644 KB Output is correct
8 Correct 6 ms 17644 KB Output is correct
9 Correct 6 ms 17644 KB Output is correct
10 Correct 6 ms 17644 KB Output is correct
11 Correct 7 ms 17644 KB Output is correct
12 Correct 6 ms 17644 KB Output is correct
13 Correct 100 ms 17644 KB Output is correct
14 Correct 115 ms 17644 KB Output is correct
15 Correct 151 ms 17724 KB Output is correct
16 Correct 125 ms 17724 KB Output is correct
17 Correct 167 ms 17724 KB Output is correct
18 Correct 163 ms 17724 KB Output is correct
19 Correct 110 ms 17724 KB Output is correct
20 Correct 6 ms 17724 KB Output is correct
21 Correct 6 ms 17724 KB Output is correct
22 Correct 119 ms 17756 KB Output is correct
23 Correct 96 ms 17756 KB Output is correct
24 Correct 144 ms 17808 KB Output is correct
25 Correct 133 ms 17808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 6 ms 17808 KB Output isn't correct
2 Correct 7 ms 17808 KB Output is correct
3 Incorrect 159 ms 20532 KB Output isn't correct
4 Correct 87 ms 20532 KB Output is correct
5 Correct 82 ms 20532 KB Output is correct
6 Incorrect 113 ms 20532 KB Output isn't correct
7 Incorrect 6 ms 20532 KB Output isn't correct
8 Correct 6 ms 20532 KB Output is correct
9 Incorrect 6 ms 20532 KB Output isn't correct
10 Correct 6 ms 20532 KB Output is correct
11 Correct 6 ms 20532 KB Output is correct
12 Correct 6 ms 20532 KB Output is correct
13 Correct 7 ms 20532 KB Output is correct
14 Incorrect 6 ms 20532 KB Output isn't correct
15 Correct 7 ms 20532 KB Output is correct
16 Correct 7 ms 20532 KB Output is correct
17 Incorrect 7 ms 20532 KB Output isn't correct
18 Correct 7 ms 20532 KB Output is correct
19 Correct 6 ms 20532 KB Output is correct
20 Correct 6 ms 20532 KB Output is correct
21 Correct 7 ms 20532 KB Output is correct
22 Incorrect 6 ms 20532 KB Output isn't correct
23 Correct 108 ms 20532 KB Output is correct
24 Correct 133 ms 21940 KB Output is correct
25 Incorrect 157 ms 21940 KB Output isn't correct
26 Correct 78 ms 21940 KB Output is correct
27 Correct 74 ms 21940 KB Output is correct
28 Incorrect 110 ms 21940 KB Output isn't correct
29 Incorrect 103 ms 21940 KB Output isn't correct
30 Incorrect 102 ms 21940 KB Output isn't correct
31 Correct 160 ms 24756 KB Output is correct
32 Correct 128 ms 24756 KB Output is correct
33 Incorrect 103 ms 24756 KB Output isn't correct
34 Correct 85 ms 24756 KB Output is correct
35 Correct 76 ms 24756 KB Output is correct
36 Incorrect 113 ms 24756 KB Output isn't correct
37 Incorrect 102 ms 24756 KB Output isn't correct
38 Correct 136 ms 24756 KB Output is correct
39 Incorrect 123 ms 24756 KB Output isn't correct
40 Incorrect 123 ms 24756 KB Output isn't correct
41 Incorrect 146 ms 24820 KB Output isn't correct
42 Correct 79 ms 24820 KB Output is correct
43 Correct 78 ms 24820 KB Output is correct
44 Correct 86 ms 24820 KB Output is correct
45 Incorrect 141 ms 24820 KB Output isn't correct
46 Correct 140 ms 24820 KB Output is correct
47 Incorrect 121 ms 24820 KB Output isn't correct
48 Correct 151 ms 24820 KB Output is correct
49 Incorrect 117 ms 24820 KB Output isn't correct
50 Correct 75 ms 24820 KB Output is correct
51 Correct 108 ms 24820 KB Output is correct
52 Correct 79 ms 24820 KB Output is correct
53 Runtime error 106 ms 38748 KB Execution killed with signal 11 (could be triggered by violating memory limits)
54 Incorrect 109 ms 38748 KB Output isn't correct