Submission #255617

# Submission time Handle Problem Language Result Execution time Memory
255617 2020-08-01T12:40:18 Z shayan_p Pipes (BOI13_pipes) C++14
100 / 100
76 ms 13944 KB
// And you curse yourself for things you never done

#include<bits/stdc++.h>

#define F first
#define S second
#define PB push_back
#define sz(s) int((s).size())
#define bit(n,k) (((n)>>(k))&1)

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll, ll> pll;

const int maxn = 1e6 + 10, mod = 1e9 + 7, inf = 1e9 + 10;

ll c[maxn], ans[maxn];
pll p[maxn];
int ID;

int tp[maxn], to[maxn], nxt[maxn];

void add_edge(int a, int b){
    static int C = 0;
    nxt[C] = tp[a], tp[a] = C, to[C] = b, C++;
}

int mark[maxn];

pll operator - (pll a, pll b){
    return {a.F - b.F, a.S - b.S};
}

void dfs(int u, int par = -1){
    p[u].F = c[u];
    mark[u] = 1;
    for(int id = tp[u]; id != -1; id = nxt[id]){
	if(mark[to[id]] == 1 && to[id] != par){
	    if(ID == -1)
		ID = id >> 1;
	    if(ID != (id >> 1))
		cout << 0 << endl, exit(0);
	    p[u].S--;
	}
	else if(mark[to[id]] != 1){
	    dfs(to[id], u);
	    p[u]= p[u] - p[to[id]];
	}
    }
}
void go(int u){
    mark[u] = 2;
    for(int id = tp[u]; id != -1; id = nxt[id]){
	if(mark[to[id]] != 2){
	    go(to[id]);
	    ans[id >> 1] = p[to[id]].F + 1ll * (ID == -1 ? 0 : ans[ID]) * p[to[id]].S;
	}
    }
}

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

    memset(tp, -1, sizeof tp);
    
    int n, m;
    cin >> n >> m;

    if(m > n)
	return cout << 0 << endl, 0;
    
    for(int i = 1; i <= n; i++){
	cin >> c[i];
    }
    for(int i = 0; i < m; i++){
	int a, b;
	cin >> a >> b;
	add_edge(a, b);
	add_edge(b, a);
    }
    for(int i = 1; i <= n; i++){
	if(mark[i] == 0){
	    ID = -1;
	    dfs(i);
	    if(ID == -1){
		if(! (p[i].F == 0 && p[i].S == 0) )
		    return cout << 0 << endl, 0;
	    }
	    else{
		if(p[i].S == 0)
		    return cout << 0 << endl, 0;
		if(p[i].F % p[i].S != 0)
		    return cout << 0 << endl, 0;
		ans[ID] = -(p[i].F / p[i].S);
	    }
	    go(i);
	}
    }
    for(int i = 0; i < m; i++){
	cout << 2ll * ans[i] << "\n";
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4352 KB Output is correct
2 Correct 2 ms 4352 KB Output is correct
3 Correct 3 ms 4384 KB Output is correct
4 Correct 57 ms 10132 KB Output is correct
5 Correct 3 ms 4352 KB Output is correct
6 Correct 2 ms 4352 KB Output is correct
7 Correct 3 ms 4352 KB Output is correct
8 Correct 2 ms 4352 KB Output is correct
9 Correct 3 ms 4352 KB Output is correct
10 Correct 3 ms 4352 KB Output is correct
11 Correct 4 ms 4352 KB Output is correct
12 Correct 3 ms 4352 KB Output is correct
13 Correct 45 ms 8952 KB Output is correct
14 Correct 54 ms 9848 KB Output is correct
15 Correct 67 ms 10184 KB Output is correct
16 Correct 48 ms 9336 KB Output is correct
17 Correct 57 ms 10144 KB Output is correct
18 Correct 54 ms 10104 KB Output is correct
19 Correct 65 ms 12540 KB Output is correct
20 Correct 3 ms 4352 KB Output is correct
21 Correct 3 ms 4352 KB Output is correct
22 Correct 54 ms 10104 KB Output is correct
23 Correct 42 ms 9080 KB Output is correct
24 Correct 54 ms 10172 KB Output is correct
25 Correct 44 ms 9208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4352 KB Output is correct
2 Correct 3 ms 4352 KB Output is correct
3 Correct 43 ms 10360 KB Output is correct
4 Correct 3 ms 4224 KB Output is correct
5 Correct 3 ms 4224 KB Output is correct
6 Correct 2 ms 4224 KB Output is correct
7 Correct 2 ms 4352 KB Output is correct
8 Correct 2 ms 4352 KB Output is correct
9 Correct 3 ms 4352 KB Output is correct
10 Correct 2 ms 4224 KB Output is correct
11 Correct 3 ms 4224 KB Output is correct
12 Correct 3 ms 4224 KB Output is correct
13 Correct 3 ms 4224 KB Output is correct
14 Correct 2 ms 4352 KB Output is correct
15 Correct 3 ms 4352 KB Output is correct
16 Correct 3 ms 4352 KB Output is correct
17 Correct 3 ms 4352 KB Output is correct
18 Correct 2 ms 4224 KB Output is correct
19 Correct 4 ms 4224 KB Output is correct
20 Correct 3 ms 4224 KB Output is correct
21 Correct 2 ms 4224 KB Output is correct
22 Correct 3 ms 4352 KB Output is correct
23 Correct 52 ms 11256 KB Output is correct
24 Correct 57 ms 12280 KB Output is correct
25 Correct 43 ms 10384 KB Output is correct
26 Correct 3 ms 4224 KB Output is correct
27 Correct 2 ms 4224 KB Output is correct
28 Correct 3 ms 4224 KB Output is correct
29 Correct 2 ms 4224 KB Output is correct
30 Correct 71 ms 13812 KB Output is correct
31 Correct 59 ms 13944 KB Output is correct
32 Correct 59 ms 10616 KB Output is correct
33 Correct 47 ms 11256 KB Output is correct
34 Correct 3 ms 4224 KB Output is correct
35 Correct 3 ms 4224 KB Output is correct
36 Correct 2 ms 4224 KB Output is correct
37 Correct 2 ms 4224 KB Output is correct
38 Correct 58 ms 12024 KB Output is correct
39 Correct 62 ms 10232 KB Output is correct
40 Correct 62 ms 12152 KB Output is correct
41 Correct 48 ms 12664 KB Output is correct
42 Correct 2 ms 4224 KB Output is correct
43 Correct 3 ms 4224 KB Output is correct
44 Correct 2 ms 4224 KB Output is correct
45 Correct 3 ms 4224 KB Output is correct
46 Correct 76 ms 12792 KB Output is correct
47 Correct 59 ms 12280 KB Output is correct
48 Correct 68 ms 13820 KB Output is correct
49 Correct 44 ms 8952 KB Output is correct
50 Correct 3 ms 4224 KB Output is correct
51 Correct 3 ms 4352 KB Output is correct
52 Correct 2 ms 4224 KB Output is correct
53 Correct 2 ms 4352 KB Output is correct
54 Correct 61 ms 12920 KB Output is correct