Submission #242877

# Submission time Handle Problem Language Result Execution time Memory
242877 2020-06-29T15:18:16 Z ajpiano Pipes (BOI13_pipes) C++14
100 / 100
113 ms 17272 KB
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse4")

using namespace std;

#define FOR(a,b) for(int a=0;a<b;a++)
#define F0R(a,b,c) for(int a = b; a<=c; a++)
#define f first
#define s second
#define m0(x) memset(x,0,sizeof(x))

typedef pair<int,int> pi;
typedef long long ll;
typedef vector<int> vi;
typedef vector<pi> vpi;

int n,m;
const int large = 1e5;
vpi edges[large+1];
vpi parents;
vi found;
vi change;
vi ans;

bool ef = 0;
pair<int,pi> ge;

int dfs(int p,int child, int height){
    found[child] = height;
    int upedge = 0;
    int got = 0;
//    cerr << child << " " << found[child] << "\n";
    for(auto &a: edges[child]){
        if(a.f == p){
            upedge = a.s; continue;
        }
        if(!found[a.f]){
            got += dfs(child,a.f,height+1);
        }else if(found[a.f] < found[child]){
            int dist = found[child]-found[a.f];
            if(dist&1 || ef){
//                cerr << found[child] << " " << found[a.f] << " " << child << " " << a.f << " " << ge.s.f << " " << ge.s.s << "\n";
                cout << 0 << "\n";
                exit(0);
            }else{
                ef = 1;
                ge = {a.s,{child,a.f}};
            }
        }
    }
    parents[child] = {p,upedge};
    ans[upedge] = change[child]-got;
    return ans[upedge];
}

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin >> n >> m;
    if(m > n){
        cout << "0\n"; return 0;
    }
    parents.resize(n+1);
    found.resize(n+1);
    change.resize(n+1);
    ans.resize(m+1);
    F0R(i,1,n) cin >> change[i];
    F0R(i,1,m){
        int u,v; cin >> u >> v;
        edges[u].push_back({v,i});
        edges[v].push_back({u,i});
    }
    int rem = dfs(0,1,1);
    if(rem&1 || (rem != 0 && ef == 0)){
        cout << "0\n"; return 0;
    }
    if(ef){
        int add = rem/2;
        if((found[ge.s.s]-1)&1){
            add = -add;
        }
        ans[ge.f] = add;
        int ptr = ge.s.f;
        bool m1 = 1;
        for(; ptr != 1; ){
            if(ptr == ge.s.s) add *= 2;
            if(m1) ans[parents[ptr].s] -= add;
            else ans[parents[ptr].s] += add;
            ptr = parents[ptr].f;
            m1 = !m1;
        }
    }
    F0R(i,1,m) cout << 2*ans[i] << "\n";
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 6 ms 2688 KB Output is correct
2 Correct 6 ms 2688 KB Output is correct
3 Correct 6 ms 2816 KB Output is correct
4 Correct 91 ms 10360 KB Output is correct
5 Correct 6 ms 2688 KB Output is correct
6 Correct 6 ms 2688 KB Output is correct
7 Correct 6 ms 2688 KB Output is correct
8 Correct 6 ms 2688 KB Output is correct
9 Correct 6 ms 2816 KB Output is correct
10 Correct 6 ms 2816 KB Output is correct
11 Correct 6 ms 2816 KB Output is correct
12 Correct 7 ms 2816 KB Output is correct
13 Correct 62 ms 8824 KB Output is correct
14 Correct 78 ms 9952 KB Output is correct
15 Correct 84 ms 10488 KB Output is correct
16 Correct 85 ms 9208 KB Output is correct
17 Correct 80 ms 10488 KB Output is correct
18 Correct 88 ms 10568 KB Output is correct
19 Correct 87 ms 13688 KB Output is correct
20 Correct 6 ms 2688 KB Output is correct
21 Correct 7 ms 2816 KB Output is correct
22 Correct 106 ms 10492 KB Output is correct
23 Correct 77 ms 8824 KB Output is correct
24 Correct 84 ms 10360 KB Output is correct
25 Correct 78 ms 9080 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 2688 KB Output is correct
2 Correct 6 ms 2816 KB Output is correct
3 Correct 83 ms 12792 KB Output is correct
4 Correct 6 ms 2688 KB Output is correct
5 Correct 6 ms 2756 KB Output is correct
6 Correct 6 ms 2688 KB Output is correct
7 Correct 6 ms 2688 KB Output is correct
8 Correct 6 ms 2688 KB Output is correct
9 Correct 7 ms 2688 KB Output is correct
10 Correct 6 ms 2688 KB Output is correct
11 Correct 6 ms 2688 KB Output is correct
12 Correct 6 ms 2688 KB Output is correct
13 Correct 6 ms 2688 KB Output is correct
14 Correct 6 ms 2688 KB Output is correct
15 Correct 6 ms 2816 KB Output is correct
16 Correct 6 ms 2816 KB Output is correct
17 Correct 6 ms 2816 KB Output is correct
18 Correct 6 ms 2688 KB Output is correct
19 Correct 6 ms 2688 KB Output is correct
20 Correct 7 ms 2688 KB Output is correct
21 Correct 7 ms 2688 KB Output is correct
22 Correct 7 ms 2816 KB Output is correct
23 Correct 65 ms 12664 KB Output is correct
24 Correct 83 ms 14200 KB Output is correct
25 Correct 65 ms 12796 KB Output is correct
26 Correct 6 ms 2688 KB Output is correct
27 Correct 6 ms 2688 KB Output is correct
28 Correct 6 ms 2688 KB Output is correct
29 Correct 6 ms 2816 KB Output is correct
30 Correct 89 ms 16504 KB Output is correct
31 Correct 90 ms 16760 KB Output is correct
32 Correct 86 ms 11768 KB Output is correct
33 Correct 69 ms 13944 KB Output is correct
34 Correct 6 ms 2688 KB Output is correct
35 Correct 7 ms 2688 KB Output is correct
36 Correct 6 ms 2688 KB Output is correct
37 Correct 6 ms 2688 KB Output is correct
38 Correct 84 ms 15736 KB Output is correct
39 Correct 89 ms 11128 KB Output is correct
40 Correct 89 ms 13944 KB Output is correct
41 Correct 73 ms 16248 KB Output is correct
42 Correct 6 ms 2688 KB Output is correct
43 Correct 6 ms 2688 KB Output is correct
44 Correct 6 ms 2688 KB Output is correct
45 Correct 6 ms 2688 KB Output is correct
46 Correct 95 ms 17272 KB Output is correct
47 Correct 88 ms 14072 KB Output is correct
48 Correct 104 ms 16504 KB Output is correct
49 Correct 62 ms 10616 KB Output is correct
50 Correct 6 ms 2688 KB Output is correct
51 Correct 6 ms 2688 KB Output is correct
52 Correct 7 ms 2688 KB Output is correct
53 Correct 6 ms 2688 KB Output is correct
54 Correct 113 ms 15096 KB Output is correct