Submission #114791

# Submission time Handle Problem Language Result Execution time Memory
114791 2019-06-02T17:41:26 Z luciocf Pipes (BOI13_pipes) C++14
100 / 100
285 ms 17008 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, c[v] = 0;
		}
	}
}

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);

	if (firstCycle == -1)
		solve(1);
	else
		solve(firstCycle);

	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 7 ms 2816 KB Output is correct
2 Correct 6 ms 2816 KB Output is correct
3 Correct 5 ms 2944 KB Output is correct
4 Correct 96 ms 8952 KB Output is correct
5 Correct 2 ms 2816 KB Output is correct
6 Correct 4 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 5 ms 2860 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 7672 KB Output is correct
14 Correct 85 ms 8568 KB Output is correct
15 Correct 87 ms 8924 KB Output is correct
16 Correct 71 ms 8040 KB Output is correct
17 Correct 77 ms 8824 KB Output is correct
18 Correct 94 ms 8852 KB Output is correct
19 Correct 95 ms 10740 KB Output is correct
20 Correct 3 ms 2816 KB Output is correct
21 Correct 4 ms 2816 KB Output is correct
22 Correct 100 ms 9052 KB Output is correct
23 Correct 78 ms 7696 KB Output is correct
24 Correct 104 ms 8924 KB Output is correct
25 Correct 79 ms 7932 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2816 KB Output is correct
2 Correct 5 ms 2816 KB Output is correct
3 Correct 68 ms 9692 KB Output is correct
4 Correct 70 ms 6392 KB Output is correct
5 Correct 62 ms 6520 KB Output is correct
6 Correct 277 ms 17008 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 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 3 ms 2688 KB Output is correct
14 Correct 4 ms 2816 KB Output is correct
15 Correct 4 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 Correct 4 ms 2944 KB Output is correct
23 Correct 81 ms 9976 KB Output is correct
24 Correct 103 ms 11128 KB Output is correct
25 Correct 69 ms 9744 KB Output is correct
26 Correct 60 ms 6420 KB Output is correct
27 Correct 60 ms 6264 KB Output is correct
28 Correct 60 ms 6776 KB Output is correct
29 Correct 206 ms 14348 KB Output is correct
30 Correct 116 ms 12408 KB Output is correct
31 Correct 109 ms 12632 KB Output is correct
32 Correct 94 ms 9720 KB Output is correct
33 Correct 73 ms 10364 KB Output is correct
34 Correct 56 ms 6352 KB Output is correct
35 Correct 57 ms 6392 KB Output is correct
36 Correct 67 ms 6752 KB Output is correct
37 Correct 285 ms 16988 KB Output is correct
38 Correct 139 ms 12024 KB Output is correct
39 Correct 112 ms 9464 KB Output is correct
40 Correct 118 ms 11076 KB Output is correct
41 Correct 82 ms 11576 KB Output is correct
42 Correct 63 ms 6264 KB Output is correct
43 Correct 69 ms 6268 KB Output is correct
44 Correct 61 ms 6612 KB Output is correct
45 Correct 189 ms 15176 KB Output is correct
46 Correct 119 ms 12840 KB Output is correct
47 Correct 113 ms 11000 KB Output is correct
48 Correct 124 ms 12408 KB Output is correct
49 Correct 71 ms 8440 KB Output is correct
50 Correct 64 ms 6484 KB Output is correct
51 Correct 56 ms 6520 KB Output is correct
52 Correct 58 ms 6264 KB Output is correct
53 Correct 203 ms 14968 KB Output is correct
54 Correct 113 ms 11660 KB Output is correct