Submission #114790

# Submission time Handle Problem Language Result Execution time Memory
114790 2019-06-02T17:25:09 Z luciocf Pipes (BOI13_pipes) C++14
88.3333 / 100
269 ms 17016 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], ok=1;

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;

			if (abs(nivel[u]-nivel[v])%2 == 1)
				ok = 0;

			continue;
		}

		pai[v] = u, nivel[v] = nivel[u]+1, edgePai[v] = e;

		dfs(v, u);
	}
}

void markCycle(void)
{
	if (firstCycle == -1) return;

	int v = lastCycle;

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

		if (v == firstCycle) break;

		v = pai[v];
	}
}

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 (!ok)
	{
		printf("0\n");
		return 0;
	}

	markCycle();

	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:119: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:122:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &c[i]);
   ~~~~~^~~~~~~~~~~~~
pipes.cpp:127: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 4 ms 2816 KB Output is correct
4 Correct 86 ms 8896 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 4 ms 2816 KB Output is correct
10 Correct 5 ms 2816 KB Output is correct
11 Correct 4 ms 2816 KB Output is correct
12 Correct 5 ms 2816 KB Output is correct
13 Correct 62 ms 7644 KB Output is correct
14 Correct 79 ms 8696 KB Output is correct
15 Correct 80 ms 8952 KB Output is correct
16 Correct 68 ms 7928 KB Output is correct
17 Correct 89 ms 8920 KB Output is correct
18 Correct 104 ms 8824 KB Output is correct
19 Correct 105 ms 10592 KB Output is correct
20 Correct 4 ms 2816 KB Output is correct
21 Correct 4 ms 2816 KB Output is correct
22 Correct 86 ms 8952 KB Output is correct
23 Correct 62 ms 7672 KB Output is correct
24 Correct 86 ms 8960 KB Output is correct
25 Correct 66 ms 7900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 2816 KB Output isn't correct
2 Correct 4 ms 2944 KB Output is correct
3 Correct 71 ms 9592 KB Output is correct
4 Correct 60 ms 6264 KB Output is correct
5 Correct 66 ms 6524 KB Output is correct
6 Correct 257 ms 16980 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 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 Incorrect 4 ms 2816 KB Output isn't correct
15 Correct 5 ms 2944 KB Output is correct
16 Correct 4 ms 2816 KB Output is correct
17 Correct 4 ms 2816 KB Output is correct
18 Correct 4 ms 2688 KB Output is correct
19 Correct 4 ms 2688 KB Output is correct
20 Correct 4 ms 2688 KB Output is correct
21 Correct 4 ms 2816 KB Output is correct
22 Incorrect 4 ms 2944 KB Output isn't correct
23 Correct 79 ms 9976 KB Output is correct
24 Correct 102 ms 11100 KB Output is correct
25 Correct 82 ms 9592 KB Output is correct
26 Correct 62 ms 6392 KB Output is correct
27 Correct 63 ms 6264 KB Output is correct
28 Correct 67 ms 6776 KB Output is correct
29 Correct 203 ms 14328 KB Output is correct
30 Incorrect 123 ms 12408 KB Output isn't correct
31 Correct 121 ms 12540 KB Output is correct
32 Correct 86 ms 9720 KB Output is correct
33 Correct 76 ms 10360 KB Output is correct
34 Correct 65 ms 6264 KB Output is correct
35 Correct 64 ms 6264 KB Output is correct
36 Correct 71 ms 6520 KB Output is correct
37 Correct 269 ms 17016 KB Output is correct
38 Correct 122 ms 12024 KB Output is correct
39 Incorrect 91 ms 9424 KB Output isn't correct
40 Incorrect 119 ms 11120 KB Output isn't correct
41 Correct 92 ms 11564 KB Output is correct
42 Correct 65 ms 6264 KB Output is correct
43 Correct 65 ms 6136 KB Output is correct
44 Correct 63 ms 6648 KB Output is correct
45 Correct 212 ms 15096 KB Output is correct
46 Correct 132 ms 12920 KB Output is correct
47 Incorrect 147 ms 10972 KB Output isn't correct
48 Correct 120 ms 12408 KB Output is correct
49 Correct 75 ms 8440 KB Output is correct
50 Correct 67 ms 6392 KB Output is correct
51 Correct 59 ms 6520 KB Output is correct
52 Correct 62 ms 6264 KB Output is correct
53 Correct 216 ms 15056 KB Output is correct
54 Incorrect 114 ms 11772 KB Output isn't correct