Submission #747563

# Submission time Handle Problem Language Result Execution time Memory
747563 2023-05-24T10:28:51 Z irmuun Pipes (BOI13_pipes) C++17
100 / 100
264 ms 40148 KB
#include<bits/stdc++.h>
 
using namespace std;
 
#define pb push_back
#define ll long long
#define ff first
#define ss second
#define all(s) s.begin(),s.end()
 
int main(){
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    ll n,m;
    cin>>n>>m;
    if(m>n){
        cout<<0;
        return 0;
    }
    map<pair<ll,ll>,ll>mp;
    set<ll>s[n+5];
    ll c[n+5],p[n+5];
    fill(p,p+n+1,0);
    for(ll i=1;i<=n;i++){
        cin>>c[i];
    }
    vector<pair<ll,ll>>edge;
    for(ll i=1;i<=m;i++){
        ll u,v;
        cin>>u>>v;
        edge.pb({u,v});
        s[u].insert(v);
        s[v].insert(u);
        p[u]++;
        p[v]++;
    }
    if(m==n-1){
        queue<ll>q;
        for(ll i=1;i<=n;i++){
            if(p[i]==1){
                q.push(i);
            }
        }
        for(ll i=1;i<n;i++){
            ll x=q.front();
            q.pop();
            if(p[x]==0){
                continue;
            }
            ll y=*s[x].begin();
            mp[{x,y}]=2*c[x];
            c[y]-=c[x];
            p[y]--;
            p[x]--;
            s[x].erase(y);
            s[y].erase(x);
            if(p[y]==1){
                q.push(y);
            }
        }
        if(c[q.front()]!=0){
            cout<<0;
            return 0;
        }
        for(auto [u,v]:edge){
            cout<<mp[{u,v}]+mp[{v,u}]<<"\n";
        }
    }
    else{
        queue<ll>q;
        ll used[n+5];
        fill(used,used+n+1,0);
        for(ll i=1;i<=n;i++){
            if(p[i]==1){
                q.push(i);
            }
        }
        ll cnt=n;
        while(!q.empty()){
            ll x=q.front();
            q.pop();
            if(used[x]==1){
                continue;
            }
            used[x]=1;
            cnt--;
            ll y=*s[x].begin();
            mp[{x,y}]=2*c[x];
            c[y]-=c[x];
            p[y]--;
            p[x]--;
            s[x].erase(y);
            s[y].erase(x);
            if(p[y]==1){
                q.push(y);
            }
        }
        if(cnt%2==0){
            cout<<0;
            return 0;
        }
        vector<ll>node;
        ll sum=0;
        function <void(ll)> dfs=[&](ll x){
            used[x]=1;
            node.pb(x);
            sum+=c[x];
            for(auto y:s[x]){
                if(used[y]==0){
                    dfs(y);
                }
            }
        };
        for(ll i=1;i<=n;i++){
            if(used[i]==0){
                dfs(i);
                break;
            }
        }
        sum/=2;
        ll sz=node.size();
        node.insert(node.end(),all(node));
        ll a[sz*2+5];
        a[0]=c[node[0]];
        a[1]=c[node[1]];
        for(ll i=2;i<sz*2;i++){
            a[i]=a[i-2]+c[node[i]];
        }
        for(ll i=0;i<sz;i++){
            mp[{node[i],node[i+1]}]=(sum-(a[i+(sz-1)]-a[i]))*2;
        }
        for(auto [u,v]:edge){
            cout<<mp[{u,v}]+mp[{v,u}]<<"\n";
        }
    }
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 2 ms 596 KB Output is correct
4 Correct 228 ms 23152 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 316 KB Output is correct
8 Correct 1 ms 320 KB Output is correct
9 Correct 2 ms 580 KB Output is correct
10 Correct 2 ms 596 KB Output is correct
11 Correct 2 ms 588 KB Output is correct
12 Correct 2 ms 596 KB Output is correct
13 Correct 173 ms 18516 KB Output is correct
14 Correct 215 ms 21948 KB Output is correct
15 Correct 223 ms 23252 KB Output is correct
16 Correct 161 ms 19712 KB Output is correct
17 Correct 226 ms 23128 KB Output is correct
18 Correct 209 ms 23204 KB Output is correct
19 Correct 221 ms 23096 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 584 KB Output is correct
22 Correct 264 ms 23216 KB Output is correct
23 Correct 163 ms 18396 KB Output is correct
24 Correct 198 ms 23256 KB Output is correct
25 Correct 191 ms 19396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 2 ms 596 KB Output is correct
3 Correct 96 ms 18968 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 316 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 316 KB Output is correct
13 Correct 1 ms 324 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 716 KB Output is correct
16 Correct 1 ms 584 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 0 ms 340 KB Output is correct
22 Correct 1 ms 596 KB Output is correct
23 Correct 156 ms 29192 KB Output is correct
24 Correct 248 ms 33900 KB Output is correct
25 Correct 101 ms 18936 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 0 ms 328 KB Output is correct
30 Correct 218 ms 31804 KB Output is correct
31 Correct 249 ms 40148 KB Output is correct
32 Correct 248 ms 27216 KB Output is correct
33 Correct 86 ms 20156 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Correct 0 ms 340 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
38 Correct 211 ms 32568 KB Output is correct
39 Correct 230 ms 25880 KB Output is correct
40 Correct 209 ms 33084 KB Output is correct
41 Correct 76 ms 19964 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 1 ms 328 KB Output is correct
45 Correct 1 ms 328 KB Output is correct
46 Correct 205 ms 31476 KB Output is correct
47 Correct 206 ms 33268 KB Output is correct
48 Correct 229 ms 39388 KB Output is correct
49 Correct 121 ms 19132 KB Output is correct
50 Correct 1 ms 340 KB Output is correct
51 Correct 1 ms 340 KB Output is correct
52 Correct 1 ms 336 KB Output is correct
53 Correct 0 ms 340 KB Output is correct
54 Correct 206 ms 30620 KB Output is correct