Submission #36485

# Submission time Handle Problem Language Result Execution time Memory
36485 2017-12-09T17:06:48 Z cheater2k Pipes (BOI13_pipes) C++14
100 / 100
129 ms 17828 KB
#include <bits/stdc++.h>
using namespace std;

const int N = 100005;
const int M = 500005;

int n, m;
vector< pair<int,int> > G[N]; // (edge, vertex)
vector< pair<int,int> > cyc; // (vertex, edge)
int a[N], del[N], deg[N];
long long w[M];

void find_cycle(int u, int r) {
	del[u] = 1;
	for (auto e : G[u]) {
		if (!del[e.second]) {
			cyc.push_back(make_pair(u, e.first));
			return find_cycle(e.second, r);
		}
	}
	for (auto e : G[u]) {
		if (e.second == r) {
			cyc.push_back(make_pair(u, e.first));
			return;
		}
	}
}

void dfs(int u) {
	for (auto e : G[u]) {
		int v = e.second;
		if (del[v]) {
			w[e.first] = a[v], a[u] -= w[e.first];
		}
	}
}

void out() {
	for (int i = 1; i <= m; ++i) {
		printf("%lld\n", 2LL * w[i]);
	}
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(0);
	cin >> n >> m;
	if (n < m) return printf("0\n"), 0; // invalid
	// ------------- init -------------------------
	for (int i = 1; i <= n; ++i) cin >> a[i];
	for (int i = 1; i <= m; ++i) {
		int u, v; cin >> u >> v;
		G[u].push_back(make_pair(i,v));
		G[v].push_back(make_pair(i,u));
		++deg[u]; ++deg[v];
	}

	// ---------- find cycle ------------------------------------
	queue <int> q;
	for (int i = 1; i <= n; ++i) {
		if (deg[i] == 1) q.push(i);
	}
	while(!q.empty()) {
		int u = q.front(); q.pop(); del[u] = 1;
		dfs(u); // for subtree
		for (auto e : G[u]) if (!del[e.second]) {
			int v = e.second;
			if (--deg[v] == 1) q.push(v);
		}
	}
	for (int i = 1; i <= n; ++i) if (!del[i]) dfs(i); // roots of the subtrees
	for (int i = 1; i <= n; ++i) if (!del[i]) {
		find_cycle(i, i); break;
	}

	// ----- find the weights of the edges ----------------------------------------------------
	if (cyc.size() == 0) { // tree
		out();
		return 0;
	}
	if (cyc.size() % 2 == 0) return printf("0\n"), 0; // invalid

	// solve in the cycle
	int sz = cyc.size();
	int cur = 0;
	long long sum = 0;
	long long weight = 0;
	long long cursum = 0;
	// first: vertex
	// second: edge
	while(true) {
		if ((cur + 2) % sz == 0) break;
		cursum += a[cyc[(cur + 1) % sz].first] - a[cyc[(cur + 2) % sz].first];
		weight += cursum;
		cur = (cur + 2) % sz;
	}
	for (int i = 0; i < sz; ++i) sum += a[cyc[i].first]; sum /= 2; // sum of weights of the edges
	w[cyc[0].second] = (sum + weight) / sz;
	cur = 0;
	cursum = 0;
	while(true) {
		cursum += a[cyc[(cur + 1) % sz].first] - a[cyc[(cur + 2) % sz].first];
		cur = (cur + 2) % sz;
		if (cur == 0) break;
		w[cyc[cur].second] = w[cyc[0].second] - cursum;
	}

	out();
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 9600 KB Output is correct
2 Correct 0 ms 9600 KB Output is correct
3 Correct 0 ms 9600 KB Output is correct
4 Correct 113 ms 13428 KB Output is correct
5 Correct 3 ms 9600 KB Output is correct
6 Correct 0 ms 9600 KB Output is correct
7 Correct 0 ms 9600 KB Output is correct
8 Correct 0 ms 9600 KB Output is correct
9 Correct 0 ms 9600 KB Output is correct
10 Correct 3 ms 9600 KB Output is correct
11 Correct 0 ms 9600 KB Output is correct
12 Correct 0 ms 9600 KB Output is correct
13 Correct 63 ms 12636 KB Output is correct
14 Correct 79 ms 13296 KB Output is correct
15 Correct 73 ms 13560 KB Output is correct
16 Correct 59 ms 12900 KB Output is correct
17 Correct 99 ms 13428 KB Output is correct
18 Correct 93 ms 13560 KB Output is correct
19 Correct 96 ms 12768 KB Output is correct
20 Correct 0 ms 9600 KB Output is correct
21 Correct 0 ms 9600 KB Output is correct
22 Correct 96 ms 13560 KB Output is correct
23 Correct 66 ms 12636 KB Output is correct
24 Correct 103 ms 13560 KB Output is correct
25 Correct 73 ms 12900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 9600 KB Output is correct
2 Correct 0 ms 9600 KB Output is correct
3 Correct 73 ms 15488 KB Output is correct
4 Correct 0 ms 9600 KB Output is correct
5 Correct 0 ms 9600 KB Output is correct
6 Correct 0 ms 9600 KB Output is correct
7 Correct 0 ms 9600 KB Output is correct
8 Correct 0 ms 9600 KB Output is correct
9 Correct 0 ms 9600 KB Output is correct
10 Correct 0 ms 9600 KB Output is correct
11 Correct 0 ms 9600 KB Output is correct
12 Correct 0 ms 9600 KB Output is correct
13 Correct 0 ms 9600 KB Output is correct
14 Correct 0 ms 9600 KB Output is correct
15 Correct 0 ms 9600 KB Output is correct
16 Correct 0 ms 9600 KB Output is correct
17 Correct 0 ms 9600 KB Output is correct
18 Correct 0 ms 9600 KB Output is correct
19 Correct 0 ms 9600 KB Output is correct
20 Correct 0 ms 9600 KB Output is correct
21 Correct 0 ms 9600 KB Output is correct
22 Correct 0 ms 9600 KB Output is correct
23 Correct 73 ms 15244 KB Output is correct
24 Correct 93 ms 15960 KB Output is correct
25 Correct 76 ms 15492 KB Output is correct
26 Correct 0 ms 9600 KB Output is correct
27 Correct 0 ms 9600 KB Output is correct
28 Correct 0 ms 9600 KB Output is correct
29 Correct 0 ms 9600 KB Output is correct
30 Correct 86 ms 15256 KB Output is correct
31 Correct 103 ms 17828 KB Output is correct
32 Correct 79 ms 14420 KB Output is correct
33 Correct 86 ms 16056 KB Output is correct
34 Correct 0 ms 9600 KB Output is correct
35 Correct 0 ms 9600 KB Output is correct
36 Correct 0 ms 9600 KB Output is correct
37 Correct 0 ms 9600 KB Output is correct
38 Correct 129 ms 15480 KB Output is correct
39 Correct 86 ms 13904 KB Output is correct
40 Correct 113 ms 15740 KB Output is correct
41 Correct 69 ms 17828 KB Output is correct
42 Correct 0 ms 9600 KB Output is correct
43 Correct 0 ms 9600 KB Output is correct
44 Correct 0 ms 9600 KB Output is correct
45 Correct 0 ms 9600 KB Output is correct
46 Correct 96 ms 15068 KB Output is correct
47 Correct 86 ms 15804 KB Output is correct
48 Correct 103 ms 17724 KB Output is correct
49 Correct 79 ms 13904 KB Output is correct
50 Correct 0 ms 9600 KB Output is correct
51 Correct 0 ms 9600 KB Output is correct
52 Correct 0 ms 9600 KB Output is correct
53 Correct 0 ms 9600 KB Output is correct
54 Correct 109 ms 15124 KB Output is correct