Submission #79880

# Submission time Handle Problem Language Result Execution time Memory
79880 2018-10-17T03:15:57 Z win11905 Pipes (BOI13_pipes) C++11
88.3333 / 100
208 ms 24400 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);
    if(n < m) return puts("0"), 0;
    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]]++;
    }
    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:64: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:66: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 5 ms 5108 KB Output is correct
3 Correct 6 ms 5272 KB Output is correct
4 Correct 140 ms 17068 KB Output is correct
5 Correct 6 ms 17068 KB Output is correct
6 Correct 6 ms 17068 KB Output is correct
7 Correct 6 ms 17068 KB Output is correct
8 Correct 8 ms 17068 KB Output is correct
9 Correct 9 ms 17068 KB Output is correct
10 Correct 7 ms 17068 KB Output is correct
11 Correct 7 ms 17068 KB Output is correct
12 Correct 8 ms 17068 KB Output is correct
13 Correct 106 ms 17068 KB Output is correct
14 Correct 138 ms 17068 KB Output is correct
15 Correct 191 ms 17292 KB Output is correct
16 Correct 118 ms 17292 KB Output is correct
17 Correct 182 ms 17292 KB Output is correct
18 Correct 143 ms 17308 KB Output is correct
19 Correct 136 ms 17308 KB Output is correct
20 Correct 6 ms 17308 KB Output is correct
21 Correct 6 ms 17308 KB Output is correct
22 Correct 126 ms 17308 KB Output is correct
23 Correct 114 ms 17308 KB Output is correct
24 Correct 158 ms 17308 KB Output is correct
25 Correct 146 ms 17308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 7 ms 17308 KB Output isn't correct
2 Correct 7 ms 17308 KB Output is correct
3 Correct 127 ms 17308 KB Output is correct
4 Correct 6 ms 17308 KB Output is correct
5 Correct 6 ms 17308 KB Output is correct
6 Correct 6 ms 17308 KB Output is correct
7 Incorrect 6 ms 17308 KB Output isn't correct
8 Correct 7 ms 17308 KB Output is correct
9 Correct 6 ms 17308 KB Output is correct
10 Correct 7 ms 17308 KB Output is correct
11 Correct 6 ms 17308 KB Output is correct
12 Correct 6 ms 17308 KB Output is correct
13 Correct 6 ms 17308 KB Output is correct
14 Incorrect 8 ms 17308 KB Output isn't correct
15 Correct 7 ms 17308 KB Output is correct
16 Correct 9 ms 17308 KB Output is correct
17 Correct 7 ms 17308 KB Output is correct
18 Correct 6 ms 17308 KB Output is correct
19 Correct 6 ms 17308 KB Output is correct
20 Correct 6 ms 17308 KB Output is correct
21 Correct 6 ms 17308 KB Output is correct
22 Incorrect 7 ms 17308 KB Output isn't correct
23 Correct 107 ms 19004 KB Output is correct
24 Correct 186 ms 21428 KB Output is correct
25 Correct 129 ms 21428 KB Output is correct
26 Correct 6 ms 21428 KB Output is correct
27 Correct 6 ms 21428 KB Output is correct
28 Correct 6 ms 21428 KB Output is correct
29 Correct 6 ms 21428 KB Output is correct
30 Incorrect 122 ms 21428 KB Output isn't correct
31 Correct 192 ms 24400 KB Output is correct
32 Correct 174 ms 24400 KB Output is correct
33 Correct 118 ms 24400 KB Output is correct
34 Correct 7 ms 24400 KB Output is correct
35 Correct 6 ms 24400 KB Output is correct
36 Correct 6 ms 24400 KB Output is correct
37 Correct 6 ms 24400 KB Output is correct
38 Correct 168 ms 24400 KB Output is correct
39 Incorrect 162 ms 24400 KB Output isn't correct
40 Incorrect 140 ms 24400 KB Output isn't correct
41 Correct 105 ms 24400 KB Output is correct
42 Correct 6 ms 24400 KB Output is correct
43 Correct 6 ms 24400 KB Output is correct
44 Correct 6 ms 24400 KB Output is correct
45 Correct 6 ms 24400 KB Output is correct
46 Correct 208 ms 24400 KB Output is correct
47 Incorrect 157 ms 24400 KB Output isn't correct
48 Correct 197 ms 24400 KB Output is correct
49 Correct 143 ms 24400 KB Output is correct
50 Correct 7 ms 24400 KB Output is correct
51 Correct 6 ms 24400 KB Output is correct
52 Correct 6 ms 24400 KB Output is correct
53 Correct 6 ms 24400 KB Output is correct
54 Incorrect 122 ms 24400 KB Output isn't correct