Submission #383225

# Submission time Handle Problem Language Result Execution time Memory
383225 2021-03-29T08:51:28 Z fammo Pipes (BOI13_pipes) C++11
96.1111 / 100
186 ms 26212 KB
#include <bits/stdc++.h>

using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;
#define X first
#define Y second
#define pb push_back
#define fastio ios_base::sync_with_stdio(0);cin.tie(nullptr);cout.tie(nullptr);
#define rndom mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())

#define endl '\n'
#define int long long
const int  N = 100 * 1000 + 20, L = 20, M = 100 * 1000 + 20;////////////////ya5e5
int n, m, c[N], ans[M], a[N], h[N], par[N];
vector<int>adj[N], ind[N];
int edg[N];
void dfst(int v, int p){
    int id = -1;
    for(int i = 0; i < (int)adj[v].size(); i ++){
        int u = adj[v][i], ed = ind[v][i];
        if(u == p){id = ed; continue;}
        dfst(u, v);
        a[v] -= ans[ed];
    }
    if(id!= -1)ans[id] = a[v], a[v] = 0;
}
void solvetree(){
    dfst(0, -1);
    for(int i = 0; i < n; i ++)if(a[i] != 0){cout << 0 << endl; exit(0);}
    for(int i = 0; i < m; i ++)cout << ans[i] << endl;
    exit(0);
}
bool mark[N], dead[M], cyc[M], seen[N];
int S = -1, sz = 0;
void markcyc(int v, int u){//cyc u --- v(-backedge-)u
    if(h[u] < h[v])swap(u,v);
    seen[v] = 1;
    while(u != v){
        seen[u] = 1;
        cyc[edg[u]] = 1;
        u = par[u];
    }return;
}
void pre(int v, int id = -1){
    mark[v] = 1;
    for(int i = 0; i < (int)adj[v].size() ; i ++){
        int u = adj[v][i], ed = ind[v][i];
        //cout << "ADJ " << u << " VIA " << ed << endl;
        if(ed == id)continue;
        if(!mark[u]){
            par[u] = v;
            h[u] = h[v] + 1;
            pre(u, ed);
        }
    }
    edg[v] = id;
}
void findcyc(int v, int id = -1){
   // cout << "HERE " << v << " FROM " << id << endl;
    mark[v] = 1;
    for(int i = 0; i < (int)adj[v].size() ; i ++){
        int u = adj[v][i], ed = ind[v][i];
        //cout << "ADJ " << u << " VIA " << ed << endl;
        if(ed == id)continue;
        if(mark[u]){
            if((h[v] - h[u])%2){
               // cout << "h" <<v << " -h" << u << " = " << h[v] -h[u] << endl;
                cout << 0 << endl;
                exit(0);
            }
            cyc[ed] = 1;
            S=v;sz = abs(h[u]-h[v])+1;
            markcyc(v,u);
        }else{
            par[u] = v;
            h[u] = h[v] + 1;
            findcyc(u, ed);
        }
    }
    return;
}
void dfs(int v, int id = -1){
    mark[v] = 1;
    for(int i = 0; i < (int)adj[v].size(); i ++){
        int u = adj[v][i], ed = ind[v][i];
        if(!mark[u])dfs(u,ed);
        a[v] -= ans[ed];
    }
    if(id != -1 && !seen[v])ans[id] = a[v], a[v] = 0;
    return;
}
int32_t main(){
    fastio;
    ///auto t = clock();
    cin >> n >> m;
    if(m > n) return cout << 0 << endl, 0;
    for(int i = 0; i < n; i ++)cin >> c[i], a[i] = c[i]*2;
    for(int i = 0; i < m; i ++){
        int u, v; cin >> u >> v;
        u --; v --;
        adj[u].pb(v), adj[v].pb(u);
        ind[u].pb(i), ind[v].pb(i);
    }
    if(m == n - 1) solvetree();
    ////////////////////////////////////////////////////////////////////////////////
    /// \brief findcyc
    pre(0);
    for(int i = 0; i < n; i ++)mark[i] = 0;
    findcyc(0);int root = 0;
    for(int i = 0;i < n; i ++){mark[i] = 0; if((int)adj[i].size() != 1)root = i;}
    dfs(root);

    vector<int>vec;
    int cur = S, pr = -1;
    for(int t = 0; t < sz; t ++){
        for(int i = 0; i < (int)adj[cur].size(); i ++){
            int u = adj[cur][i], ed =ind[cur][i];
            if(cyc[ed] && u != pr){
                vec.pb(ed);
                if(pr == -1){
                    ans[ed] = 0;
                }else{
                    ans[ed] = a[cur];
                    a[cur]=0;
                    a[u]-=ans[ed];
                }pr= cur; cur = u;goto hell;
            }
        }
        hell:;
    }
    //cout << "S : " << S << ": " << a[S] << endl;
    int df = a[S]/2;
    ans[vec[0]] = df;
    for(int i = 1; i < (int)vec.size(); i ++){ans[vec[i]] -= df; df*=-1;}

    for(int i = 0; i < m; i ++)cout << ans[i] << '\n';
    ///cout << clock() - t << "ms" << endl;
    return 0;

}

/*13 13
3 7 6 8 -2 -4 12 -5 17 -6 1 0 3
1 2
2 3
3 5
5 9
9 8
5 13
13 10
13 7
7 11
11 12
11 6
7 4
4 3*/
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 5 ms 5228 KB Output is correct
4 Correct 109 ms 15340 KB Output is correct
5 Correct 4 ms 5100 KB Output is correct
6 Correct 4 ms 5100 KB Output is correct
7 Correct 4 ms 5100 KB Output is correct
8 Correct 4 ms 5100 KB Output is correct
9 Correct 5 ms 5100 KB Output is correct
10 Correct 5 ms 5228 KB Output is correct
11 Correct 5 ms 5228 KB Output is correct
12 Correct 5 ms 5228 KB Output is correct
13 Correct 85 ms 13164 KB Output is correct
14 Correct 102 ms 14700 KB Output is correct
15 Correct 113 ms 15340 KB Output is correct
16 Correct 91 ms 13804 KB Output is correct
17 Correct 111 ms 15340 KB Output is correct
18 Correct 114 ms 15348 KB Output is correct
19 Correct 116 ms 18028 KB Output is correct
20 Correct 4 ms 5100 KB Output is correct
21 Correct 5 ms 5228 KB Output is correct
22 Correct 112 ms 15340 KB Output is correct
23 Correct 81 ms 13164 KB Output is correct
24 Correct 113 ms 15468 KB Output is correct
25 Correct 88 ms 13696 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 5100 KB Output is correct
2 Correct 5 ms 5356 KB Output is correct
3 Correct 108 ms 19564 KB Output is correct
4 Correct 4 ms 5100 KB Output is correct
5 Correct 4 ms 5100 KB Output is correct
6 Correct 4 ms 5100 KB Output is correct
7 Correct 4 ms 5100 KB Output is correct
8 Correct 4 ms 5100 KB Output is correct
9 Correct 4 ms 5100 KB Output is correct
10 Correct 4 ms 5100 KB Output is correct
11 Correct 4 ms 5100 KB Output is correct
12 Correct 4 ms 5100 KB Output is correct
13 Correct 4 ms 5100 KB Output is correct
14 Correct 4 ms 5100 KB Output is correct
15 Correct 5 ms 5356 KB Output is correct
16 Correct 5 ms 5228 KB Output is correct
17 Correct 5 ms 5228 KB Output is correct
18 Correct 4 ms 5100 KB Output is correct
19 Correct 4 ms 5100 KB Output is correct
20 Correct 4 ms 5100 KB Output is correct
21 Correct 4 ms 5100 KB Output is correct
22 Incorrect 5 ms 5228 KB Output isn't correct
23 Correct 130 ms 20460 KB Output is correct
24 Correct 168 ms 22756 KB Output is correct
25 Correct 111 ms 19564 KB Output is correct
26 Correct 4 ms 5100 KB Output is correct
27 Correct 4 ms 5100 KB Output is correct
28 Correct 4 ms 5100 KB Output is correct
29 Correct 5 ms 5120 KB Output is correct
30 Correct 178 ms 25320 KB Output is correct
31 Correct 180 ms 25952 KB Output is correct
32 Correct 158 ms 19624 KB Output is correct
33 Correct 113 ms 20972 KB Output is correct
34 Correct 4 ms 5100 KB Output is correct
35 Correct 4 ms 5100 KB Output is correct
36 Correct 4 ms 5100 KB Output is correct
37 Correct 4 ms 5100 KB Output is correct
38 Correct 186 ms 24420 KB Output is correct
39 Incorrect 159 ms 19052 KB Output isn't correct
40 Incorrect 170 ms 22504 KB Output isn't correct
41 Correct 123 ms 23660 KB Output is correct
42 Correct 5 ms 5100 KB Output is correct
43 Correct 4 ms 5100 KB Output is correct
44 Correct 4 ms 5100 KB Output is correct
45 Correct 4 ms 5100 KB Output is correct
46 Correct 178 ms 26212 KB Output is correct
47 Correct 173 ms 22632 KB Output is correct
48 Correct 182 ms 25576 KB Output is correct
49 Correct 105 ms 17132 KB Output is correct
50 Correct 4 ms 5100 KB Output is correct
51 Correct 4 ms 5100 KB Output is correct
52 Correct 4 ms 5100 KB Output is correct
53 Correct 4 ms 5100 KB Output is correct
54 Correct 178 ms 23656 KB Output is correct