Submission #710000

# Submission time Handle Problem Language Result Execution time Memory
710000 2023-03-15T02:50:31 Z LittleCube Mergers (JOI19_mergers) C++14
100 / 100
1391 ms 130776 KB
#include <bits/stdc++.h>
#define pii pair<int, int>
#define F first
#define S second
using namespace std;

const int P = 20;
int N, K, pre[500005][P], block[500005], deg[500005], split[500005], down[500005], up[500005], t, cc;
vector<int> state[500005];
vector<pii> E[500005];
pii io[500005];


bool ischild(int r, int c)
{
	return io[r].F <= io[c].F && io[c].S <= io[r].S;
}

int lca(int u, int v)
{
	int l = u;
	for(int p = P - 1; p >= 0; p--)
		if(pre[l][p] && !ischild(pre[l][p], v))
			l = pre[l][p];
	if(!ischild(l, v))
		l = pre[l][0];
	return l;
}

void dfs(int u)
{
	io[u].F = ++t;
	for(auto [v, i] : E[u])
		if(v != pre[u][0])
		{
			pre[v][0] = u;
			dfs(v);
		}
	io[u].S = t;
}

int find_edge(int u)
{
	int cur = 0;
	for(auto [v, i] : E[u])
		if(v != pre[u][0])
		{	
			int child = find_edge(v);
			if(child == 0)
				split[i] = 1;
			cur += child;
		}
	// cerr << u << '=' << cur - down[u] + up[u] << '\n';
	return cur - down[u] + up[u];
}

void color(int u, int r)
{
	for(auto [v, i] : E[u])
		if(v != pre[u][0])
		{
			if(split[i])
			{
				// cerr << u << ' ' << v << '\n';	
				deg[r]++, deg[++cc]++;
				color(v, cc);
			}
			else
				color(v, r);
		}
}

signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	cin >> N >> K;
	for(int i = 1; i < N; i++)
	{
		int A, B;
		cin >> A >> B;
		E[A].emplace_back(pii(B, i));
		E[B].emplace_back(pii(A, i));
	}
	dfs(1);
	for(int i = 1; i <= N; i++)
	{
		int S;
		cin >> S;
		state[S].emplace_back(i);
	}
	for(int p = 1; p < P; p++)
		for(int i = 1; i <= N; i++)
			pre[i][p] = pre[pre[i][p - 1]][p - 1];
	for(int i = 1; i <= K; i++)
	{
		for(int j = 1; j < state[i].size(); j++)
		{
			int u = state[i][j - 1], v = state[i][j], l = lca(u, v);
			if(l == u)
				up[v]++, down[u]++;
			else if(l == v)
				up[u]++, down[v]++;
			else
				up[u]++, up[v]++, down[l] += 2;
		}
	}
	find_edge(1);
	color(1, ++cc);
	int cnt = 0;
	for(int i = 1; i <= N; i++)
		if(deg[i] == 1)
			cnt++;
	cout << (cnt + 1) / 2 << '\n';
}

Compilation message

mergers.cpp: In function 'void dfs(int)':
mergers.cpp:33:11: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   33 |  for(auto [v, i] : E[u])
      |           ^
mergers.cpp: In function 'int find_edge(int)':
mergers.cpp:45:11: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   45 |  for(auto [v, i] : E[u])
      |           ^
mergers.cpp: In function 'void color(int, int)':
mergers.cpp:59:11: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   59 |  for(auto [v, i] : E[u])
      |           ^
mergers.cpp: In function 'int main()':
mergers.cpp:97:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   97 |   for(int j = 1; j < state[i].size(); j++)
      |                  ~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23764 KB Output is correct
2 Correct 12 ms 23764 KB Output is correct
3 Correct 13 ms 23800 KB Output is correct
4 Correct 12 ms 23804 KB Output is correct
5 Correct 14 ms 23764 KB Output is correct
6 Correct 13 ms 23836 KB Output is correct
7 Correct 13 ms 23764 KB Output is correct
8 Correct 13 ms 23860 KB Output is correct
9 Correct 13 ms 23764 KB Output is correct
10 Correct 13 ms 23764 KB Output is correct
11 Correct 12 ms 23824 KB Output is correct
12 Correct 12 ms 23764 KB Output is correct
13 Correct 13 ms 23764 KB Output is correct
14 Correct 13 ms 23748 KB Output is correct
15 Correct 13 ms 23764 KB Output is correct
16 Correct 12 ms 23764 KB Output is correct
17 Correct 15 ms 23860 KB Output is correct
18 Correct 12 ms 23820 KB Output is correct
19 Correct 12 ms 23828 KB Output is correct
20 Correct 13 ms 23764 KB Output is correct
21 Correct 13 ms 23860 KB Output is correct
22 Correct 13 ms 23764 KB Output is correct
23 Correct 13 ms 23824 KB Output is correct
24 Correct 12 ms 23764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23764 KB Output is correct
2 Correct 12 ms 23764 KB Output is correct
3 Correct 13 ms 23800 KB Output is correct
4 Correct 12 ms 23804 KB Output is correct
5 Correct 14 ms 23764 KB Output is correct
6 Correct 13 ms 23836 KB Output is correct
7 Correct 13 ms 23764 KB Output is correct
8 Correct 13 ms 23860 KB Output is correct
9 Correct 13 ms 23764 KB Output is correct
10 Correct 13 ms 23764 KB Output is correct
11 Correct 12 ms 23824 KB Output is correct
12 Correct 12 ms 23764 KB Output is correct
13 Correct 13 ms 23764 KB Output is correct
14 Correct 13 ms 23748 KB Output is correct
15 Correct 13 ms 23764 KB Output is correct
16 Correct 12 ms 23764 KB Output is correct
17 Correct 15 ms 23860 KB Output is correct
18 Correct 12 ms 23820 KB Output is correct
19 Correct 12 ms 23828 KB Output is correct
20 Correct 13 ms 23764 KB Output is correct
21 Correct 13 ms 23860 KB Output is correct
22 Correct 13 ms 23764 KB Output is correct
23 Correct 13 ms 23824 KB Output is correct
24 Correct 12 ms 23764 KB Output is correct
25 Correct 13 ms 23852 KB Output is correct
26 Correct 14 ms 24276 KB Output is correct
27 Correct 13 ms 24276 KB Output is correct
28 Correct 14 ms 24472 KB Output is correct
29 Correct 15 ms 24348 KB Output is correct
30 Correct 14 ms 24292 KB Output is correct
31 Correct 13 ms 23764 KB Output is correct
32 Correct 14 ms 24404 KB Output is correct
33 Correct 13 ms 23764 KB Output is correct
34 Correct 14 ms 24276 KB Output is correct
35 Correct 13 ms 24344 KB Output is correct
36 Correct 14 ms 24288 KB Output is correct
37 Correct 16 ms 24304 KB Output is correct
38 Correct 13 ms 23824 KB Output is correct
39 Correct 14 ms 24256 KB Output is correct
40 Correct 14 ms 24276 KB Output is correct
41 Correct 15 ms 24276 KB Output is correct
42 Correct 14 ms 24348 KB Output is correct
43 Correct 15 ms 24344 KB Output is correct
44 Correct 13 ms 23808 KB Output is correct
45 Correct 15 ms 24404 KB Output is correct
46 Correct 14 ms 24276 KB Output is correct
47 Correct 13 ms 23864 KB Output is correct
48 Correct 14 ms 24276 KB Output is correct
49 Correct 14 ms 24288 KB Output is correct
50 Correct 14 ms 24404 KB Output is correct
51 Correct 15 ms 24292 KB Output is correct
52 Correct 14 ms 24276 KB Output is correct
53 Correct 14 ms 24348 KB Output is correct
54 Correct 14 ms 24244 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23764 KB Output is correct
2 Correct 12 ms 23764 KB Output is correct
3 Correct 13 ms 23800 KB Output is correct
4 Correct 12 ms 23804 KB Output is correct
5 Correct 14 ms 23764 KB Output is correct
6 Correct 13 ms 23836 KB Output is correct
7 Correct 13 ms 23764 KB Output is correct
8 Correct 13 ms 23860 KB Output is correct
9 Correct 13 ms 23764 KB Output is correct
10 Correct 13 ms 23764 KB Output is correct
11 Correct 12 ms 23824 KB Output is correct
12 Correct 12 ms 23764 KB Output is correct
13 Correct 13 ms 23764 KB Output is correct
14 Correct 13 ms 23748 KB Output is correct
15 Correct 13 ms 23764 KB Output is correct
16 Correct 12 ms 23764 KB Output is correct
17 Correct 15 ms 23860 KB Output is correct
18 Correct 12 ms 23820 KB Output is correct
19 Correct 12 ms 23828 KB Output is correct
20 Correct 13 ms 23764 KB Output is correct
21 Correct 13 ms 23860 KB Output is correct
22 Correct 13 ms 23764 KB Output is correct
23 Correct 13 ms 23824 KB Output is correct
24 Correct 12 ms 23764 KB Output is correct
25 Correct 14 ms 23804 KB Output is correct
26 Correct 80 ms 38772 KB Output is correct
27 Correct 103 ms 38800 KB Output is correct
28 Correct 14 ms 24216 KB Output is correct
29 Correct 12 ms 23764 KB Output is correct
30 Correct 12 ms 23828 KB Output is correct
31 Correct 113 ms 39012 KB Output is correct
32 Correct 13 ms 24276 KB Output is correct
33 Correct 172 ms 44176 KB Output is correct
34 Correct 108 ms 38956 KB Output is correct
35 Correct 15 ms 24216 KB Output is correct
36 Correct 140 ms 39072 KB Output is correct
37 Correct 15 ms 24276 KB Output is correct
38 Correct 15 ms 24280 KB Output is correct
39 Correct 78 ms 38644 KB Output is correct
40 Correct 15 ms 24472 KB Output is correct
41 Correct 94 ms 38816 KB Output is correct
42 Correct 176 ms 40276 KB Output is correct
43 Correct 13 ms 23820 KB Output is correct
44 Correct 162 ms 44280 KB Output is correct
45 Correct 169 ms 42468 KB Output is correct
46 Correct 13 ms 24288 KB Output is correct
47 Correct 16 ms 24220 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 77 ms 38720 KB Output is correct
2 Correct 81 ms 41988 KB Output is correct
3 Correct 18 ms 24344 KB Output is correct
4 Correct 15 ms 24220 KB Output is correct
5 Correct 14 ms 23872 KB Output is correct
6 Correct 13 ms 23764 KB Output is correct
7 Correct 14 ms 24224 KB Output is correct
8 Correct 120 ms 40576 KB Output is correct
9 Correct 15 ms 24276 KB Output is correct
10 Correct 120 ms 39412 KB Output is correct
11 Correct 13 ms 23816 KB Output is correct
12 Correct 145 ms 39060 KB Output is correct
13 Correct 128 ms 40672 KB Output is correct
14 Correct 115 ms 41784 KB Output is correct
15 Correct 80 ms 38576 KB Output is correct
16 Correct 15 ms 24240 KB Output is correct
17 Correct 13 ms 23816 KB Output is correct
18 Correct 85 ms 41976 KB Output is correct
19 Correct 143 ms 44400 KB Output is correct
20 Correct 15 ms 24276 KB Output is correct
21 Correct 14 ms 23764 KB Output is correct
22 Correct 79 ms 40392 KB Output is correct
23 Correct 16 ms 24296 KB Output is correct
24 Correct 128 ms 39660 KB Output is correct
25 Correct 117 ms 44136 KB Output is correct
26 Correct 16 ms 24344 KB Output is correct
27 Correct 15 ms 24420 KB Output is correct
28 Correct 16 ms 24312 KB Output is correct
29 Correct 14 ms 24320 KB Output is correct
30 Correct 14 ms 24276 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23764 KB Output is correct
2 Correct 12 ms 23764 KB Output is correct
3 Correct 13 ms 23800 KB Output is correct
4 Correct 12 ms 23804 KB Output is correct
5 Correct 14 ms 23764 KB Output is correct
6 Correct 13 ms 23836 KB Output is correct
7 Correct 13 ms 23764 KB Output is correct
8 Correct 13 ms 23860 KB Output is correct
9 Correct 13 ms 23764 KB Output is correct
10 Correct 13 ms 23764 KB Output is correct
11 Correct 12 ms 23824 KB Output is correct
12 Correct 12 ms 23764 KB Output is correct
13 Correct 13 ms 23764 KB Output is correct
14 Correct 13 ms 23748 KB Output is correct
15 Correct 13 ms 23764 KB Output is correct
16 Correct 12 ms 23764 KB Output is correct
17 Correct 15 ms 23860 KB Output is correct
18 Correct 12 ms 23820 KB Output is correct
19 Correct 12 ms 23828 KB Output is correct
20 Correct 13 ms 23764 KB Output is correct
21 Correct 13 ms 23860 KB Output is correct
22 Correct 13 ms 23764 KB Output is correct
23 Correct 13 ms 23824 KB Output is correct
24 Correct 12 ms 23764 KB Output is correct
25 Correct 13 ms 23852 KB Output is correct
26 Correct 14 ms 24276 KB Output is correct
27 Correct 13 ms 24276 KB Output is correct
28 Correct 14 ms 24472 KB Output is correct
29 Correct 15 ms 24348 KB Output is correct
30 Correct 14 ms 24292 KB Output is correct
31 Correct 13 ms 23764 KB Output is correct
32 Correct 14 ms 24404 KB Output is correct
33 Correct 13 ms 23764 KB Output is correct
34 Correct 14 ms 24276 KB Output is correct
35 Correct 13 ms 24344 KB Output is correct
36 Correct 14 ms 24288 KB Output is correct
37 Correct 16 ms 24304 KB Output is correct
38 Correct 13 ms 23824 KB Output is correct
39 Correct 14 ms 24256 KB Output is correct
40 Correct 14 ms 24276 KB Output is correct
41 Correct 15 ms 24276 KB Output is correct
42 Correct 14 ms 24348 KB Output is correct
43 Correct 15 ms 24344 KB Output is correct
44 Correct 13 ms 23808 KB Output is correct
45 Correct 15 ms 24404 KB Output is correct
46 Correct 14 ms 24276 KB Output is correct
47 Correct 13 ms 23864 KB Output is correct
48 Correct 14 ms 24276 KB Output is correct
49 Correct 14 ms 24288 KB Output is correct
50 Correct 14 ms 24404 KB Output is correct
51 Correct 15 ms 24292 KB Output is correct
52 Correct 14 ms 24276 KB Output is correct
53 Correct 14 ms 24348 KB Output is correct
54 Correct 14 ms 24244 KB Output is correct
55 Correct 14 ms 23804 KB Output is correct
56 Correct 80 ms 38772 KB Output is correct
57 Correct 103 ms 38800 KB Output is correct
58 Correct 14 ms 24216 KB Output is correct
59 Correct 12 ms 23764 KB Output is correct
60 Correct 12 ms 23828 KB Output is correct
61 Correct 113 ms 39012 KB Output is correct
62 Correct 13 ms 24276 KB Output is correct
63 Correct 172 ms 44176 KB Output is correct
64 Correct 108 ms 38956 KB Output is correct
65 Correct 15 ms 24216 KB Output is correct
66 Correct 140 ms 39072 KB Output is correct
67 Correct 15 ms 24276 KB Output is correct
68 Correct 15 ms 24280 KB Output is correct
69 Correct 78 ms 38644 KB Output is correct
70 Correct 15 ms 24472 KB Output is correct
71 Correct 94 ms 38816 KB Output is correct
72 Correct 176 ms 40276 KB Output is correct
73 Correct 13 ms 23820 KB Output is correct
74 Correct 162 ms 44280 KB Output is correct
75 Correct 169 ms 42468 KB Output is correct
76 Correct 13 ms 24288 KB Output is correct
77 Correct 16 ms 24220 KB Output is correct
78 Correct 77 ms 38720 KB Output is correct
79 Correct 81 ms 41988 KB Output is correct
80 Correct 18 ms 24344 KB Output is correct
81 Correct 15 ms 24220 KB Output is correct
82 Correct 14 ms 23872 KB Output is correct
83 Correct 13 ms 23764 KB Output is correct
84 Correct 14 ms 24224 KB Output is correct
85 Correct 120 ms 40576 KB Output is correct
86 Correct 15 ms 24276 KB Output is correct
87 Correct 120 ms 39412 KB Output is correct
88 Correct 13 ms 23816 KB Output is correct
89 Correct 145 ms 39060 KB Output is correct
90 Correct 128 ms 40672 KB Output is correct
91 Correct 115 ms 41784 KB Output is correct
92 Correct 80 ms 38576 KB Output is correct
93 Correct 15 ms 24240 KB Output is correct
94 Correct 13 ms 23816 KB Output is correct
95 Correct 85 ms 41976 KB Output is correct
96 Correct 143 ms 44400 KB Output is correct
97 Correct 15 ms 24276 KB Output is correct
98 Correct 14 ms 23764 KB Output is correct
99 Correct 79 ms 40392 KB Output is correct
100 Correct 16 ms 24296 KB Output is correct
101 Correct 128 ms 39660 KB Output is correct
102 Correct 117 ms 44136 KB Output is correct
103 Correct 16 ms 24344 KB Output is correct
104 Correct 15 ms 24420 KB Output is correct
105 Correct 16 ms 24312 KB Output is correct
106 Correct 14 ms 24320 KB Output is correct
107 Correct 14 ms 24276 KB Output is correct
108 Correct 549 ms 106872 KB Output is correct
109 Correct 1391 ms 110980 KB Output is correct
110 Correct 1285 ms 119940 KB Output is correct
111 Correct 928 ms 130776 KB Output is correct
112 Correct 951 ms 126492 KB Output is correct
113 Correct 510 ms 116124 KB Output is correct
114 Correct 639 ms 98912 KB Output is correct
115 Correct 651 ms 98980 KB Output is correct
116 Correct 839 ms 105612 KB Output is correct
117 Correct 777 ms 115096 KB Output is correct
118 Correct 827 ms 101244 KB Output is correct
119 Correct 783 ms 115104 KB Output is correct
120 Correct 865 ms 127884 KB Output is correct
121 Correct 743 ms 115848 KB Output is correct
122 Correct 786 ms 110396 KB Output is correct
123 Correct 510 ms 116040 KB Output is correct
124 Correct 931 ms 113036 KB Output is correct