Submission #85892

# Submission time Handle Problem Language Result Execution time Memory
85892 2018-11-22T14:53:24 Z fasterthanyou Pipes (BOI13_pipes) C++14
100 / 100
254 ms 18872 KB
#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <map>
using namespace std;
 
int n, m, child[100015], tmp[100015];
long long d[100015], res[500015];
vector<pair<int, int> > g[100015];
pair<int, int> p[100015];
queue<int> q;

int color[100015], cycleStart, cycleEnd;
bool flag = 0, inCycle[100015];
pair<int, int> par[100015];
vector<int> Ncycle;

void findcycle (int u, int pv)
{
	color[u] = 1;
	for (int i = 0; i < (int)g[u].size(); ++i)
	{
		if (flag) return;
		int v = g[u][i].first;
		if (color[v] == 0)
		{
			findcycle(v, u);
			par[v] = {u, g[u][i].second};
		}
		else if (color[v] == 1 && v != pv)
		{
			cycleStart = v;
			cycleEnd = u;
			flag = 1;
			return;
		}
	}
	color[u] = 2;
}

void dfsx (int u, int pv)
{
	child[u] = 0;
	for (int i = 0; i < (int)g[u].size(); ++i)
	{
		int v = g[u][i].first;
		if (v == pv || inCycle[v]) continue;
		dfsx(v, u);
		child[u] += child[v] + 1;
		p[v] = {u, g[u][i].second};
	}
}

void caseCycle()
{
	findcycle(1, -1);
	int tEnd = cycleEnd;
	while (cycleEnd != cycleStart)
	{
		inCycle[cycleEnd] = 1;
		Ncycle.push_back(cycleEnd);
		cycleEnd = par[cycleEnd].first;
	}
	inCycle[cycleStart] = 1;
	Ncycle.push_back(cycleStart);

	if ((int)Ncycle.size() % 2 == 0)
	{
		cout << 0;
		return;
	}

	for (int i = 0; i < (int)Ncycle.size(); ++i)
		dfsx(Ncycle[i], -1);

	for (int i = 1; i <= n; ++i)
		tmp[i] = child[i];

	for (int i = 1; i <= n; ++i)
		if (child[i] == 0 && !inCycle[i]) q.push(i);

	while (!q.empty())
	{
		int u = q.front(); q.pop();
		d[p[u].first] -= d[u];
		child[p[u].first] -= (tmp[u] + 1);
 
		res[p[u].second] = d[u];
		if (child[p[u].first] < 1 && !inCycle[p[u].first]) q.push(p[u].first);		
	}
	

	long long sum = 0;
	for (int i = 0; i < (int)Ncycle.size(); ++i)
		sum += d[Ncycle[i]] * (i & 1 ? -1 : 1);
	sum /= 2;
	for (int i = 0; i < (int)g[cycleStart].size(); ++i)
		if (g[cycleStart][i].first == tEnd)
		{
			res[g[cycleStart][i].second] = sum;
			d[cycleStart] -= sum;
			d[tEnd] -= sum;
			break;
		}
	while (tEnd != cycleStart)
	{
		res[par[tEnd].second] = d[tEnd];
		d[par[tEnd].first] -= d[tEnd];
		tEnd = par[tEnd].first;
	}
	for (int id = 1; id <= m; ++id)
		cout << res[id] << '\n';
}

void dfs (int u, int pv)
{
	child[u] = 0;
	for (int i = 0; i < (int)g[u].size(); ++i)
	{
		int v = g[u][i].first;
		if (v == pv) continue;
 
		dfs(v, u);
		child[u] += child[v] + 1;
		p[v] = {u, g[u][i].second};
	}
}
 
void caseTree()
{
	p[1] = {-1, -1};
	dfs(1, -1);
 
	for (int i = 2; i <= n; ++i)
		if (child[i] == 0) q.push(i);	
	for (int i = 1; i <= n; ++i)
		tmp[i] = child[i];
 
	while (!q.empty())
	{
		int u = q.front(); q.pop();
		d[p[u].first] -= d[u];
		child[p[u].first] -= (tmp[u] + 1);
 
		if (p[u].first != -1) res[p[u].second] = d[u];
		if (child[p[u].first] < 1 && p[u].first != -1) q.push(p[u].first);
	}
	for (int id = 1; id <= m; ++id)
		cout << res[id] << '\n';
}
 
int main()
{
	ios_base::sync_with_stdio(false); cin.tie(NULL);
	//freopen("pipes.inp", "r", stdin);
	//freopen("pipes.out", "w", stdout);
 
	cin >> n >> m;
	for (int i = 1; i <= n; ++i)
		cin >> d[i],
		d[i] *= 2;
	for (int i = 0, u, v; i < m; ++i)
	{
		cin >> u >> v;
		g[u].push_back({v, i + 1});
		g[v].push_back({u, i + 1});
 	}
	if (m == n - 1) 
		caseTree();
	else if (m == n)
		caseCycle();
	else
		cout << 0;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2680 KB Output is correct
2 Correct 4 ms 2812 KB Output is correct
3 Correct 4 ms 2872 KB Output is correct
4 Correct 86 ms 10312 KB Output is correct
5 Correct 5 ms 10312 KB Output is correct
6 Correct 5 ms 10312 KB Output is correct
7 Correct 4 ms 10312 KB Output is correct
8 Correct 4 ms 10312 KB Output is correct
9 Correct 5 ms 10312 KB Output is correct
10 Correct 4 ms 10312 KB Output is correct
11 Correct 5 ms 10312 KB Output is correct
12 Correct 5 ms 10312 KB Output is correct
13 Correct 68 ms 10312 KB Output is correct
14 Correct 82 ms 10312 KB Output is correct
15 Correct 87 ms 10404 KB Output is correct
16 Correct 71 ms 10404 KB Output is correct
17 Correct 89 ms 10416 KB Output is correct
18 Correct 94 ms 10512 KB Output is correct
19 Correct 78 ms 13612 KB Output is correct
20 Correct 5 ms 13612 KB Output is correct
21 Correct 5 ms 13612 KB Output is correct
22 Correct 123 ms 13612 KB Output is correct
23 Correct 57 ms 13612 KB Output is correct
24 Correct 78 ms 13612 KB Output is correct
25 Correct 67 ms 13612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 13612 KB Output is correct
2 Correct 4 ms 13612 KB Output is correct
3 Correct 66 ms 13612 KB Output is correct
4 Correct 67 ms 13612 KB Output is correct
5 Correct 61 ms 13612 KB Output is correct
6 Correct 227 ms 17596 KB Output is correct
7 Correct 4 ms 17596 KB Output is correct
8 Correct 5 ms 17596 KB Output is correct
9 Correct 4 ms 17596 KB Output is correct
10 Correct 4 ms 17596 KB Output is correct
11 Correct 4 ms 17596 KB Output is correct
12 Correct 4 ms 17596 KB Output is correct
13 Correct 4 ms 17596 KB Output is correct
14 Correct 4 ms 17596 KB Output is correct
15 Correct 5 ms 17596 KB Output is correct
16 Correct 5 ms 17596 KB Output is correct
17 Correct 4 ms 17596 KB Output is correct
18 Correct 4 ms 17596 KB Output is correct
19 Correct 4 ms 17596 KB Output is correct
20 Correct 4 ms 17596 KB Output is correct
21 Correct 5 ms 17596 KB Output is correct
22 Correct 5 ms 17596 KB Output is correct
23 Correct 68 ms 17596 KB Output is correct
24 Correct 87 ms 17596 KB Output is correct
25 Correct 88 ms 17596 KB Output is correct
26 Correct 55 ms 17596 KB Output is correct
27 Correct 60 ms 17596 KB Output is correct
28 Correct 101 ms 17596 KB Output is correct
29 Correct 207 ms 17596 KB Output is correct
30 Correct 134 ms 18108 KB Output is correct
31 Correct 92 ms 18356 KB Output is correct
32 Correct 86 ms 18356 KB Output is correct
33 Correct 73 ms 18356 KB Output is correct
34 Correct 59 ms 18356 KB Output is correct
35 Correct 54 ms 18356 KB Output is correct
36 Correct 56 ms 18356 KB Output is correct
37 Correct 254 ms 18356 KB Output is correct
38 Correct 94 ms 18356 KB Output is correct
39 Correct 85 ms 18356 KB Output is correct
40 Correct 88 ms 18356 KB Output is correct
41 Correct 75 ms 18356 KB Output is correct
42 Correct 66 ms 18356 KB Output is correct
43 Correct 55 ms 18356 KB Output is correct
44 Correct 60 ms 18356 KB Output is correct
45 Correct 197 ms 18356 KB Output is correct
46 Correct 110 ms 18872 KB Output is correct
47 Correct 99 ms 18872 KB Output is correct
48 Correct 116 ms 18872 KB Output is correct
49 Correct 62 ms 18872 KB Output is correct
50 Correct 56 ms 18872 KB Output is correct
51 Correct 54 ms 18872 KB Output is correct
52 Correct 53 ms 18872 KB Output is correct
53 Correct 242 ms 18872 KB Output is correct
54 Correct 107 ms 18872 KB Output is correct