답안 #816117

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
816117 2023-08-09T04:13:54 Z QwertyPi Pipes (BOI13_pipes) C++14
100 / 100
414 ms 58388 KB
#include <bits/stdc++.h>
using namespace std;

const int MAXN = 1e5 + 11;
const int MAXM = 5e5 + 11;

struct edge{
    int u, v, w;
    int exc(int x){
        return u + v - x;
    }
};
vector<edge> E;
set<int> G[MAXN];
int a[MAXN], b[MAXN], c[MAXM];

int32_t main(){
    cin.tie(0); cout.tie(0)->sync_with_stdio(false);
    int n, m; cin >> n >> m;
    for(int i = 1; i <= n; i++) cin >> a[i];
    int s = accumulate(a + 1, a + n + 1, 0LL);
    if(s % 2 == 1){
        cout << 0 << endl;
        return 0;
    }
    for(int i = 0; i < m; i++){
        int u, v; cin >> u >> v;
        E.push_back({u, v, 0});
        G[u].insert(i);
        G[v].insert(i);
    }

    vector<int> d1;
    for(int i = 1; i <= n; i++){
        if(G[i].size() == 1) d1.push_back(i);
    }

    while(!d1.empty()){
        int v = d1.back(); d1.pop_back();
        if(G[v].empty()) continue;
        for(auto e : G[v]){
            int u = E[e].u + E[e].v - v;
            G[u].erase(e);
            if(G[u].size() == 1) d1.push_back(u);
        }
        for(auto e : G[v]){
            c[e] = (a[v] - b[v]) * 2;
            b[E[e].u] += c[e] / 2;
            b[E[e].v] += c[e] / 2;
        }
        G[v].clear();
    }

    int node = 0, x = -1;
    for(int i = 1; i <= n; i++){
        if(G[i].size() == 2){
            node++; x = i;
        }else if(G[i].size() > 2){
            cout << 0 << endl;
            return 0;
        }
    }

    if(node > 0 && node % 2 == 0){
        cout << 0 << endl;
        return 0;
    }

    if(node >= 3){
        vector<int> V, paths;
        V.push_back(x); paths.push_back(*G[x].begin());
        x = E[*G[x].begin()].u;
        for(int i = 1; i < node; i++){
            V.push_back(x); paths.push_back(E[*G[x].begin()].exc(x) == V[i - 1] ? *--G[x].end() : *G[x].begin());
            x = E[paths.back()].exc(x);
        }

        vector<int> tmp; tmp.push_back(0); for(int i = 1; i < node; i++) tmp.push_back((a[V[i]] - b[V[i]]) * 2 - tmp.back());
        int d = ((a[V[0]] - b[V[0]]) * 2 - tmp[0] - tmp[node - 1]) / 2;
        tmp.clear(); tmp.push_back(d); for(int i = 1; i < node; i++) tmp.push_back((a[V[i]] - b[V[i]]) * 2 - tmp.back());
        for(int i = 0; i < node; i++) c[paths[i]] = tmp[i];
    }

    for(int i = 0; i < m; i++){
        cout << c[i] << '\n';
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 3 ms 5076 KB Output is correct
4 Correct 69 ms 17492 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Correct 2 ms 4948 KB Output is correct
7 Correct 2 ms 4948 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 3 ms 5076 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
11 Correct 3 ms 5164 KB Output is correct
12 Correct 3 ms 5076 KB Output is correct
13 Correct 63 ms 15060 KB Output is correct
14 Correct 70 ms 16916 KB Output is correct
15 Correct 71 ms 17604 KB Output is correct
16 Correct 65 ms 15704 KB Output is correct
17 Correct 68 ms 17480 KB Output is correct
18 Correct 66 ms 17580 KB Output is correct
19 Correct 74 ms 17156 KB Output is correct
20 Correct 2 ms 4948 KB Output is correct
21 Correct 3 ms 5044 KB Output is correct
22 Correct 66 ms 17624 KB Output is correct
23 Correct 49 ms 15016 KB Output is correct
24 Correct 78 ms 17604 KB Output is correct
25 Correct 60 ms 15472 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 2 ms 5076 KB Output is correct
3 Correct 56 ms 16284 KB Output is correct
4 Correct 67 ms 16744 KB Output is correct
5 Correct 53 ms 16872 KB Output is correct
6 Correct 366 ms 58388 KB Output is correct
7 Correct 2 ms 4948 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 2 ms 4948 KB Output is correct
10 Correct 2 ms 4948 KB Output is correct
11 Correct 2 ms 4948 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 2 ms 4948 KB Output is correct
14 Correct 2 ms 4948 KB Output is correct
15 Correct 2 ms 5076 KB Output is correct
16 Correct 3 ms 5076 KB Output is correct
17 Correct 2 ms 5076 KB Output is correct
18 Correct 2 ms 5076 KB Output is correct
19 Correct 4 ms 5076 KB Output is correct
20 Correct 3 ms 5076 KB Output is correct
21 Correct 3 ms 5332 KB Output is correct
22 Correct 2 ms 5076 KB Output is correct
23 Correct 70 ms 16056 KB Output is correct
24 Correct 111 ms 18336 KB Output is correct
25 Correct 67 ms 16440 KB Output is correct
26 Correct 53 ms 16892 KB Output is correct
27 Correct 56 ms 16732 KB Output is correct
28 Correct 72 ms 17564 KB Output is correct
29 Correct 295 ms 48660 KB Output is correct
30 Correct 64 ms 17784 KB Output is correct
31 Correct 81 ms 18608 KB Output is correct
32 Correct 70 ms 17668 KB Output is correct
33 Correct 51 ms 16896 KB Output is correct
34 Correct 45 ms 16812 KB Output is correct
35 Correct 56 ms 16868 KB Output is correct
36 Correct 55 ms 17084 KB Output is correct
37 Correct 414 ms 58328 KB Output is correct
38 Correct 67 ms 18208 KB Output is correct
39 Correct 82 ms 17500 KB Output is correct
40 Correct 70 ms 18284 KB Output is correct
41 Correct 43 ms 16768 KB Output is correct
42 Correct 43 ms 16676 KB Output is correct
43 Correct 46 ms 16720 KB Output is correct
44 Correct 55 ms 16784 KB Output is correct
45 Correct 339 ms 52112 KB Output is correct
46 Correct 61 ms 18044 KB Output is correct
47 Correct 72 ms 18304 KB Output is correct
48 Correct 63 ms 18588 KB Output is correct
49 Correct 76 ms 16444 KB Output is correct
50 Correct 72 ms 16880 KB Output is correct
51 Correct 51 ms 16932 KB Output is correct
52 Correct 44 ms 16644 KB Output is correct
53 Correct 332 ms 52992 KB Output is correct
54 Correct 83 ms 17928 KB Output is correct