Submission #109998

# Submission time Handle Problem Language Result Execution time Memory
109998 2019-05-08T15:08:25 Z luciocf Mergers (JOI19_mergers) C++14
70 / 100
1688 ms 78848 KB
#include <bits/stdc++.h>

using namespace std;

const int maxn = 1e5+10;
const int maxk = 55;
const int maxl = 20;

int n, k;

int pai[maxn], peso[maxn];

int s[maxn];

int nivel[maxn];
int tab[maxn][maxl];

int grau[maxn];

bool mark[maxn];

vector<int> grafo[maxn], state[maxn];

int sub[maxk][maxn];

void init(void)
{
	for (int i = 1; i <= n; i++)
		pai[i] = i, peso[i] = 1;
}

int Find(int x)
{
	if (pai[x] == x) return x;
	return pai[x] = Find(pai[x]);
}

void Join(int x, int y)
{
	x = Find(x), y = Find(y);
	if (x == y) return;

	if (peso[x] < peso[y]) swap(x, y);

	pai[y] = x, peso[x] += peso[y];
}

void dfs(int u, int p)
{
	for (auto v: grafo[u])
	{
		if (v == p) continue;

		nivel[v] = nivel[u]+1, tab[v][0] = u;
		dfs(v, u);
	}
}

void build(void)
{
	for (int j = 1; j < maxl; j++)
		for (int i = 1; i <= n; i++)
			tab[i][j] = tab[tab[i][j-1]][j-1];
}

int lca(int u, int v)
{
	if (nivel[u] < nivel[v]) swap(u, v);

	for (int i = maxl-1; i >= 0; i--)
		if (nivel[u]-(1<<i) >= nivel[v])
			u = tab[u][i];

	if (u == v) return u;

	for (int i = maxl-1; i >= 0; i--)
		if (tab[u][i] && tab[v][i] && tab[u][i] != tab[v][i])
			u = tab[u][i], v = tab[v][i];

	return tab[u][0];
}

void dfs2(int u, int p, int q)
{
	if (s[u] == q) 
		sub[q][u]++;

	for (auto v: grafo[u])
	{
		if (v == p) continue;

		dfs2(v, u, q);

		sub[q][u] += sub[q][v];
	}
}

void solve(int u, int p)
{
	for (auto v: grafo[u])
	{
		if (v == p) continue;

		solve(v, u);

		bool ok = 1;

		for (int i = 1; i <= k; i++)
			if (sub[i][v] > 0 && sub[i][1] > sub[i][v])
				ok = 0;

		if (!ok)
			Join(v, u);
	}
}

void small_k(void)
{
	for (int i = 1; i <= k; i++)
		dfs2(1, 0, i);

	solve(1, 0);

	for (int i = 1; i <= n; i++)
		for (auto v: grafo[i])
			if (Find(v) != Find(i))
				grau[Find(v)]++;

	int leaf = 0;

	for (int i = 1; i <= n; i++)
		if (grau[i] == 1)
			leaf++;

	printf("%d\n", (leaf+1)/2);
}

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

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

		grafo[u].push_back(v);
		grafo[v].push_back(u);
	}

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

		state[s[i]].push_back(i);
	}

	init();

	if (k <= 50)
	{
		small_k();
		return 0;
	}

	nivel[1] = 1;
	dfs(1, 0); build();

	for (int i = 1; i <= n; i++)
	{
		if (mark[s[i]]) continue;
		mark[s[i]] = 1;

		for (auto j: state[s[i]])
		{
			int v = j;
			int l = lca(i, v);
			
			while (nivel[v] >= nivel[l])
			{
				Join(i, v);
				v = tab[v][0];
			}

			v = i;

			while (nivel[v] >= nivel[l])
			{
				Join(i, v);
				v = tab[v][0];
			}
		}
	}

	for (int i = 1; i <= n; i++)
		for (auto v: grafo[i])
			if (Find(v) != Find(i))
				grau[Find(v)]++;

	int leaf = 0;

	for (int i = 1; i <= n; i++)
		if (grau[i] == 1)
			leaf++;

	printf("%d\n", (leaf+1)/2);
}

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:140:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d", &n, &k);
  ~~~~~^~~~~~~~~~~~~~~~~
mergers.cpp:145:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", &u, &v);
   ~~~~~^~~~~~~~~~~~~~~~~
mergers.cpp:153:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &s[i]);
   ~~~~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5120 KB Output is correct
2 Correct 7 ms 5120 KB Output is correct
3 Correct 6 ms 5120 KB Output is correct
4 Correct 7 ms 5120 KB Output is correct
5 Correct 7 ms 4992 KB Output is correct
6 Correct 7 ms 5120 KB Output is correct
7 Correct 6 ms 4992 KB Output is correct
8 Correct 8 ms 5120 KB Output is correct
9 Correct 8 ms 5120 KB Output is correct
10 Correct 8 ms 4992 KB Output is correct
11 Correct 7 ms 5120 KB Output is correct
12 Correct 6 ms 5120 KB Output is correct
13 Correct 6 ms 5120 KB Output is correct
14 Correct 6 ms 5120 KB Output is correct
15 Correct 7 ms 5120 KB Output is correct
16 Correct 7 ms 5120 KB Output is correct
17 Correct 7 ms 5120 KB Output is correct
18 Correct 7 ms 5120 KB Output is correct
19 Correct 7 ms 5120 KB Output is correct
20 Correct 7 ms 5120 KB Output is correct
21 Correct 7 ms 5120 KB Output is correct
22 Correct 8 ms 5120 KB Output is correct
23 Correct 7 ms 5120 KB Output is correct
24 Correct 6 ms 5120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5120 KB Output is correct
2 Correct 7 ms 5120 KB Output is correct
3 Correct 6 ms 5120 KB Output is correct
4 Correct 7 ms 5120 KB Output is correct
5 Correct 7 ms 4992 KB Output is correct
6 Correct 7 ms 5120 KB Output is correct
7 Correct 6 ms 4992 KB Output is correct
8 Correct 8 ms 5120 KB Output is correct
9 Correct 8 ms 5120 KB Output is correct
10 Correct 8 ms 4992 KB Output is correct
11 Correct 7 ms 5120 KB Output is correct
12 Correct 6 ms 5120 KB Output is correct
13 Correct 6 ms 5120 KB Output is correct
14 Correct 6 ms 5120 KB Output is correct
15 Correct 7 ms 5120 KB Output is correct
16 Correct 7 ms 5120 KB Output is correct
17 Correct 7 ms 5120 KB Output is correct
18 Correct 7 ms 5120 KB Output is correct
19 Correct 7 ms 5120 KB Output is correct
20 Correct 7 ms 5120 KB Output is correct
21 Correct 7 ms 5120 KB Output is correct
22 Correct 8 ms 5120 KB Output is correct
23 Correct 7 ms 5120 KB Output is correct
24 Correct 6 ms 5120 KB Output is correct
25 Correct 6 ms 5376 KB Output is correct
26 Correct 9 ms 5504 KB Output is correct
27 Correct 15 ms 6144 KB Output is correct
28 Correct 11 ms 5632 KB Output is correct
29 Correct 11 ms 5504 KB Output is correct
30 Correct 9 ms 5504 KB Output is correct
31 Correct 8 ms 5248 KB Output is correct
32 Correct 10 ms 5760 KB Output is correct
33 Correct 8 ms 5248 KB Output is correct
34 Correct 13 ms 6016 KB Output is correct
35 Correct 10 ms 5504 KB Output is correct
36 Correct 13 ms 6148 KB Output is correct
37 Correct 11 ms 5504 KB Output is correct
38 Correct 7 ms 5120 KB Output is correct
39 Correct 11 ms 5504 KB Output is correct
40 Correct 8 ms 5248 KB Output is correct
41 Correct 8 ms 5376 KB Output is correct
42 Correct 9 ms 5504 KB Output is correct
43 Correct 9 ms 5504 KB Output is correct
44 Correct 6 ms 5120 KB Output is correct
45 Correct 12 ms 5632 KB Output is correct
46 Correct 10 ms 5504 KB Output is correct
47 Correct 7 ms 5376 KB Output is correct
48 Correct 9 ms 5504 KB Output is correct
49 Correct 9 ms 5632 KB Output is correct
50 Correct 9 ms 5632 KB Output is correct
51 Correct 9 ms 5504 KB Output is correct
52 Correct 13 ms 6144 KB Output is correct
53 Correct 8 ms 5504 KB Output is correct
54 Correct 14 ms 6144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5120 KB Output is correct
2 Correct 7 ms 5120 KB Output is correct
3 Correct 6 ms 5120 KB Output is correct
4 Correct 7 ms 5120 KB Output is correct
5 Correct 7 ms 4992 KB Output is correct
6 Correct 7 ms 5120 KB Output is correct
7 Correct 6 ms 4992 KB Output is correct
8 Correct 8 ms 5120 KB Output is correct
9 Correct 8 ms 5120 KB Output is correct
10 Correct 8 ms 4992 KB Output is correct
11 Correct 7 ms 5120 KB Output is correct
12 Correct 6 ms 5120 KB Output is correct
13 Correct 6 ms 5120 KB Output is correct
14 Correct 6 ms 5120 KB Output is correct
15 Correct 7 ms 5120 KB Output is correct
16 Correct 7 ms 5120 KB Output is correct
17 Correct 7 ms 5120 KB Output is correct
18 Correct 7 ms 5120 KB Output is correct
19 Correct 7 ms 5120 KB Output is correct
20 Correct 7 ms 5120 KB Output is correct
21 Correct 7 ms 5120 KB Output is correct
22 Correct 8 ms 5120 KB Output is correct
23 Correct 7 ms 5120 KB Output is correct
24 Correct 6 ms 5120 KB Output is correct
25 Correct 7 ms 5376 KB Output is correct
26 Correct 468 ms 31416 KB Output is correct
27 Correct 945 ms 30944 KB Output is correct
28 Correct 13 ms 6116 KB Output is correct
29 Correct 6 ms 5248 KB Output is correct
30 Correct 7 ms 5376 KB Output is correct
31 Correct 865 ms 31096 KB Output is correct
32 Correct 12 ms 6144 KB Output is correct
33 Correct 1688 ms 38280 KB Output is correct
34 Correct 974 ms 31196 KB Output is correct
35 Correct 16 ms 6016 KB Output is correct
36 Correct 1547 ms 31868 KB Output is correct
37 Correct 9 ms 5220 KB Output is correct
38 Correct 11 ms 5376 KB Output is correct
39 Correct 385 ms 31220 KB Output is correct
40 Correct 9 ms 5504 KB Output is correct
41 Correct 117 ms 11764 KB Output is correct
42 Correct 1653 ms 33296 KB Output is correct
43 Correct 8 ms 5248 KB Output is correct
44 Correct 190 ms 19128 KB Output is correct
45 Correct 1154 ms 34808 KB Output is correct
46 Correct 15 ms 6144 KB Output is correct
47 Correct 13 ms 6016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 522 ms 31364 KB Output is correct
2 Correct 164 ms 23280 KB Output is correct
3 Correct 9 ms 5504 KB Output is correct
4 Correct 15 ms 6144 KB Output is correct
5 Correct 8 ms 5248 KB Output is correct
6 Correct 7 ms 5376 KB Output is correct
7 Correct 12 ms 6012 KB Output is correct
8 Correct 222 ms 21368 KB Output is correct
9 Correct 13 ms 6016 KB Output is correct
10 Correct 165 ms 19832 KB Output is correct
11 Correct 7 ms 5120 KB Output is correct
12 Correct 238 ms 19704 KB Output is correct
13 Correct 199 ms 21368 KB Output is correct
14 Correct 173 ms 23004 KB Output is correct
15 Correct 374 ms 31092 KB Output is correct
16 Correct 10 ms 5504 KB Output is correct
17 Correct 6 ms 5120 KB Output is correct
18 Correct 152 ms 23016 KB Output is correct
19 Correct 189 ms 25412 KB Output is correct
20 Correct 10 ms 5504 KB Output is correct
21 Correct 6 ms 5376 KB Output is correct
22 Correct 167 ms 21592 KB Output is correct
23 Correct 10 ms 5504 KB Output is correct
24 Correct 187 ms 20344 KB Output is correct
25 Correct 183 ms 24568 KB Output is correct
26 Correct 11 ms 5632 KB Output is correct
27 Correct 11 ms 5632 KB Output is correct
28 Correct 11 ms 5504 KB Output is correct
29 Correct 12 ms 6016 KB Output is correct
30 Correct 10 ms 5504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5120 KB Output is correct
2 Correct 7 ms 5120 KB Output is correct
3 Correct 6 ms 5120 KB Output is correct
4 Correct 7 ms 5120 KB Output is correct
5 Correct 7 ms 4992 KB Output is correct
6 Correct 7 ms 5120 KB Output is correct
7 Correct 6 ms 4992 KB Output is correct
8 Correct 8 ms 5120 KB Output is correct
9 Correct 8 ms 5120 KB Output is correct
10 Correct 8 ms 4992 KB Output is correct
11 Correct 7 ms 5120 KB Output is correct
12 Correct 6 ms 5120 KB Output is correct
13 Correct 6 ms 5120 KB Output is correct
14 Correct 6 ms 5120 KB Output is correct
15 Correct 7 ms 5120 KB Output is correct
16 Correct 7 ms 5120 KB Output is correct
17 Correct 7 ms 5120 KB Output is correct
18 Correct 7 ms 5120 KB Output is correct
19 Correct 7 ms 5120 KB Output is correct
20 Correct 7 ms 5120 KB Output is correct
21 Correct 7 ms 5120 KB Output is correct
22 Correct 8 ms 5120 KB Output is correct
23 Correct 7 ms 5120 KB Output is correct
24 Correct 6 ms 5120 KB Output is correct
25 Correct 6 ms 5376 KB Output is correct
26 Correct 9 ms 5504 KB Output is correct
27 Correct 15 ms 6144 KB Output is correct
28 Correct 11 ms 5632 KB Output is correct
29 Correct 11 ms 5504 KB Output is correct
30 Correct 9 ms 5504 KB Output is correct
31 Correct 8 ms 5248 KB Output is correct
32 Correct 10 ms 5760 KB Output is correct
33 Correct 8 ms 5248 KB Output is correct
34 Correct 13 ms 6016 KB Output is correct
35 Correct 10 ms 5504 KB Output is correct
36 Correct 13 ms 6148 KB Output is correct
37 Correct 11 ms 5504 KB Output is correct
38 Correct 7 ms 5120 KB Output is correct
39 Correct 11 ms 5504 KB Output is correct
40 Correct 8 ms 5248 KB Output is correct
41 Correct 8 ms 5376 KB Output is correct
42 Correct 9 ms 5504 KB Output is correct
43 Correct 9 ms 5504 KB Output is correct
44 Correct 6 ms 5120 KB Output is correct
45 Correct 12 ms 5632 KB Output is correct
46 Correct 10 ms 5504 KB Output is correct
47 Correct 7 ms 5376 KB Output is correct
48 Correct 9 ms 5504 KB Output is correct
49 Correct 9 ms 5632 KB Output is correct
50 Correct 9 ms 5632 KB Output is correct
51 Correct 9 ms 5504 KB Output is correct
52 Correct 13 ms 6144 KB Output is correct
53 Correct 8 ms 5504 KB Output is correct
54 Correct 14 ms 6144 KB Output is correct
55 Correct 7 ms 5376 KB Output is correct
56 Correct 468 ms 31416 KB Output is correct
57 Correct 945 ms 30944 KB Output is correct
58 Correct 13 ms 6116 KB Output is correct
59 Correct 6 ms 5248 KB Output is correct
60 Correct 7 ms 5376 KB Output is correct
61 Correct 865 ms 31096 KB Output is correct
62 Correct 12 ms 6144 KB Output is correct
63 Correct 1688 ms 38280 KB Output is correct
64 Correct 974 ms 31196 KB Output is correct
65 Correct 16 ms 6016 KB Output is correct
66 Correct 1547 ms 31868 KB Output is correct
67 Correct 9 ms 5220 KB Output is correct
68 Correct 11 ms 5376 KB Output is correct
69 Correct 385 ms 31220 KB Output is correct
70 Correct 9 ms 5504 KB Output is correct
71 Correct 117 ms 11764 KB Output is correct
72 Correct 1653 ms 33296 KB Output is correct
73 Correct 8 ms 5248 KB Output is correct
74 Correct 190 ms 19128 KB Output is correct
75 Correct 1154 ms 34808 KB Output is correct
76 Correct 15 ms 6144 KB Output is correct
77 Correct 13 ms 6016 KB Output is correct
78 Correct 522 ms 31364 KB Output is correct
79 Correct 164 ms 23280 KB Output is correct
80 Correct 9 ms 5504 KB Output is correct
81 Correct 15 ms 6144 KB Output is correct
82 Correct 8 ms 5248 KB Output is correct
83 Correct 7 ms 5376 KB Output is correct
84 Correct 12 ms 6012 KB Output is correct
85 Correct 222 ms 21368 KB Output is correct
86 Correct 13 ms 6016 KB Output is correct
87 Correct 165 ms 19832 KB Output is correct
88 Correct 7 ms 5120 KB Output is correct
89 Correct 238 ms 19704 KB Output is correct
90 Correct 199 ms 21368 KB Output is correct
91 Correct 173 ms 23004 KB Output is correct
92 Correct 374 ms 31092 KB Output is correct
93 Correct 10 ms 5504 KB Output is correct
94 Correct 6 ms 5120 KB Output is correct
95 Correct 152 ms 23016 KB Output is correct
96 Correct 189 ms 25412 KB Output is correct
97 Correct 10 ms 5504 KB Output is correct
98 Correct 6 ms 5376 KB Output is correct
99 Correct 167 ms 21592 KB Output is correct
100 Correct 10 ms 5504 KB Output is correct
101 Correct 187 ms 20344 KB Output is correct
102 Correct 183 ms 24568 KB Output is correct
103 Correct 11 ms 5632 KB Output is correct
104 Correct 11 ms 5632 KB Output is correct
105 Correct 11 ms 5504 KB Output is correct
106 Correct 12 ms 6016 KB Output is correct
107 Correct 10 ms 5504 KB Output is correct
108 Runtime error 408 ms 78848 KB Execution killed with signal 11 (could be triggered by violating memory limits)
109 Halted 0 ms 0 KB -