Submission #448914

# Submission time Handle Problem Language Result Execution time Memory
448914 2021-08-02T02:02:08 Z JovanB Pipes (BOI13_pipes) C++17
100 / 100
264 ms 23980 KB
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ld = long double;

const int N = 100000;
const int M = 500000;

ll cap[N+5];
vector <pair <int, int>> graf[N+5];
int deg[N+5];
bool bio[N+5];
ll sol[M+5];

pair <int, int> f[N+5][2];

void idi(int v, int lst, int root){
    if(v == root && lst != 0) return;
    int was = 0;
    for(auto pr : graf[v]){
        int c = pr.first;
        if(deg[c] <= 0) continue;
        if(v == root){
            f[v][was++] = pr;
        }
        else{
            if(c == lst) f[v][0] = pr;
            else f[v][1] = pr;
        }
    }
    idi(f[v][1].first, v, root);
}

int main(){
    ios_base::sync_with_stdio(false), cin.tie(0);
    cout.precision(10);
    cout << fixed;

    int n, m;
    cin >> n >> m;
    for(int i=1; i<=n; i++) cin >> cap[i];
    for(int i=1; i<=m; i++){
        int a, b;
        cin >> a >> b;
        graf[a].push_back({b, i});
        graf[b].push_back({a, i});
        deg[a]++;
        deg[b]++;
    }
    if(m > n){
        cout << "0\n";
        return 0;
    }
    queue <int> q;
    for(int i=1; i<=n; i++){
        if(deg[i] == 1){
            q.push(i);
        }
    }
    int ost = n;
    while(!q.empty()){
        int v = q.front();
        q.pop();
        ost--;
        bio[v] = 1;
        deg[v] = 0;
        for(auto pr : graf[v]){
            int c = pr.first, ind = pr.second;
            if(bio[c]) continue;
            sol[ind] = cap[v];
            cap[v] = 0;
            cap[c] -= sol[ind];
            deg[c]--;
            if(deg[c] == 1){
                q.push(c);
            }
        }
    }
    if(ost > 0 && ost%2 == 0){
        cout << "0\n";
        return 0;
    }
    if(ost > 0){
        int beg = 1;
        for(int i=1; i<=n; i++){
            if(deg[i] > 0) beg = i;
        }
        idi(beg, 0, beg);
        int v = f[beg][1].first;
        while(v != beg){
            sol[f[v][1].second] = cap[v] - sol[f[v][0].second];
            v = f[v][1].first;
        }
        ll dif = cap[v] - sol[f[beg][0].second];
        if(dif%2){
            cout << "0\n";
            return 0;
        }
        sol[f[beg][1].second] = dif/2;
        v = f[beg][1].first;
        while(v != beg){
            sol[f[v][1].second] = cap[v] - sol[f[v][0].second];
            v = f[v][1].first;
        }
    }
    for(int i=1; i<=m; i++) cout << 2*sol[i] << "\n";
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2680 KB Output is correct
3 Correct 2 ms 2688 KB Output is correct
4 Correct 70 ms 10572 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 2 ms 2636 KB Output is correct
7 Correct 2 ms 2636 KB Output is correct
8 Correct 2 ms 2636 KB Output is correct
9 Correct 2 ms 2636 KB Output is correct
10 Correct 2 ms 2684 KB Output is correct
11 Correct 3 ms 2764 KB Output is correct
12 Correct 2 ms 2636 KB Output is correct
13 Correct 58 ms 9008 KB Output is correct
14 Correct 81 ms 10208 KB Output is correct
15 Correct 71 ms 10648 KB Output is correct
16 Correct 60 ms 9428 KB Output is correct
17 Correct 76 ms 10604 KB Output is correct
18 Correct 78 ms 10616 KB Output is correct
19 Correct 87 ms 9852 KB Output is correct
20 Correct 2 ms 2624 KB Output is correct
21 Correct 2 ms 2684 KB Output is correct
22 Correct 75 ms 10732 KB Output is correct
23 Correct 61 ms 8900 KB Output is correct
24 Correct 85 ms 10636 KB Output is correct
25 Correct 59 ms 9252 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2764 KB Output is correct
3 Correct 60 ms 9516 KB Output is correct
4 Correct 54 ms 8632 KB Output is correct
5 Correct 52 ms 8756 KB Output is correct
6 Correct 251 ms 23980 KB Output is correct
7 Correct 2 ms 2636 KB Output is correct
8 Correct 2 ms 2680 KB Output is correct
9 Correct 2 ms 2696 KB Output is correct
10 Correct 3 ms 2636 KB Output is correct
11 Correct 2 ms 2636 KB Output is correct
12 Correct 2 ms 2676 KB Output is correct
13 Correct 2 ms 2636 KB Output is correct
14 Correct 2 ms 2672 KB Output is correct
15 Correct 2 ms 2764 KB Output is correct
16 Correct 2 ms 2692 KB Output is correct
17 Correct 2 ms 2636 KB Output is correct
18 Correct 2 ms 2636 KB Output is correct
19 Correct 2 ms 2636 KB Output is correct
20 Correct 2 ms 2636 KB Output is correct
21 Correct 3 ms 2732 KB Output is correct
22 Correct 2 ms 2764 KB Output is correct
23 Correct 60 ms 9992 KB Output is correct
24 Correct 87 ms 11772 KB Output is correct
25 Correct 66 ms 9508 KB Output is correct
26 Correct 49 ms 8600 KB Output is correct
27 Correct 53 ms 8512 KB Output is correct
28 Correct 68 ms 8984 KB Output is correct
29 Correct 219 ms 20036 KB Output is correct
30 Correct 75 ms 11288 KB Output is correct
31 Correct 87 ms 11516 KB Output is correct
32 Correct 79 ms 12148 KB Output is correct
33 Correct 55 ms 9796 KB Output is correct
34 Correct 65 ms 8560 KB Output is correct
35 Correct 55 ms 8680 KB Output is correct
36 Correct 60 ms 8924 KB Output is correct
37 Correct 264 ms 23828 KB Output is correct
38 Correct 76 ms 11608 KB Output is correct
39 Correct 74 ms 12024 KB Output is correct
40 Correct 85 ms 11744 KB Output is correct
41 Correct 57 ms 9516 KB Output is correct
42 Correct 56 ms 8516 KB Output is correct
43 Correct 64 ms 8544 KB Output is correct
44 Correct 54 ms 8816 KB Output is correct
45 Correct 155 ms 20864 KB Output is correct
46 Correct 81 ms 11332 KB Output is correct
47 Correct 98 ms 11732 KB Output is correct
48 Correct 78 ms 11504 KB Output is correct
49 Correct 55 ms 9668 KB Output is correct
50 Correct 49 ms 8748 KB Output is correct
51 Correct 55 ms 8872 KB Output is correct
52 Correct 53 ms 8496 KB Output is correct
53 Correct 204 ms 20932 KB Output is correct
54 Correct 75 ms 11456 KB Output is correct