답안 #283430

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
283430 2020-08-25T18:16:06 Z Atill83 Pipes (BOI13_pipes) C++14
100 / 100
263 ms 27384 KB
#include <bits/stdc++.h>
#define ff first
#define ss second
#define endl '\n'
using namespace std;
const long long INF = (long long) 1e18;
const int mod = (int) 1e9+7;
const int MAXN = (int) 5e5+5;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll n, m;
pii edg[MAXN];
int c[MAXN];
vector<pii> adj[MAXN];
int sz[MAXN];
ll ans[MAXN];
bool vis[MAXN];
int remd;
void dfs(int v){
    sz[v]--;
    for(pii j: adj[v]){
        //cerr<<v<<" "<<j.ff<<endl;
        if(sz[j.ff] <= 0) continue;
        ans[j.ss] = 2*c[v];
        c[j.ff] -= c[v];
        c[v] = 0;
        sz[j.ff]--;
        remd--;
        if(sz[j.ff] == 1){
            dfs(j.ff);
        }
    }
}



int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);cout.tie(nullptr);

    #ifdef Local
        freopen("C:/Users/Admin/Desktop/Yazilim/C/IO/int.txt","r",stdin);
        freopen("C:/Users/Admin/Desktop/Yazilim/C/IO/out.txt","w",stdout);
    #endif

    cin>>n>>m;
    remd = m;

    for(int i = 0; i < n; i++){
        cin>>c[i + 1];
    }
    if(n == 2){
        if(c[1] == c[2]){
            cout<<c[1]<<endl;
        }else{
            cout<<0<<endl;
        }
        return 0;
    }
    for(int i = 0; i < m; i++){
        int a, b;
        cin>>a>>b;
        adj[a].push_back({b, i});
        adj[b].push_back({a, i});
        sz[a]++;
        sz[b]++;
    }

    for(int i = 1; i <= n; i++){
        if(sz[i] == 1){
            //cout<<i<<endl;
            dfs(i);
        }
    }
    int kal = 0;
    int bir = -1;
    for(int i = 1; i <= n; i++){
        //cout<<c[i]<<" ";
        if(sz[i]){
            kal++;
            bir = i;
        }
    }
    //cout<<endl<<kal<<endl;
    //cerr<<kal<<endl;
    if(kal == 0){
        bool tru = 1;
        for(int i = 1; i <= n; i++){
            tru = (tru && c[i] == 0);
        }
        if(tru){
            for(int i = 0; i < m; i++){
                cout<<ans[i]<<endl;
            }
        }else{
            cout<<0<<endl;
        }
    }else if(remd > kal || kal % 2 == 0){
        cout<<0<<endl;
    }else{

        int cur = bir;
        for(pii j: adj[cur]){
            if(sz[j.ff] != 0){
               cur = j.ff; 
            }
        }
        int idx = 0;
        int top = 0, last = -1;
        while(cur != bir){
            if(idx % 2){
                top -= c[cur];
            }else{
                top += c[cur];
            }
            idx++;
            
            for(pii nx: adj[cur]){
                if(nx.ff == last) continue;
                if(sz[nx.ff] > 0){
                    last = cur;
                    cur = nx.ff;
                
                    break;
                }
            }
        }
        //cerr<<top<<endl;
        for(pii j: adj[bir]){
            if(j.ff == last) continue; 
            if(sz[j.ff] > 0){
                if((top +c[bir] ) % 2 != 0){
                    cout<<0<<endl;
                    return 0;
                }
               int deg = (top + c[bir]) / 2;
               ans[j.ss] = 2*deg;
               cur = j.ff;
               c[j.ff] -= deg;
               break;
            }
        }
        last = bir;
        while(cur != bir){
            for(pii nx: adj[cur]){
                if(nx.ff == last) continue;
                if(sz[nx.ff] > 0){
                    ans[nx.ss] = 2*c[cur];
                    c[nx.ff] -= c[cur];
                    last = cur;
                    cur = nx.ff;

                    break;
                }
            }
        }
        for(int i = 1; i <= m; i++){
            cout<<ans[i - 1]<<endl;
        }
    }


    

    #ifdef Local
        cout<<endl<<fixed<<setprecision(2)<<1000.0 * clock() / CLOCKS_PER_SEC<< " milliseconds ";
    #endif
}
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 12160 KB Output is correct
2 Correct 9 ms 12288 KB Output is correct
3 Correct 9 ms 12160 KB Output is correct
4 Correct 79 ms 18296 KB Output is correct
5 Correct 8 ms 12160 KB Output is correct
6 Correct 8 ms 12160 KB Output is correct
7 Correct 8 ms 12160 KB Output is correct
8 Correct 8 ms 12160 KB Output is correct
9 Correct 9 ms 12160 KB Output is correct
10 Correct 9 ms 12160 KB Output is correct
11 Correct 9 ms 12160 KB Output is correct
12 Correct 9 ms 12288 KB Output is correct
13 Correct 60 ms 17144 KB Output is correct
14 Correct 74 ms 18040 KB Output is correct
15 Correct 82 ms 18424 KB Output is correct
16 Correct 66 ms 17528 KB Output is correct
17 Correct 85 ms 18296 KB Output is correct
18 Correct 86 ms 18424 KB Output is correct
19 Correct 88 ms 18808 KB Output is correct
20 Correct 8 ms 12160 KB Output is correct
21 Correct 9 ms 12160 KB Output is correct
22 Correct 90 ms 18352 KB Output is correct
23 Correct 81 ms 17144 KB Output is correct
24 Correct 98 ms 18424 KB Output is correct
25 Correct 66 ms 17404 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 12072 KB Output is correct
2 Correct 9 ms 12160 KB Output is correct
3 Correct 64 ms 17400 KB Output is correct
4 Correct 65 ms 17400 KB Output is correct
5 Correct 73 ms 17656 KB Output is correct
6 Correct 263 ms 27384 KB Output is correct
7 Correct 8 ms 12160 KB Output is correct
8 Correct 9 ms 12160 KB Output is correct
9 Correct 9 ms 12160 KB Output is correct
10 Correct 8 ms 12160 KB Output is correct
11 Correct 9 ms 12160 KB Output is correct
12 Correct 9 ms 12160 KB Output is correct
13 Correct 9 ms 12160 KB Output is correct
14 Correct 8 ms 12160 KB Output is correct
15 Correct 10 ms 12160 KB Output is correct
16 Correct 9 ms 12160 KB Output is correct
17 Correct 9 ms 12160 KB Output is correct
18 Correct 9 ms 12160 KB Output is correct
19 Correct 9 ms 12160 KB Output is correct
20 Correct 9 ms 12160 KB Output is correct
21 Correct 9 ms 12288 KB Output is correct
22 Correct 9 ms 12160 KB Output is correct
23 Correct 69 ms 16952 KB Output is correct
24 Correct 88 ms 18040 KB Output is correct
25 Correct 65 ms 17372 KB Output is correct
26 Correct 63 ms 17628 KB Output is correct
27 Correct 62 ms 17272 KB Output is correct
28 Correct 69 ms 17912 KB Output is correct
29 Correct 185 ms 25080 KB Output is correct
30 Correct 86 ms 19192 KB Output is correct
31 Correct 94 ms 17876 KB Output is correct
32 Correct 85 ms 18296 KB Output is correct
33 Correct 64 ms 17528 KB Output is correct
34 Correct 61 ms 17400 KB Output is correct
35 Correct 62 ms 17400 KB Output is correct
36 Correct 74 ms 17784 KB Output is correct
37 Correct 233 ms 27256 KB Output is correct
38 Correct 88 ms 18936 KB Output is correct
39 Correct 85 ms 18296 KB Output is correct
40 Correct 94 ms 18044 KB Output is correct
41 Correct 64 ms 17400 KB Output is correct
42 Correct 63 ms 17272 KB Output is correct
43 Correct 61 ms 17272 KB Output is correct
44 Correct 70 ms 17784 KB Output is correct
45 Correct 234 ms 25280 KB Output is correct
46 Correct 90 ms 19576 KB Output is correct
47 Correct 95 ms 18040 KB Output is correct
48 Correct 97 ms 17912 KB Output is correct
49 Correct 69 ms 17528 KB Output is correct
50 Correct 65 ms 17464 KB Output is correct
51 Correct 68 ms 17656 KB Output is correct
52 Correct 61 ms 17400 KB Output is correct
53 Correct 194 ms 25100 KB Output is correct
54 Correct 87 ms 19192 KB Output is correct