답안 #747554

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
747554 2023-05-24T10:15:34 Z irmuun Pipes (BOI13_pipes) C++17
74.0741 / 100
256 ms 40012 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]);
        }
        for(auto [u,v]:edge){
            cout<<mp[{u,v}]+mp[{v,u}]<<"\n";
        }
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 596 KB Output is correct
4 Correct 223 ms 23248 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 320 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 580 KB Output is correct
10 Correct 2 ms 596 KB Output is correct
11 Correct 1 ms 588 KB Output is correct
12 Correct 2 ms 596 KB Output is correct
13 Correct 155 ms 18488 KB Output is correct
14 Correct 196 ms 21916 KB Output is correct
15 Correct 215 ms 23260 KB Output is correct
16 Correct 167 ms 19796 KB Output is correct
17 Correct 256 ms 23156 KB Output is correct
18 Correct 207 ms 23276 KB Output is correct
19 Correct 233 ms 23020 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 584 KB Output is correct
22 Correct 221 ms 23248 KB Output is correct
23 Correct 152 ms 18512 KB Output is correct
24 Correct 220 ms 23216 KB Output is correct
25 Correct 166 ms 19324 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 340 KB Output isn't correct
2 Incorrect 1 ms 716 KB Output isn't correct
3 Correct 90 ms 18940 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Incorrect 1 ms 340 KB Output isn't correct
8 Incorrect 0 ms 340 KB Output isn't correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Incorrect 0 ms 340 KB Output isn't correct
15 Incorrect 1 ms 724 KB Output isn't correct
16 Incorrect 1 ms 596 KB Output isn't correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 0 ms 332 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Incorrect 1 ms 596 KB Output isn't correct
23 Incorrect 172 ms 29376 KB Output isn't correct
24 Incorrect 223 ms 33844 KB Output isn't correct
25 Correct 101 ms 19036 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 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Incorrect 241 ms 31836 KB Output isn't correct
31 Incorrect 230 ms 40012 KB Output isn't correct
32 Incorrect 212 ms 27160 KB Output isn't correct
33 Correct 96 ms 20172 KB Output is correct
34 Correct 0 ms 340 KB Output is correct
35 Correct 0 ms 336 KB Output is correct
36 Correct 0 ms 336 KB Output is correct
37 Correct 1 ms 332 KB Output is correct
38 Incorrect 221 ms 32624 KB Output isn't correct
39 Incorrect 217 ms 25824 KB Output isn't correct
40 Incorrect 229 ms 33112 KB Output isn't correct
41 Correct 72 ms 20024 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 340 KB Output is correct
45 Correct 0 ms 340 KB Output is correct
46 Incorrect 250 ms 31524 KB Output isn't correct
47 Incorrect 222 ms 33444 KB Output isn't correct
48 Incorrect 246 ms 39460 KB Output isn't correct
49 Correct 129 ms 19116 KB Output is correct
50 Correct 0 ms 332 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 Incorrect 228 ms 30576 KB Output isn't correct