답안 #114768

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
114768 2019-06-02T15:57:30 Z luciocf Pipes (BOI13_pipes) C++14
65 / 100
1000 ms 23160 KB
#include <bits/stdc++.h>

using namespace std;

const int maxn = 1e5+10;

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

int n, m;

int c[maxn];

int ans[maxn];

int pai[maxn], edgePai[maxn];

int firstCycle=-1, lastCycle=-1, lastEdge = -1;

bool mark[maxn], inCycle[maxn];

vector<pii> grafo[maxn];

void dfs(int u, int p)
{
	mark[u] = true;

	for (auto pp: grafo[u])
	{
		int v = pp.first, e = pp.second;

		if (mark[v])
		{
			if (v == p) continue;

			firstCycle = v, lastCycle = u, lastEdge = e;
			continue;
		}

		pai[v] = u, edgePai[v] = e;
		dfs(v, u);
	}
}

bool checkSize(void)
{
	if (firstCycle == -1) return 1;

	int v = lastCycle, qtd = 0;

	while (true)
	{
		inCycle[v] = true;
		qtd++;

		if (v == firstCycle) break;

		v = pai[v];
	}

	return (qtd%2 == 1);
}

void solve(int u)
{
	mark[u] = true;

	for (auto pp: grafo[u])
	{
		int v = pp.first, e = pp.second;

		if (mark[v]) continue;

		solve(v);

		if (!inCycle[v])
		{
			ans[e] = 2*c[v];
			c[u] -= ans[e]/2;
		}
	}
}

void solveCycle(void)
{
	int v = lastCycle, sign = 1;
	ll sum = 0ll;

	while (true)
	{
		sum += 1ll*sign*c[v];

		if (v == firstCycle) break;

		sign *= -1, v = pai[v];
	}

	ans[lastEdge] = 2ll*c[firstCycle] - sum;

	int prev = ans[lastEdge];
	v = lastCycle;

	while (true)
	{
		int e = edgePai[v];

		ans[e] = 2*c[v] - prev;

		if (v == firstCycle) break;

		v = pai[v];
	}
}

int main(void)
{
	scanf("%d %d", &n, &m);

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

	for (int i = 1; i <= m; i++)
	{
		int u, v;
		scanf("%d %d", &u, &v);

		grafo[u].push_back({v, i});
		grafo[v].push_back({u, i});
	}

	if (m > n)
	{
		printf("0\n");
		return 0;
	}

	dfs(1, 0);

	if (!checkSize())
	{
		printf("0\n");
		return 0;
	}

	memset(mark, 0, sizeof mark);
	solve(1);

	solveCycle();

	for (int i = 1; i <= m; i++)
		printf("%d\n", ans[i]);
}

Compilation message

pipes.cpp: In function 'int main()':
pipes.cpp:117: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:120:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &c[i]);
   ~~~~~^~~~~~~~~~~~~
pipes.cpp:125: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 Correct 4 ms 2816 KB Output is correct
2 Correct 42 ms 2864 KB Output is correct
3 Correct 5 ms 2816 KB Output is correct
4 Correct 93 ms 8668 KB Output is correct
5 Correct 4 ms 2816 KB Output is correct
6 Correct 4 ms 2816 KB Output is correct
7 Correct 3 ms 2816 KB Output is correct
8 Correct 4 ms 2816 KB Output is correct
9 Correct 5 ms 2816 KB Output is correct
10 Correct 4 ms 2816 KB Output is correct
11 Correct 5 ms 2816 KB Output is correct
12 Correct 5 ms 2816 KB Output is correct
13 Correct 65 ms 7644 KB Output is correct
14 Correct 82 ms 8568 KB Output is correct
15 Correct 86 ms 8824 KB Output is correct
16 Correct 74 ms 8084 KB Output is correct
17 Correct 88 ms 8824 KB Output is correct
18 Correct 92 ms 8696 KB Output is correct
19 Correct 123 ms 10596 KB Output is correct
20 Correct 4 ms 2816 KB Output is correct
21 Correct 5 ms 2816 KB Output is correct
22 Correct 88 ms 8732 KB Output is correct
23 Correct 58 ms 7672 KB Output is correct
24 Correct 94 ms 8804 KB Output is correct
25 Correct 82 ms 7804 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1067 ms 2716 KB Time limit exceeded
2 Execution timed out 1074 ms 2816 KB Time limit exceeded
3 Execution timed out 1082 ms 10744 KB Time limit exceeded
4 Correct 59 ms 7928 KB Output is correct
5 Correct 59 ms 8184 KB Output is correct
6 Correct 250 ms 23132 KB Output is correct
7 Execution timed out 1078 ms 2688 KB Time limit exceeded
8 Execution timed out 1070 ms 2688 KB Time limit exceeded
9 Execution timed out 1062 ms 2688 KB Time limit exceeded
10 Correct 4 ms 2688 KB Output is correct
11 Correct 4 ms 2688 KB Output is correct
12 Correct 4 ms 2688 KB Output is correct
13 Correct 4 ms 2688 KB Output is correct
14 Execution timed out 1085 ms 2688 KB Time limit exceeded
15 Execution timed out 1084 ms 2724 KB Time limit exceeded
16 Execution timed out 1044 ms 2816 KB Time limit exceeded
17 Execution timed out 1040 ms 2688 KB Time limit exceeded
18 Correct 7 ms 2688 KB Output is correct
19 Correct 4 ms 2688 KB Output is correct
20 Correct 5 ms 2688 KB Output is correct
21 Correct 6 ms 2816 KB Output is correct
22 Execution timed out 1087 ms 2816 KB Time limit exceeded
23 Execution timed out 1086 ms 10232 KB Time limit exceeded
24 Execution timed out 1058 ms 11384 KB Time limit exceeded
25 Execution timed out 1065 ms 10744 KB Time limit exceeded
26 Correct 72 ms 8056 KB Output is correct
27 Correct 58 ms 7800 KB Output is correct
28 Correct 64 ms 8312 KB Output is correct
29 Correct 204 ms 19448 KB Output is correct
30 Execution timed out 1076 ms 12792 KB Time limit exceeded
31 Execution timed out 1072 ms 12792 KB Time limit exceeded
32 Execution timed out 1090 ms 9976 KB Time limit exceeded
33 Execution timed out 1078 ms 11512 KB Time limit exceeded
34 Correct 57 ms 7928 KB Output is correct
35 Correct 61 ms 8056 KB Output is correct
36 Correct 63 ms 8312 KB Output is correct
37 Correct 256 ms 23160 KB Output is correct
38 Execution timed out 1045 ms 12280 KB Time limit exceeded
39 Execution timed out 1020 ms 8312 KB Time limit exceeded
40 Execution timed out 1073 ms 11256 KB Time limit exceeded
41 Execution timed out 1071 ms 12792 KB Time limit exceeded
42 Correct 64 ms 7800 KB Output is correct
43 Correct 63 ms 7800 KB Output is correct
44 Correct 63 ms 8184 KB Output is correct
45 Correct 199 ms 20600 KB Output is correct
46 Execution timed out 1079 ms 11768 KB Time limit exceeded
47 Execution timed out 1070 ms 11384 KB Time limit exceeded
48 Execution timed out 1072 ms 12664 KB Time limit exceeded
49 Execution timed out 1071 ms 9592 KB Time limit exceeded
50 Correct 61 ms 8056 KB Output is correct
51 Correct 69 ms 8056 KB Output is correct
52 Correct 66 ms 7800 KB Output is correct
53 Correct 241 ms 20444 KB Output is correct
54 Execution timed out 1055 ms 12024 KB Time limit exceeded