답안 #84109

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
84109 2018-11-13T02:20:21 Z shoemakerjo Pipes (BOI13_pipes) C++14
65 / 100
297 ms 20540 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.second] -= 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;
	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 << adj2[og][0].second << " assigned to " << sum << endl;
	cur = adj2[og][0].first;
	while (cur != og) {
		for (int i = 0; i <= 1; i++) {
			pii vp = adj2[cur][i];
			if (!vis[vp.first]) {

				ans[vp.second] = 2 * (nums[cur] - 
					ans[adj2[cur][1-i].second]/2);
				cur = vp.first;
				vis[cur] = true;
				break;
			}
		}
	}
	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 5112 KB Output is correct
2 Correct 7 ms 5316 KB Output is correct
3 Correct 7 ms 5316 KB Output is correct
4 Correct 83 ms 11200 KB Output is correct
5 Correct 7 ms 11200 KB Output is correct
6 Correct 6 ms 11200 KB Output is correct
7 Correct 6 ms 11200 KB Output is correct
8 Correct 7 ms 11200 KB Output is correct
9 Correct 7 ms 11200 KB Output is correct
10 Correct 8 ms 11200 KB Output is correct
11 Correct 7 ms 11200 KB Output is correct
12 Correct 8 ms 11200 KB Output is correct
13 Correct 66 ms 11200 KB Output is correct
14 Correct 79 ms 11200 KB Output is correct
15 Correct 97 ms 11468 KB Output is correct
16 Correct 74 ms 11468 KB Output is correct
17 Correct 98 ms 11468 KB Output is correct
18 Correct 86 ms 11468 KB Output is correct
19 Correct 92 ms 14748 KB Output is correct
20 Correct 6 ms 14748 KB Output is correct
21 Correct 6 ms 14748 KB Output is correct
22 Correct 84 ms 14748 KB Output is correct
23 Correct 66 ms 14748 KB Output is correct
24 Correct 90 ms 14748 KB Output is correct
25 Correct 70 ms 14748 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 6 ms 14748 KB Output isn't correct
2 Incorrect 8 ms 14748 KB Output isn't correct
3 Incorrect 170 ms 14748 KB Output isn't correct
4 Correct 61 ms 14748 KB Output is correct
5 Correct 67 ms 14748 KB Output is correct
6 Correct 284 ms 20384 KB Output is correct
7 Incorrect 6 ms 20384 KB Output isn't correct
8 Incorrect 8 ms 20384 KB Output isn't correct
9 Incorrect 6 ms 20384 KB Output isn't correct
10 Correct 8 ms 20384 KB Output is correct
11 Correct 6 ms 20384 KB Output is correct
12 Correct 6 ms 20384 KB Output is correct
13 Correct 6 ms 20384 KB Output is correct
14 Incorrect 6 ms 20384 KB Output isn't correct
15 Incorrect 8 ms 20384 KB Output isn't correct
16 Incorrect 8 ms 20384 KB Output isn't correct
17 Incorrect 8 ms 20384 KB Output isn't correct
18 Correct 7 ms 20384 KB Output is correct
19 Correct 7 ms 20384 KB Output is correct
20 Correct 7 ms 20384 KB Output is correct
21 Correct 7 ms 20384 KB Output is correct
22 Incorrect 8 ms 20384 KB Output isn't correct
23 Incorrect 166 ms 20384 KB Output isn't correct
24 Incorrect 193 ms 20384 KB Output isn't correct
25 Incorrect 166 ms 20384 KB Output isn't correct
26 Correct 58 ms 20384 KB Output is correct
27 Correct 55 ms 20384 KB Output is correct
28 Correct 65 ms 20384 KB Output is correct
29 Correct 217 ms 20384 KB Output is correct
30 Incorrect 181 ms 20384 KB Output isn't correct
31 Incorrect 264 ms 20384 KB Output isn't correct
32 Incorrect 178 ms 20384 KB Output isn't correct
33 Incorrect 209 ms 20384 KB Output isn't correct
34 Correct 100 ms 20384 KB Output is correct
35 Correct 56 ms 20384 KB Output is correct
36 Correct 57 ms 20384 KB Output is correct
37 Correct 261 ms 20540 KB Output is correct
38 Incorrect 190 ms 20540 KB Output isn't correct
39 Incorrect 100 ms 20540 KB Output isn't correct
40 Incorrect 189 ms 20540 KB Output isn't correct
41 Incorrect 297 ms 20540 KB Output isn't correct
42 Correct 58 ms 20540 KB Output is correct
43 Correct 62 ms 20540 KB Output is correct
44 Correct 58 ms 20540 KB Output is correct
45 Correct 195 ms 20540 KB Output is correct
46 Incorrect 161 ms 20540 KB Output isn't correct
47 Incorrect 183 ms 20540 KB Output isn't correct
48 Incorrect 288 ms 20540 KB Output isn't correct
49 Incorrect 103 ms 20540 KB Output isn't correct
50 Correct 62 ms 20540 KB Output is correct
51 Correct 67 ms 20540 KB Output is correct
52 Correct 55 ms 20540 KB Output is correct
53 Correct 214 ms 20540 KB Output is correct
54 Incorrect 164 ms 20540 KB Output isn't correct