Submission #43843

# Submission time Handle Problem Language Result Execution time Memory
43843 2018-03-25T11:26:47 Z Bruteforceman Pipes (BOI13_pipes) C++11
100 / 100
347 ms 20744 KB
#include "bits/stdc++.h"
using namespace std;
bool vis[100010];
int l[500010], r[500010];
vector <int> g[100010];
long long a[100010];
bool del[500010];
int deg[100010];
long long ans[500010];

vector <int> can;
bool bad;
void check(int x) {
	vis[x] = true;
	can.push_back(x);
	int deg = 0;
	for(auto i : g[x]) {
		if(del[i]) continue;
		int y = l[i] ^ r[i] ^ x;
		++deg;
		if(vis[y] == false) check(y);
	}
	if(deg != 2 && deg != 0) {
		bad = true;
	}
}

int edge(int x, int y) {
	for(auto i : g[x]) {
		int p = l[i] ^ r[i] ^ x;
		if(p == y) return i;
	}
	return -1;
}
void solve(vector <int> v) {
	int size = v.size();
	vector <long long> Odd (size), Even (size);
	long long odd = 0;
	long long even = 0;
	long long sum = 0;
	for(int i = 0; i < size; i++) {
		if(i & 1) {
			odd += a[v[i]];
		} else {
			even += a[v[i]];
		}
		Even[i] = even;
		Odd[i] = odd;
		sum += a[v[i]];
	}
	if(sum % 2 != 0) {
		printf("0\n");
		exit(0);
	}
	sum /= 2;
	for(int i = 0; i < size; i++) {
		int p = v[i];
		int q = v[(i + 1) % size];
		int id = edge(p, q);
		if(i & 1) {
			ans[id] = Even[i];
			ans[id] += odd - Odd[i];
		} else {
			ans[id] = Odd[i];
			ans[id] += even - Even[i]; 
		}
		ans[id] = sum - ans[id];
	}
}
int main(int argc, char const *argv[])
{
	int n, m;
	scanf("%d %d", &n, &m);
	for(int i = 1; i <= n; i++) {
		scanf("%lld", &a[i]);
	}
	queue <int> Q;
	for(int i = 1; i <= m; i++) {
		int p, q;
		scanf("%d %d", &p, &q);
		g[p].push_back(i);
		g[q].push_back(i);
		l[i] = p;
		r[i] = q;
	}
	for(int i = 1; i <= n; i++) {
		deg[i] = g[i].size();
		if(g[i].size() == 1) {
			Q.push(i);
		}
	}
	while(!Q.empty()) {
		int p = Q.front();
		Q.pop();
		if(deg[p] != 1) {
			if(a[p] != 0) {
				printf("0\n");
				exit(0);
			}
			continue;
		}
		for(auto i : g[p]) {
			if(del[i] == true) continue;
			int j = l[i] ^ r[i] ^ p;
			deg[j]--;
			if(deg[j] == 1) {
				Q.push(j);
			}
			ans[i] = a[p];
			a[j] -= a[p];
			del[i] = true; 
		}
	}
	for(int i = 1; i <= n; i++) {
		if(vis[i] == false) {
			bad = false;
			can.clear();
			check(i);
			if(can.size() % 2 == 0) {
				bad = true;
			} 
			if(bad) {
				printf("0\n");
				exit(0);
			}
			if(can.size() > 1) {
				solve(can);
			}
		}
	}
	for(int i = 1; i <= m; i++) {
		printf("%lld\n", ans[i] << 1);
	}
	return 0;
}

Compilation message

pipes.cpp: In function 'int main(int, const char**)':
pipes.cpp:73:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d", &n, &m);
                        ^
pipes.cpp:75:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%lld", &a[i]);
                       ^
pipes.cpp:80:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", &p, &q);
                         ^
# Verdict Execution time Memory Grader output
1 Correct 3 ms 2684 KB Output is correct
2 Correct 3 ms 2784 KB Output is correct
3 Correct 4 ms 2960 KB Output is correct
4 Correct 94 ms 9604 KB Output is correct
5 Correct 3 ms 9604 KB Output is correct
6 Correct 3 ms 9604 KB Output is correct
7 Correct 4 ms 9604 KB Output is correct
8 Correct 3 ms 9604 KB Output is correct
9 Correct 4 ms 9604 KB Output is correct
10 Correct 4 ms 9604 KB Output is correct
11 Correct 4 ms 9604 KB Output is correct
12 Correct 4 ms 9604 KB Output is correct
13 Correct 81 ms 9604 KB Output is correct
14 Correct 91 ms 9604 KB Output is correct
15 Correct 96 ms 9724 KB Output is correct
16 Correct 77 ms 9724 KB Output is correct
17 Correct 99 ms 9884 KB Output is correct
18 Correct 102 ms 9884 KB Output is correct
19 Correct 108 ms 9884 KB Output is correct
20 Correct 4 ms 9884 KB Output is correct
21 Correct 4 ms 9884 KB Output is correct
22 Correct 95 ms 9884 KB Output is correct
23 Correct 74 ms 9884 KB Output is correct
24 Correct 97 ms 9884 KB Output is correct
25 Correct 79 ms 9884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 9884 KB Output is correct
2 Correct 4 ms 9884 KB Output is correct
3 Correct 82 ms 11176 KB Output is correct
4 Correct 93 ms 13092 KB Output is correct
5 Correct 87 ms 13092 KB Output is correct
6 Correct 347 ms 20648 KB Output is correct
7 Correct 4 ms 20648 KB Output is correct
8 Correct 3 ms 20648 KB Output is correct
9 Correct 3 ms 20648 KB Output is correct
10 Correct 4 ms 20648 KB Output is correct
11 Correct 3 ms 20648 KB Output is correct
12 Correct 3 ms 20648 KB Output is correct
13 Correct 4 ms 20648 KB Output is correct
14 Correct 3 ms 20648 KB Output is correct
15 Correct 4 ms 20648 KB Output is correct
16 Correct 4 ms 20648 KB Output is correct
17 Correct 4 ms 20648 KB Output is correct
18 Correct 4 ms 20648 KB Output is correct
19 Correct 4 ms 20648 KB Output is correct
20 Correct 4 ms 20648 KB Output is correct
21 Correct 4 ms 20648 KB Output is correct
22 Correct 4 ms 20648 KB Output is correct
23 Correct 92 ms 20648 KB Output is correct
24 Correct 114 ms 20648 KB Output is correct
25 Correct 81 ms 20648 KB Output is correct
26 Correct 90 ms 20648 KB Output is correct
27 Correct 96 ms 20648 KB Output is correct
28 Correct 113 ms 20648 KB Output is correct
29 Correct 269 ms 20648 KB Output is correct
30 Correct 110 ms 20648 KB Output is correct
31 Correct 115 ms 20648 KB Output is correct
32 Correct 102 ms 20648 KB Output is correct
33 Correct 89 ms 20648 KB Output is correct
34 Correct 98 ms 20648 KB Output is correct
35 Correct 94 ms 20648 KB Output is correct
36 Correct 90 ms 20648 KB Output is correct
37 Correct 330 ms 20744 KB Output is correct
38 Correct 110 ms 20744 KB Output is correct
39 Correct 92 ms 20744 KB Output is correct
40 Correct 102 ms 20744 KB Output is correct
41 Correct 95 ms 20744 KB Output is correct
42 Correct 104 ms 20744 KB Output is correct
43 Correct 103 ms 20744 KB Output is correct
44 Correct 78 ms 20744 KB Output is correct
45 Correct 241 ms 20744 KB Output is correct
46 Correct 119 ms 20744 KB Output is correct
47 Correct 110 ms 20744 KB Output is correct
48 Correct 138 ms 20744 KB Output is correct
49 Correct 93 ms 20744 KB Output is correct
50 Correct 99 ms 20744 KB Output is correct
51 Correct 92 ms 20744 KB Output is correct
52 Correct 94 ms 20744 KB Output is correct
53 Correct 243 ms 20744 KB Output is correct
54 Correct 110 ms 20744 KB Output is correct