Submission #285734

# Submission time Handle Problem Language Result Execution time Memory
285734 2020-08-29T14:09:10 Z kostia244 Pipes (BOI13_pipes) C++17
100 / 100
263 ms 39684 KB
    #pragma GCC optimize("Ofast")
    #include<bits/stdc++.h>
    #define all(v) (v).begin(),(v).end()
    using ll = long long;
    using namespace std;
    const int maxn = 1<<19;
    int n, m;
    vector<array<ll, 2>> g[maxn];
    ll val[maxn];
    ll vis[maxn], C[maxn], h[maxn], L = -1, R, ID= -69, bb;
    ll s = 0;
    void dfs(int v, int p) {
    	ll cur = C[v];
    	vis[v] = 1;
    	if(h[v]&1) s -= C[v];
    	else s += C[v];
    	for(auto &[i, id] : g[v]) if(i != p) {
    		if(!vis[i]) {
    			h[i] = h[v]+1;
    			dfs(i, v);
    		} else if(h[i] < h[v]) {
    			if(L != -1 || (h[i]&1) != (h[v]&1)) {cout << "0\n", exit(0);}
    			L = i, R = v, ID = id, bb = h[v]&1;
    		}
    	}
    }
    ll recover(int v, int p) {
    	ll cur = C[v];
    	for(auto [i, id] : g[v]) if(i != p && id != ID) {
    		ll t = recover(i, v);
    		val[id] = t;
    		cur += t;
    	}
    	return -cur;
    }
    signed main() {
    	cin.tie(0)->sync_with_stdio(0);
    	cin >> n >> m;
    	for(int i = 1; i <= n; i++) cin >> C[i];
    	for(int f, t, i = 0; i < m; i++) {
    		cin >> f >> t;
    		g[f].push_back({t, i});
    		g[t].push_back({f, i});
    	}
    	dfs(1, 1);
    	if(L != -1) {
    		//cout << ID << ":\n";
    		if(bb) s *= -1;
    		C[L] -= s/2;
    		C[R] -= s/2;
    		val[ID] = (-s)/2;
    	}
    	recover(1, 1);
    	for(int i = 0; i < m; i++) cout << -2ll*val[i] << "\n"; cout << endl;
    }
    //?VK
    //??V
    //K?

Compilation message

pipes.cpp: In function 'void dfs(int, int)':
pipes.cpp:13:9: warning: unused variable 'cur' [-Wunused-variable]
   13 |      ll cur = C[v];
      |         ^~~
pipes.cpp: In function 'int main()':
pipes.cpp:54:6: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
   54 |      for(int i = 0; i < m; i++) cout << -2ll*val[i] << "\n"; cout << endl;
      |      ^~~
pipes.cpp:54:62: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
   54 |      for(int i = 0; i < m; i++) cout << -2ll*val[i] << "\n"; cout << endl;
      |                                                              ^~~~
# Verdict Execution time Memory Grader output
1 Correct 9 ms 12672 KB Output is correct
2 Correct 9 ms 12672 KB Output is correct
3 Correct 11 ms 12800 KB Output is correct
4 Correct 102 ms 21368 KB Output is correct
5 Correct 9 ms 12672 KB Output is correct
6 Correct 10 ms 12672 KB Output is correct
7 Correct 9 ms 12672 KB Output is correct
8 Correct 10 ms 12672 KB Output is correct
9 Correct 10 ms 12800 KB Output is correct
10 Correct 10 ms 12800 KB Output is correct
11 Correct 11 ms 12800 KB Output is correct
12 Correct 10 ms 12800 KB Output is correct
13 Correct 72 ms 19576 KB Output is correct
14 Correct 93 ms 21008 KB Output is correct
15 Correct 98 ms 21368 KB Output is correct
16 Correct 81 ms 20088 KB Output is correct
17 Correct 97 ms 21464 KB Output is correct
18 Correct 103 ms 21372 KB Output is correct
19 Correct 109 ms 24568 KB Output is correct
20 Correct 9 ms 12672 KB Output is correct
21 Correct 10 ms 12800 KB Output is correct
22 Correct 94 ms 21360 KB Output is correct
23 Correct 73 ms 19576 KB Output is correct
24 Correct 95 ms 21372 KB Output is correct
25 Correct 83 ms 20024 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 12672 KB Output is correct
2 Correct 10 ms 12800 KB Output is correct
3 Correct 77 ms 21752 KB Output is correct
4 Correct 85 ms 23728 KB Output is correct
5 Correct 61 ms 19960 KB Output is correct
6 Correct 261 ms 39684 KB Output is correct
7 Correct 11 ms 12672 KB Output is correct
8 Correct 9 ms 12672 KB Output is correct
9 Correct 9 ms 12672 KB Output is correct
10 Correct 9 ms 12672 KB Output is correct
11 Correct 9 ms 12672 KB Output is correct
12 Correct 9 ms 12672 KB Output is correct
13 Correct 9 ms 12672 KB Output is correct
14 Correct 10 ms 12672 KB Output is correct
15 Correct 10 ms 12800 KB Output is correct
16 Correct 10 ms 12800 KB Output is correct
17 Correct 11 ms 12800 KB Output is correct
18 Correct 10 ms 12800 KB Output is correct
19 Correct 10 ms 12800 KB Output is correct
20 Correct 10 ms 12800 KB Output is correct
21 Correct 10 ms 12800 KB Output is correct
22 Correct 10 ms 12800 KB Output is correct
23 Correct 92 ms 23068 KB Output is correct
24 Correct 109 ms 24824 KB Output is correct
25 Correct 74 ms 21756 KB Output is correct
26 Correct 83 ms 23288 KB Output is correct
27 Correct 89 ms 23544 KB Output is correct
28 Correct 78 ms 19392 KB Output is correct
29 Correct 216 ms 34424 KB Output is correct
30 Correct 135 ms 26616 KB Output is correct
31 Correct 120 ms 26872 KB Output is correct
32 Correct 109 ms 22392 KB Output is correct
33 Correct 82 ms 22776 KB Output is correct
34 Correct 90 ms 22904 KB Output is correct
35 Correct 85 ms 23928 KB Output is correct
36 Correct 71 ms 20216 KB Output is correct
37 Correct 263 ms 39608 KB Output is correct
38 Correct 107 ms 26104 KB Output is correct
39 Correct 122 ms 22012 KB Output is correct
40 Correct 105 ms 24568 KB Output is correct
41 Correct 83 ms 24312 KB Output is correct
42 Correct 88 ms 23160 KB Output is correct
43 Correct 86 ms 24056 KB Output is correct
44 Correct 59 ms 19936 KB Output is correct
45 Correct 210 ms 36600 KB Output is correct
46 Correct 110 ms 27384 KB Output is correct
47 Correct 114 ms 24544 KB Output is correct
48 Correct 109 ms 26776 KB Output is correct
49 Correct 68 ms 20344 KB Output is correct
50 Correct 86 ms 22876 KB Output is correct
51 Correct 75 ms 21752 KB Output is correct
52 Correct 65 ms 20856 KB Output is correct
53 Correct 218 ms 35964 KB Output is correct
54 Correct 108 ms 25556 KB Output is correct