Submission #631404

# Submission time Handle Problem Language Result Execution time Memory
631404 2022-08-18T05:37:16 Z socpite Pipes (BOI13_pipes) C++17
75.3704 / 100
133 ms 131072 KB
#include<bits/stdc++.h>
using namespace std;

#define f first
#define s second

const int maxn = 1e5+5;

typedef long long ll;

ll lim = 1;

int _s, _t;

int n, m;
vector<int> idx, vert;

int vis[maxn], oncyc[maxn];
ll val[maxn], ans[maxn], A[maxn];
vector<pair<int, int>> graph[maxn];

void find_ep(int x, int p){
    assert(!vis[x]);
    vis[x]=1;
    for(auto v: graph[x]){
        if(v.f == p)continue;
        if(vis[v.f]){
            if(!_s){
                _s = x;
                _t = v.f;
                idx.push_back(v.s);
            }
        }
        else find_ep(v.f, x);
    }
}

void find_cyc(int x, int p){
    if(x == _t)oncyc[x]=1;
    else{
        for(auto v: graph[x]){
            if(v.f == p || (x == _s && v.f == _t) || (x == _t && v.f == _s))continue;
            find_cyc(v.f, x);
            if(oncyc[v.f]){
                idx.push_back(v.s);
                oncyc[x]=1;
                break;
            }
        }
    }
    if(oncyc[x])vert.push_back(x);
}

void solve(int x, int p){
    for(auto v: graph[x]){
        if(v.f == p || oncyc[v.f])continue;
        solve(v.f, p);
        ans[v.s]=val[v.f];
        val[x]-=val[v.f];
    }
}

void dfs(int x, int p){
    //cout << x << endl;
    for(auto v: graph[x]){
        if(v.f == p)continue;
        dfs(v.f, x);
        ans[v.s]=val[v.f];
        val[x]-=val[v.f];
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m;
    if(m > n){
        cout << 0;
        return 0;
    }
    for(int i = 1; i <= n; i++)cin >> val[i];
    for(int i = 1; i <= m; i++){
        int a, b;
        cin >> a >> b;
        graph[a].push_back({b, i});
        graph[b].push_back({a, i});
    }
    if(m == n){
        find_ep(1, 0);
        find_cyc(_s, 0);
        if(!(vert.size()&1)){
            cout << 0;
            return 0;
        }
        assert(vert.size() == idx.size());
        ll sum = 0;
        for(auto v: vert){
            solve(v, 0);
            sum += val[v];
        }
        sum/=2;
        for(int i = 1; i < vert.size(); i+=2)sum-=val[vert[i]];
        ans[idx[0]] = sum;
        for(int i = 1; i < idx.size(); i++)ans[idx[i]] = val[vert[i-1]]-ans[idx[i-1]];
    }
    else{
        dfs(1, 0);
    }
    for(int i = 1; i <= m; i++)cout << ans[i]*2 << "\n";

}

Compilation message

pipes.cpp: In function 'int main()':
pipes.cpp:103:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  103 |         for(int i = 1; i < vert.size(); i+=2)sum-=val[vert[i]];
      |                        ~~^~~~~~~~~~~~~
pipes.cpp:105:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  105 |         for(int i = 1; i < idx.size(); i++)ans[idx[i]] = val[vert[i-1]]-ans[idx[i-1]];
      |                        ~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 48 ms 8360 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 1 ms 2644 KB Output is correct
7 Correct 1 ms 2644 KB Output is correct
8 Correct 1 ms 2644 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 2 ms 2644 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 3 ms 2772 KB Output is correct
13 Correct 46 ms 7228 KB Output is correct
14 Correct 44 ms 8048 KB Output is correct
15 Correct 49 ms 8448 KB Output is correct
16 Correct 43 ms 7436 KB Output is correct
17 Correct 52 ms 8300 KB Output is correct
18 Correct 53 ms 8348 KB Output is correct
19 Correct 56 ms 11748 KB Output is correct
20 Correct 2 ms 2644 KB Output is correct
21 Correct 2 ms 2644 KB Output is correct
22 Correct 48 ms 8304 KB Output is correct
23 Correct 39 ms 7212 KB Output is correct
24 Correct 55 ms 8312 KB Output is correct
25 Correct 40 ms 7380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 64 ms 131072 KB Execution killed with signal 9
2 Runtime error 68 ms 131072 KB Execution killed with signal 9
3 Correct 60 ms 11460 KB Output is correct
4 Correct 3 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 1 ms 2644 KB Output is correct
7 Runtime error 61 ms 131072 KB Execution killed with signal 9
8 Correct 2 ms 2644 KB Output is correct
9 Correct 1 ms 2644 KB Output is correct
10 Correct 1 ms 2644 KB Output is correct
11 Correct 1 ms 2644 KB Output is correct
12 Correct 1 ms 2644 KB Output is correct
13 Correct 2 ms 2600 KB Output is correct
14 Runtime error 75 ms 131072 KB Execution killed with signal 9
15 Runtime error 72 ms 131072 KB Execution killed with signal 9
16 Runtime error 61 ms 131072 KB Execution killed with signal 9
17 Correct 2 ms 2772 KB Output is correct
18 Correct 2 ms 2644 KB Output is correct
19 Correct 1 ms 2644 KB Output is correct
20 Correct 2 ms 2608 KB Output is correct
21 Correct 1 ms 2644 KB Output is correct
22 Runtime error 57 ms 131072 KB Execution killed with signal 9
23 Runtime error 103 ms 131072 KB Execution killed with signal 9
24 Runtime error 105 ms 131072 KB Execution killed with signal 9
25 Correct 49 ms 11416 KB Output is correct
26 Correct 1 ms 2644 KB Output is correct
27 Correct 2 ms 2644 KB Output is correct
28 Correct 2 ms 2644 KB Output is correct
29 Correct 1 ms 2644 KB Output is correct
30 Runtime error 133 ms 131072 KB Execution killed with signal 9
31 Runtime error 117 ms 131072 KB Execution killed with signal 9
32 Runtime error 101 ms 131072 KB Execution killed with signal 9
33 Correct 58 ms 12504 KB Output is correct
34 Correct 1 ms 2644 KB Output is correct
35 Correct 2 ms 2644 KB Output is correct
36 Correct 2 ms 2644 KB Output is correct
37 Correct 1 ms 2644 KB Output is correct
38 Runtime error 120 ms 131072 KB Execution killed with signal 9
39 Runtime error 102 ms 131072 KB Execution killed with signal 9
40 Runtime error 103 ms 131072 KB Execution killed with signal 9
41 Correct 81 ms 15204 KB Output is correct
42 Correct 2 ms 2644 KB Output is correct
43 Correct 1 ms 2644 KB Output is correct
44 Correct 2 ms 2644 KB Output is correct
45 Correct 1 ms 2644 KB Output is correct
46 Runtime error 110 ms 131072 KB Execution killed with signal 9
47 Runtime error 110 ms 131072 KB Execution killed with signal 9
48 Runtime error 110 ms 131072 KB Execution killed with signal 9
49 Correct 52 ms 8908 KB Output is correct
50 Correct 2 ms 2644 KB Output is correct
51 Correct 2 ms 2644 KB Output is correct
52 Correct 1 ms 2644 KB Output is correct
53 Correct 1 ms 2644 KB Output is correct
54 Runtime error 103 ms 131072 KB Execution killed with signal 9