답안 #84111

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
84111 2018-11-13T02:51:59 Z shoemakerjo Pipes (BOI13_pipes) C++14
100 / 100
262 ms 20412 KB
#include <bits/stdc++.h>

using namespace std;

#define maxn 100010
#define pii pair<int, int>
#define maxm 500010
#define ll long long

int N, M;
vector<pii> adj[maxn];
vector<pii> adj2[maxn];
ll nums[maxn];
ll ans[maxm];
int deg[maxn];
bool del[maxn];
bool vis[maxn];

void dfstree(int u, int p) {

	for (pii vp : adj[u]) {
		if (vp.first != p) {
			dfstree(vp.first, u);
			ans[vp.second] = 2 * nums[vp.first];
			nums[u] -= ans[vp.second]/2;
		}
	}

}

void gotree() {
	dfstree(1, -1);

	for (int i = 0; i < M; i++) {
		cout << ans[i] << '\n';
	}
	cout.flush();

}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin >> N >> M;
	for (int i = 1; i <= N; i++) {
		cin >> nums[i];
	}
	int u, v;
	for (int i = 0; i < M; i++) {
		cin >> u >> v;
		deg[u]++;
		deg[v]++;
		adj[u].push_back(pii(v, i));
		adj[v].push_back(pii(u, i));

	}

	if (M > N) {
		cout << 0 << endl;
		return 0;
	}
	if (M == N-1) {
		gotree();
		return 0;
	}

	stack<int> s;
	//do some stuff
	for (int i = 1; i <= N; i++) {
		if (deg[i] == 1) s.push(i);
	}
	while (s.size()) {
		u = s.top(); s.pop();
		del[u] = true;
		for (pii vp : adj[u]) {
			if (!del[vp.first]) {
				deg[vp.first]--;
				ans[vp.second] = 2*nums[u];
				nums[vp.first] -= ans[vp.second]/2;
				if (deg[vp.first] == 1) {
					del[vp.first] = true;
					s.push(vp.first);
				}
			}
		}
	}


	int cur;
	for (int i = 1; i <= N; i++) {
		if (!del[i]) {
			cur = i;
			for (pii vp : adj[i]) {
				if (!del[vp.first]) {
					adj2[i].push_back(vp);
				}
			}
		}
	}


	ll sum = 0;
	sum += nums[cur];
	ll mult = -1;
	int og = cur;
	int numseen = 1;
	// cout << "guy: " << nums[7] << endl;
	do {
		vis[cur] = true;
		// cout << "cur: " << cur << endl;

		for (int i = 1; i >= 0; i--) {
			pii vp = adj2[cur][i];
			if (!vis[vp.first]) {
				cur = vp.first;
				numseen++;
				// cout << "contribution: " << nums[cur] << " mult: " << mult << endl;
				sum += mult*nums[cur];
				mult *= -1LL;
				break;
			}
		}
	} while (!vis[cur]);

	// cout << "numseen: " << numseen << endl;
	if (numseen%2 == 0) {
		cout << 0 << endl;
		return 0;
	}

	fill(vis, vis+maxn, false);
	ans[adj2[og][0].second] = sum; //not sure if I have to divide by 2
	// cout << og << " to " << adj2[og][0].first << " assigned to " << sum << endl;
	cur = adj2[og][0].first;
	
	vis[og] = true;
	while (cur != og) {
		vis[cur] = true;
		for (int i = 0; i <= 1; i++) {
			pii vp = adj2[cur][i];
			if (!vis[vp.first]) {
				// cout << "looking at: " << cur << " to " << vp.first << endl;

				ans[vp.second] = 2 * (nums[cur] - 
					ans[adj2[cur][1-i].second]/2);
				cur = vp.first;
				// vis[cur] = true;
				break;
			}
		}
		if (vis[cur]) break;
	}

	for (int i = 0; i <= 1; i++) {
		pii vp = adj2[cur][i];
		if (vp.first == og) {
			ans[vp.second] = 2 * (nums[cur] - ans[adj2[cur][1-i].second]/2);
		}
	}
	for (int i = 0; i < M; i++) {
		cout << ans[i] << '\n';
	}

	cout.flush();
}

Compilation message

pipes.cpp: In function 'int main()':
pipes.cpp:103:17: warning: 'cur' may be used uninitialized in this function [-Wmaybe-uninitialized]
  sum += nums[cur];
         ~~~~~~~~^
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 4984 KB Output is correct
2 Correct 6 ms 5248 KB Output is correct
3 Correct 7 ms 5248 KB Output is correct
4 Correct 118 ms 11276 KB Output is correct
5 Correct 6 ms 11276 KB Output is correct
6 Correct 6 ms 11276 KB Output is correct
7 Correct 8 ms 11276 KB Output is correct
8 Correct 6 ms 11276 KB Output is correct
9 Correct 9 ms 11276 KB Output is correct
10 Correct 8 ms 11276 KB Output is correct
11 Correct 9 ms 11276 KB Output is correct
12 Correct 6 ms 11276 KB Output is correct
13 Correct 78 ms 11276 KB Output is correct
14 Correct 82 ms 11276 KB Output is correct
15 Correct 83 ms 11428 KB Output is correct
16 Correct 88 ms 11428 KB Output is correct
17 Correct 89 ms 11568 KB Output is correct
18 Correct 91 ms 11568 KB Output is correct
19 Correct 104 ms 14912 KB Output is correct
20 Correct 6 ms 14912 KB Output is correct
21 Correct 8 ms 14912 KB Output is correct
22 Correct 92 ms 14912 KB Output is correct
23 Correct 64 ms 14912 KB Output is correct
24 Correct 81 ms 14912 KB Output is correct
25 Correct 65 ms 14912 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 14912 KB Output is correct
2 Correct 7 ms 14912 KB Output is correct
3 Correct 80 ms 14912 KB Output is correct
4 Correct 62 ms 14912 KB Output is correct
5 Correct 62 ms 14912 KB Output is correct
6 Correct 262 ms 20332 KB Output is correct
7 Correct 8 ms 20332 KB Output is correct
8 Correct 6 ms 20332 KB Output is correct
9 Correct 6 ms 20332 KB Output is correct
10 Correct 6 ms 20332 KB Output is correct
11 Correct 6 ms 20332 KB Output is correct
12 Correct 6 ms 20332 KB Output is correct
13 Correct 6 ms 20332 KB Output is correct
14 Correct 6 ms 20332 KB Output is correct
15 Correct 7 ms 20332 KB Output is correct
16 Correct 7 ms 20332 KB Output is correct
17 Correct 6 ms 20332 KB Output is correct
18 Correct 7 ms 20332 KB Output is correct
19 Correct 6 ms 20332 KB Output is correct
20 Correct 6 ms 20332 KB Output is correct
21 Correct 7 ms 20332 KB Output is correct
22 Correct 7 ms 20332 KB Output is correct
23 Correct 77 ms 20332 KB Output is correct
24 Correct 99 ms 20332 KB Output is correct
25 Correct 71 ms 20332 KB Output is correct
26 Correct 68 ms 20332 KB Output is correct
27 Correct 60 ms 20332 KB Output is correct
28 Correct 74 ms 20332 KB Output is correct
29 Correct 221 ms 20332 KB Output is correct
30 Correct 98 ms 20332 KB Output is correct
31 Correct 123 ms 20332 KB Output is correct
32 Correct 87 ms 20332 KB Output is correct
33 Correct 80 ms 20332 KB Output is correct
34 Correct 60 ms 20332 KB Output is correct
35 Correct 63 ms 20332 KB Output is correct
36 Correct 63 ms 20332 KB Output is correct
37 Correct 241 ms 20412 KB Output is correct
38 Correct 127 ms 20412 KB Output is correct
39 Correct 83 ms 20412 KB Output is correct
40 Correct 103 ms 20412 KB Output is correct
41 Correct 94 ms 20412 KB Output is correct
42 Correct 56 ms 20412 KB Output is correct
43 Correct 65 ms 20412 KB Output is correct
44 Correct 58 ms 20412 KB Output is correct
45 Correct 181 ms 20412 KB Output is correct
46 Correct 91 ms 20412 KB Output is correct
47 Correct 92 ms 20412 KB Output is correct
48 Correct 107 ms 20412 KB Output is correct
49 Correct 80 ms 20412 KB Output is correct
50 Correct 61 ms 20412 KB Output is correct
51 Correct 59 ms 20412 KB Output is correct
52 Correct 65 ms 20412 KB Output is correct
53 Correct 201 ms 20412 KB Output is correct
54 Correct 90 ms 20412 KB Output is correct