답안 #158019

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
158019 2019-10-14T12:55:07 Z DrSwad Pipes (BOI13_pipes) C++14
70 / 100
631 ms 66980 KB
#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include "debug.h"
#endif

typedef long long ll;
typedef pair<int, int> pii;

#define x first
#define y second

const int N = 1e5 + 5;

int n, m;
vector<int> c;
vector<set<int>> adj;
vector<pii> e;
vector<ll> ans;

int adjacentNode(int node, int i) {
	assert(i == 0 || i == 1);
	int adj_edge = (i == 0 ? *adj[node].begin() : *adj[node].rbegin());
	int adj_node = e[adj_edge].x ^ e[adj_edge].y ^ node;
	return adj_node;
}

void dfs(int at, int p, int root, vector<pii> &order) {
	int nxt;

	nxt = adjacentNode(at, 0);
	if (nxt != p) order.push_back({at, *adj[at].begin()});
	else {
		nxt = adjacentNode(at, 1);
		if (nxt != p) order.push_back({at, *adj[at].rbegin()});
	}

	if (nxt != root) dfs(nxt, at, root, order);
}

int main() {
	#ifdef LOCAL
	freopen("in", "r", stdin);
	freopen("out", "w", stdout);
	#endif

	scanf("%d %d", &n, &m);

	c.resize(n);
	for (int i = 0; i < n; i++) scanf("%d", &c[i]);

	adj.resize(n);
	e.resize(m);
	for (int i = 0; i < m; i++) {
		int u, v;
		scanf("%d %d", &u, &v);
		u--, v--;
		adj[u].insert(i);
		adj[v].insert(i);
		e[i] = {u, v};
	}

	ans.resize(m);
	queue<int> q;
	for (int i = 0; i < n; i++) q.push(i);

	while (!q.empty()) {
		 int curr_node = q.front(); q.pop();
		 if (adj[curr_node].size() == 1) {
		 	int adj_edge = *adj[curr_node].begin();
		 	int adj_node = adjacentNode(curr_node, 0);

		 	ans[adj_edge] = 2LL * c[curr_node];
		 	c[curr_node] -= ans[adj_edge] / 2;
		 	c[adj_node] -= ans[adj_edge] / 2;

		 	adj[curr_node].erase(adj_edge);
		 	adj[adj_node].erase(adj_edge);

		 	if (adj[adj_node].size() == 1) q.push(adj_node);
		 }
	}

	int cnt_node = 0;
	for (int i = 0; i < n; i++) {
		if (adj[i].size() > 2) {
			printf("0\n");
			return 0;
		}
		cnt_node += adj[i].size() == 2;
	}

	if (cnt_node % 2 == 0) {
		printf("0\n");
		return 0;
	}

	int first_node;
	for (first_node = 0; first_node < n; first_node++) {
		if (adj[first_node].size() == 2) break;
	}

	if (first_node < n) {
		vector<pii> order;
		dfs(first_node, -1, first_node, order);

		ll sum = 2LL * c[first_node];
		ll diff = 0;
		for (int i = 1; i < order.size(); i++) {
			diff = 2LL * c[order[i].x] - diff;
		}

		ans[order[0].y] = (sum - diff) / 2LL;

		for (int i = 1; i < order.size(); i++) {
			ans[order[i].y] = 2LL * c[order[i].x] - ans[order[i - 1].y];
		}
	}

	for (int i = 0; i < m; i++) printf("%lld\n", ans[i]);

	return 0;
}

Compilation message

pipes.cpp: In function 'int main()':
pipes.cpp:111:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int i = 1; i < order.size(); i++) {
                   ~~^~~~~~~~~~~~~~
pipes.cpp:117:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int i = 1; i < order.size(); i++) {
                   ~~^~~~~~~~~~~~~~
pipes.cpp:49:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d", &n, &m);
  ~~~~~^~~~~~~~~~~~~~~~~
pipes.cpp:52:35: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for (int i = 0; i < n; i++) scanf("%d", &c[i]);
                              ~~~~~^~~~~~~~~~~~~
pipes.cpp:58:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", &u, &v);
   ~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 376 KB Output isn't correct
2 Incorrect 2 ms 252 KB Output isn't correct
3 Incorrect 3 ms 504 KB Output isn't correct
4 Incorrect 123 ms 18296 KB Output isn't correct
5 Incorrect 2 ms 256 KB Output isn't correct
6 Incorrect 2 ms 376 KB Output isn't correct
7 Incorrect 2 ms 376 KB Output isn't correct
8 Incorrect 2 ms 380 KB Output isn't correct
9 Incorrect 1 ms 504 KB Output isn't correct
10 Incorrect 3 ms 504 KB Output isn't correct
11 Incorrect 5 ms 504 KB Output isn't correct
12 Incorrect 3 ms 508 KB Output isn't correct
13 Incorrect 109 ms 14328 KB Output isn't correct
14 Incorrect 129 ms 17272 KB Output isn't correct
15 Incorrect 131 ms 18296 KB Output isn't correct
16 Incorrect 109 ms 15096 KB Output isn't correct
17 Incorrect 143 ms 18296 KB Output isn't correct
18 Incorrect 140 ms 18296 KB Output isn't correct
19 Incorrect 124 ms 18296 KB Output isn't correct
20 Incorrect 2 ms 376 KB Output isn't correct
21 Incorrect 3 ms 504 KB Output isn't correct
22 Incorrect 144 ms 18324 KB Output isn't correct
23 Incorrect 105 ms 14584 KB Output isn't correct
24 Incorrect 144 ms 18376 KB Output isn't correct
25 Incorrect 119 ms 15352 KB Output isn't correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 3 ms 504 KB Output is correct
3 Correct 102 ms 17320 KB Output is correct
4 Correct 97 ms 18168 KB Output is correct
5 Correct 125 ms 17912 KB Output is correct
6 Correct 631 ms 66892 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 360 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 3 ms 632 KB Output is correct
16 Correct 3 ms 504 KB Output is correct
17 Correct 3 ms 504 KB Output is correct
18 Correct 3 ms 504 KB Output is correct
19 Correct 3 ms 508 KB Output is correct
20 Correct 3 ms 504 KB Output is correct
21 Correct 4 ms 760 KB Output is correct
22 Correct 3 ms 504 KB Output is correct
23 Correct 108 ms 18672 KB Output is correct
24 Correct 142 ms 22012 KB Output is correct
25 Correct 115 ms 17276 KB Output is correct
26 Correct 103 ms 18296 KB Output is correct
27 Correct 94 ms 18308 KB Output is correct
28 Correct 123 ms 18552 KB Output is correct
29 Correct 473 ms 54796 KB Output is correct
30 Correct 136 ms 21152 KB Output is correct
31 Correct 128 ms 24512 KB Output is correct
32 Correct 159 ms 19916 KB Output is correct
33 Correct 104 ms 18424 KB Output is correct
34 Correct 97 ms 18324 KB Output is correct
35 Correct 98 ms 18296 KB Output is correct
36 Correct 117 ms 18424 KB Output is correct
37 Correct 603 ms 66980 KB Output is correct
38 Correct 159 ms 21624 KB Output is correct
39 Correct 141 ms 19272 KB Output is correct
40 Correct 151 ms 21816 KB Output is correct
41 Correct 87 ms 18424 KB Output is correct
42 Correct 88 ms 18396 KB Output is correct
43 Correct 91 ms 18424 KB Output is correct
44 Correct 136 ms 18040 KB Output is correct
45 Correct 559 ms 57176 KB Output is correct
46 Correct 133 ms 21328 KB Output is correct
47 Correct 130 ms 21828 KB Output is correct
48 Correct 139 ms 24172 KB Output is correct
49 Correct 116 ms 17272 KB Output is correct
50 Correct 108 ms 18324 KB Output is correct
51 Correct 115 ms 18424 KB Output is correct
52 Correct 94 ms 18092 KB Output is correct
53 Correct 540 ms 59196 KB Output is correct
54 Correct 131 ms 20772 KB Output is correct