Submission #230467

# Submission time Handle Problem Language Result Execution time Memory
230467 2020-05-10T07:44:24 Z mehrdad_sohrabi Pipes (BOI13_pipes) C++14
100 / 100
293 ms 35428 KB
#include <bits/stdc++.h>
typedef long long int ll;
typedef long double ld;
#define pb push_back
#define pii pair < ll , ll >
#define F first
#define S second
#define endl '\n'
#define int long long
#define sync ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")
#define kill(x) return cout<<x<<'\n', 0;
using namespace std;
const int N=2e5+100;
vector <int> g[N];
ll hi[N];
ll ans[N];
map <pii,int> mp;
ll val[N],par[N],vis[N];
ll ras1=0,ras2=0;
ll dfsd(ll v,ll p,ll h){
   // cout << v << " " << p << " " << h << endl;
    par[v]=p;
    hi[v]=h;
    for (auto u : g[v]){
        if (u==p) continue;
        if (hi[u]<hi[v] && hi[u]!=0){
            ras1=v,ras2=u;
        //    cout << v << " " << u << endl;
            continue;
        }
        if (hi[u]!=0) continue;
        dfsd(u,v,h+1);
    }
}
ll dfs(ll v,ll p){
    for (auto u : g[v]){
        if (u==p || vis[u]) continue;
        dfs(u,v);
        val[v]+=-val[u];
        mp[{u,v}]=2*-val[u];
        mp[{v,u}]=mp[{u,v}];
        val[u]=0;
    }
}
vector <pii> yal;
int32_t main(){
    sync;
    ll n,m;
    cin >> n >> m;
    if (m>n) kill(0);
    for (int i=1;i<=n;i++){
        cin >> val[i];
        val[i]=-val[i];
    }
    for (int i=0;i<m;i++){
        ll u,v;
        cin >> u >> v;
        g[u].pb(v);
        g[v].pb(u);
        yal.pb({u,v});
    }
    if (m>n) kill(0);
    if (m==n-1){
        dfs(1,1);
        if (val[1]!=0) kill(0);
    }
    if (m==n){
        dfsd(1,1,1);
        vector <int> ras;

        while(ras1!=ras2){
            ras.pb(ras1);
            vis[ras1]=1;
            ras1=par[ras1];
        }
        ras.pb(ras2);
        vis[ras2]=1;
        for (int i=1;i<=n;i++){
            if (vis[i]){
                dfs(i,i);
            }
        }
        ll t=ras.size();
        if (t%2==0) kill(0);
        for (int i=1;i<t;i++){
            ll v=ras[i],u=ras[(i+1)%t];
            val[u]+=-val[v];
            mp[{u,v}]=2*-val[v];
            mp[{v,u}]=2*-val[v];
            val[v]=0;

        }
        ll o=val[ras[0]];
        if (o%2!=0) kill(0);
        o/=2;
        o=-o;
        for (int i=0;i<t;i++){
            ll v=ras[i],u=ras[(i+1)%t];
            if (i%2==0){
                mp[{u,v}]+=2*o;
                mp[{v,u}]+=2*o;
            }
            else{
                mp[{u,v}]-=2*o;
                mp[{v,u}]-=2*o;
            }
        }
    }
    for (pii v : yal){
        cout << mp[v] << endl;
    }
}

Compilation message

pipes.cpp: In function 'll dfsd(ll, ll, ll)':
pipes.cpp:36:1: warning: no return statement in function returning non-void [-Wreturn-type]
 }
 ^
pipes.cpp: In function 'll dfs(ll, ll)':
pipes.cpp:46:1: warning: no return statement in function returning non-void [-Wreturn-type]
 }
 ^
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5120 KB Output is correct
2 Correct 7 ms 5120 KB Output is correct
3 Correct 8 ms 5248 KB Output is correct
4 Correct 232 ms 24012 KB Output is correct
5 Correct 7 ms 5120 KB Output is correct
6 Correct 7 ms 5120 KB Output is correct
7 Correct 7 ms 5120 KB Output is correct
8 Correct 7 ms 5120 KB Output is correct
9 Correct 8 ms 5248 KB Output is correct
10 Correct 8 ms 5248 KB Output is correct
11 Correct 8 ms 5248 KB Output is correct
12 Correct 8 ms 5376 KB Output is correct
13 Correct 172 ms 20196 KB Output is correct
14 Correct 210 ms 23008 KB Output is correct
15 Correct 211 ms 24292 KB Output is correct
16 Correct 192 ms 21220 KB Output is correct
17 Correct 221 ms 24036 KB Output is correct
18 Correct 219 ms 24164 KB Output is correct
19 Correct 217 ms 31204 KB Output is correct
20 Correct 7 ms 5120 KB Output is correct
21 Correct 8 ms 5248 KB Output is correct
22 Correct 225 ms 24184 KB Output is correct
23 Correct 158 ms 20196 KB Output is correct
24 Correct 222 ms 24164 KB Output is correct
25 Correct 187 ms 20964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 5120 KB Output is correct
2 Correct 9 ms 5376 KB Output is correct
3 Correct 126 ms 21604 KB Output is correct
4 Correct 7 ms 4992 KB Output is correct
5 Correct 7 ms 4992 KB Output is correct
6 Correct 7 ms 4992 KB Output is correct
7 Correct 8 ms 5120 KB Output is correct
8 Correct 8 ms 5120 KB Output is correct
9 Correct 7 ms 5120 KB Output is correct
10 Correct 7 ms 4992 KB Output is correct
11 Correct 7 ms 4992 KB Output is correct
12 Correct 7 ms 4992 KB Output is correct
13 Correct 7 ms 4992 KB Output is correct
14 Correct 7 ms 5120 KB Output is correct
15 Correct 8 ms 5376 KB Output is correct
16 Correct 8 ms 5248 KB Output is correct
17 Correct 8 ms 5248 KB Output is correct
18 Correct 7 ms 5120 KB Output is correct
19 Correct 8 ms 4992 KB Output is correct
20 Correct 8 ms 4992 KB Output is correct
21 Correct 8 ms 5120 KB Output is correct
22 Correct 8 ms 5376 KB Output is correct
23 Correct 204 ms 25060 KB Output is correct
24 Correct 250 ms 29028 KB Output is correct
25 Correct 127 ms 21604 KB Output is correct
26 Correct 7 ms 5120 KB Output is correct
27 Correct 7 ms 4992 KB Output is correct
28 Correct 7 ms 4992 KB Output is correct
29 Correct 8 ms 4992 KB Output is correct
30 Correct 253 ms 33252 KB Output is correct
31 Correct 272 ms 30696 KB Output is correct
32 Correct 242 ms 27236 KB Output is correct
33 Correct 132 ms 21860 KB Output is correct
34 Correct 8 ms 5120 KB Output is correct
35 Correct 7 ms 4992 KB Output is correct
36 Correct 8 ms 4992 KB Output is correct
37 Correct 7 ms 4992 KB Output is correct
38 Correct 268 ms 31204 KB Output is correct
39 Correct 247 ms 26976 KB Output is correct
40 Correct 275 ms 28772 KB Output is correct
41 Correct 94 ms 19300 KB Output is correct
42 Correct 7 ms 4992 KB Output is correct
43 Correct 7 ms 4992 KB Output is correct
44 Correct 7 ms 4992 KB Output is correct
45 Correct 7 ms 4992 KB Output is correct
46 Correct 257 ms 35428 KB Output is correct
47 Correct 245 ms 29028 KB Output is correct
48 Correct 293 ms 30692 KB Output is correct
49 Correct 159 ms 23780 KB Output is correct
50 Correct 7 ms 4992 KB Output is correct
51 Correct 7 ms 5120 KB Output is correct
52 Correct 7 ms 4992 KB Output is correct
53 Correct 7 ms 4992 KB Output is correct
54 Correct 244 ms 32716 KB Output is correct