Submission #114788

# Submission time Handle Problem Language Result Execution time Memory
114788 2019-06-02T17:01:57 Z luciocf Pipes (BOI13_pipes) C++14
88.3333 / 100
290 ms 17144 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], nivel[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 = u, lastCycle = v, lastEdge = e;
			continue;
		}

		pai[v] = u, nivel[v] = nivel[u]+1, 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)
{
	if (firstCycle == -1) return;

	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] = sum;

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

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

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

		v = pai[v], prev = ans[e];
	}
}

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:118: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:121:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &c[i]);
   ~~~~~^~~~~~~~~~~~~
pipes.cpp:126:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", &u, &v);
   ~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2816 KB Output is correct
2 Correct 4 ms 2816 KB Output is correct
3 Correct 5 ms 2816 KB Output is correct
4 Correct 86 ms 9052 KB Output is correct
5 Correct 4 ms 2816 KB Output is correct
6 Correct 3 ms 2816 KB Output is correct
7 Correct 4 ms 2816 KB Output is correct
8 Correct 4 ms 2816 KB Output is correct
9 Correct 4 ms 2816 KB Output is correct
10 Correct 4 ms 2816 KB Output is correct
11 Correct 4 ms 2816 KB Output is correct
12 Correct 4 ms 2944 KB Output is correct
13 Correct 74 ms 7928 KB Output is correct
14 Correct 106 ms 8824 KB Output is correct
15 Correct 91 ms 9208 KB Output is correct
16 Correct 71 ms 8184 KB Output is correct
17 Correct 89 ms 9080 KB Output is correct
18 Correct 108 ms 9108 KB Output is correct
19 Correct 132 ms 10852 KB Output is correct
20 Correct 4 ms 2816 KB Output is correct
21 Correct 4 ms 2816 KB Output is correct
22 Correct 90 ms 9080 KB Output is correct
23 Correct 66 ms 7800 KB Output is correct
24 Correct 88 ms 9180 KB Output is correct
25 Correct 75 ms 8184 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 2816 KB Output isn't correct
2 Correct 4 ms 2856 KB Output is correct
3 Correct 80 ms 9848 KB Output is correct
4 Correct 63 ms 6492 KB Output is correct
5 Correct 56 ms 6648 KB Output is correct
6 Correct 268 ms 17144 KB Output is correct
7 Incorrect 4 ms 2816 KB Output isn't correct
8 Correct 4 ms 2816 KB Output is correct
9 Correct 4 ms 2688 KB Output is correct
10 Correct 3 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 Incorrect 4 ms 2816 KB Output isn't correct
15 Correct 5 ms 2944 KB Output is correct
16 Correct 5 ms 2816 KB Output is correct
17 Correct 6 ms 2816 KB Output is correct
18 Correct 5 ms 2688 KB Output is correct
19 Correct 5 ms 2688 KB Output is correct
20 Correct 5 ms 2688 KB Output is correct
21 Correct 5 ms 2816 KB Output is correct
22 Incorrect 5 ms 2944 KB Output isn't correct
23 Correct 94 ms 10076 KB Output is correct
24 Correct 121 ms 11244 KB Output is correct
25 Correct 86 ms 9848 KB Output is correct
26 Correct 65 ms 6492 KB Output is correct
27 Correct 54 ms 6392 KB Output is correct
28 Correct 68 ms 6904 KB Output is correct
29 Correct 218 ms 14628 KB Output is correct
30 Incorrect 140 ms 12536 KB Output isn't correct
31 Correct 123 ms 12664 KB Output is correct
32 Correct 81 ms 9848 KB Output is correct
33 Correct 72 ms 10488 KB Output is correct
34 Correct 61 ms 6492 KB Output is correct
35 Correct 56 ms 6520 KB Output is correct
36 Correct 64 ms 6776 KB Output is correct
37 Correct 290 ms 17084 KB Output is correct
38 Correct 116 ms 12080 KB Output is correct
39 Incorrect 88 ms 9592 KB Output isn't correct
40 Incorrect 110 ms 11120 KB Output isn't correct
41 Correct 92 ms 11896 KB Output is correct
42 Correct 70 ms 6392 KB Output is correct
43 Correct 71 ms 6392 KB Output is correct
44 Correct 63 ms 6648 KB Output is correct
45 Correct 230 ms 15412 KB Output is correct
46 Correct 136 ms 13068 KB Output is correct
47 Incorrect 121 ms 11128 KB Output isn't correct
48 Correct 129 ms 12664 KB Output is correct
49 Correct 80 ms 8600 KB Output is correct
50 Correct 68 ms 6572 KB Output is correct
51 Correct 64 ms 6648 KB Output is correct
52 Correct 66 ms 6520 KB Output is correct
53 Correct 235 ms 15096 KB Output is correct
54 Incorrect 105 ms 11768 KB Output isn't correct