Submission #489907

# Submission time Handle Problem Language Result Execution time Memory
489907 2021-11-25T03:58:49 Z SirCovidThe19th Pipes (BOI13_pipes) C++17
100 / 100
399 ms 54872 KB
#include <bits/stdc++.h>
using namespace std;

#define FOR(i, x, y) for (int i = x; i < y; i++)
#define pii pair<int, int>
#define f first
#define s second

const int mN = 1e5 + 5, mM = 5e5 + 5;

int n, m, cnt, st; long long A[mN], ans[mM], altSm; bool vis[mN]; set<pii> adj[mN]; 

void compressLeaves(){
    queue<int> q; 
    FOR(i, 0, n) if (adj[i].size() == 1) q.push(i);
    while (!q.empty()){
        int cur = q.front(); q.pop();
        if (adj[cur].empty()) continue;

        pii nxt = *adj[cur].begin(); 
        ans[nxt.s] = A[cur] * 2; A[nxt.f] -= A[cur];
        adj[cur].clear(); adj[nxt.f].erase({cur, nxt.s}); 
        if (adj[nxt.f].size() == 1) q.push(nxt.f);
    }
}
void dfs(int cur, bool parity){
    altSm += !parity ? A[cur] : -A[cur]; vis[cur] = 1;
    for (pii nxt : adj[cur]) if (!vis[nxt.f]){
        dfs(nxt.f, !parity);
    }
}

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0); 
    cin >> n >> m;
    FOR(i, 0, n) cin >> A[i];
    FOR(i, 0, m){
        int a, b; cin >> a >> b; a--; b--;
        adj[a].insert({b, i}); adj[b].insert({a, i});
    }
    compressLeaves();

    FOR(i, 0, n){
        if (adj[i].size() == 2) st = i, cnt++;
        if (adj[i].size() > 2){ cout<<0<<"\n"; return 0; }
    }
    if (cnt and cnt % 2 == 0){ cout<<0<<"\n"; return 0; }

	if (cnt){
		dfs(st, 0);
        
        pii rem = *prev(adj[st].end()); 
        A[st] -= altSm / 2; A[rem.f] -= altSm / 2;
        ans[rem.s] = altSm;
        adj[st].erase(rem); adj[rem.f].erase({st, rem.s});
        
        compressLeaves();
	}
    FOR(i, 0, m) cout<<ans[i]<<"\n";
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 5128 KB Output is correct
4 Correct 116 ms 18136 KB Output is correct
5 Correct 3 ms 4940 KB Output is correct
6 Correct 3 ms 5012 KB Output is correct
7 Correct 2 ms 4940 KB Output is correct
8 Correct 3 ms 4940 KB Output is correct
9 Correct 3 ms 5080 KB Output is correct
10 Correct 3 ms 5068 KB Output is correct
11 Correct 3 ms 5068 KB Output is correct
12 Correct 3 ms 5156 KB Output is correct
13 Correct 71 ms 15444 KB Output is correct
14 Correct 97 ms 17420 KB Output is correct
15 Correct 136 ms 18176 KB Output is correct
16 Correct 81 ms 16148 KB Output is correct
17 Correct 97 ms 18072 KB Output is correct
18 Correct 104 ms 18136 KB Output is correct
19 Correct 107 ms 17992 KB Output is correct
20 Correct 3 ms 4992 KB Output is correct
21 Correct 3 ms 5068 KB Output is correct
22 Correct 102 ms 18200 KB Output is correct
23 Correct 72 ms 15400 KB Output is correct
24 Correct 103 ms 18140 KB Output is correct
25 Correct 76 ms 15920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 5196 KB Output is correct
3 Correct 74 ms 16828 KB Output is correct
4 Correct 63 ms 17520 KB Output is correct
5 Correct 81 ms 17244 KB Output is correct
6 Correct 399 ms 54872 KB Output is correct
7 Correct 2 ms 4940 KB Output is correct
8 Correct 2 ms 4940 KB Output is correct
9 Correct 2 ms 4940 KB Output is correct
10 Correct 3 ms 5020 KB Output is correct
11 Correct 4 ms 5020 KB Output is correct
12 Correct 3 ms 4940 KB Output is correct
13 Correct 3 ms 4940 KB Output is correct
14 Correct 3 ms 4940 KB Output is correct
15 Correct 3 ms 5160 KB Output is correct
16 Correct 3 ms 5152 KB Output is correct
17 Correct 3 ms 5068 KB Output is correct
18 Correct 3 ms 5068 KB Output is correct
19 Correct 3 ms 5068 KB Output is correct
20 Correct 3 ms 5068 KB Output is correct
21 Correct 4 ms 5324 KB Output is correct
22 Correct 5 ms 5160 KB Output is correct
23 Correct 79 ms 18096 KB Output is correct
24 Correct 105 ms 20528 KB Output is correct
25 Correct 79 ms 16836 KB Output is correct
26 Correct 62 ms 17584 KB Output is correct
27 Correct 57 ms 17332 KB Output is correct
28 Correct 109 ms 18112 KB Output is correct
29 Correct 311 ms 46104 KB Output is correct
30 Correct 90 ms 19908 KB Output is correct
31 Correct 98 ms 22120 KB Output is correct
32 Correct 103 ms 18968 KB Output is correct
33 Correct 81 ms 17592 KB Output is correct
34 Correct 66 ms 17472 KB Output is correct
35 Correct 66 ms 17456 KB Output is correct
36 Correct 78 ms 17728 KB Output is correct
37 Correct 394 ms 54820 KB Output is correct
38 Correct 93 ms 20292 KB Output is correct
39 Correct 119 ms 18700 KB Output is correct
40 Correct 133 ms 20400 KB Output is correct
41 Correct 60 ms 17456 KB Output is correct
42 Correct 61 ms 17516 KB Output is correct
43 Correct 57 ms 17452 KB Output is correct
44 Correct 87 ms 17324 KB Output is correct
45 Correct 378 ms 49088 KB Output is correct
46 Correct 82 ms 19908 KB Output is correct
47 Correct 110 ms 20420 KB Output is correct
48 Correct 97 ms 21968 KB Output is correct
49 Correct 82 ms 16960 KB Output is correct
50 Correct 64 ms 17512 KB Output is correct
51 Correct 105 ms 17632 KB Output is correct
52 Correct 60 ms 17384 KB Output is correct
53 Correct 389 ms 50116 KB Output is correct
54 Correct 96 ms 19616 KB Output is correct