Submission #417470

# Submission time Handle Problem Language Result Execution time Memory
417470 2021-06-03T18:43:32 Z shivensinha4 Mergers (JOI19_mergers) C++17
100 / 100
1295 ms 152372 KB
#include <bits/stdc++.h>
#ifdef mlocal
#include "./Competitive-Code-Library/snippets/prettyprint.h"
#endif
using namespace std;
#define for_(i, s, e) for (int i = s; i < (int) e; i++)
#define for__(i, s, e) for (ll i = s; i < e; i++)
typedef long long ll;
typedef vector<int> vi;
typedef array<int, 2> ii;
#define endl '\n'

int main() {
#ifdef mlocal
	freopen("test.in", "r", stdin);
#endif
	
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	
	int n, k; cin >> n >> k;
	vector<vi> adj(n);
	for_(i, 0, n-1) {
		int a, b; cin >> a >> b;
		a -= 1; b -= 1;
		adj[a].push_back(b); adj[b].push_back(a);
	}
	vi st(n), ct(n);
	for_(i, 0, n) {
		cin >> st[i];
		st[i] -= 1;
		ct[st[i]]++;
	}
	
	vector<map<int, int>> mp(n);
	vi deg(n);
	
	function<void(int,int)> dfs = [&](int p, int parent) {
		if (ct[st[p]] != 1) mp[p][st[p]] = 1;
		
		for (auto i: adj[p]) if (i != parent) {
			dfs(i, p);
			if (!mp[i].size()) {
				deg[p]++;
				deg[i]++;
			}
			else {
				deg[p] += deg[i];
				deg[i] = 0;
			}
			
			if (mp[i].size() > mp[p].size()) mp[p].swap(mp[i]);
			vi rem;
			for (auto &v: mp[i]) {
				mp[p][v.first] += v.second;
				if (mp[p][v.first] == ct[v.first]) rem.push_back(v.first);
			}
			
			for (auto v: rem) mp[p].erase(v);
		}
	};
	
	dfs(0, 0);
	int odd = 0;
	for_(i, 0, n) if (deg[i] == 1) odd++;
	cout << (odd+1)/2 << endl;
}

# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 312 KB Output is correct
7 Correct 1 ms 320 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 0 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 316 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 308 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 312 KB Output is correct
7 Correct 1 ms 320 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 0 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 316 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 308 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 1 ms 204 KB Output is correct
26 Correct 4 ms 716 KB Output is correct
27 Correct 3 ms 844 KB Output is correct
28 Correct 2 ms 972 KB Output is correct
29 Correct 3 ms 588 KB Output is correct
30 Correct 4 ms 972 KB Output is correct
31 Correct 1 ms 204 KB Output is correct
32 Correct 3 ms 1100 KB Output is correct
33 Correct 1 ms 204 KB Output is correct
34 Correct 2 ms 844 KB Output is correct
35 Correct 2 ms 588 KB Output is correct
36 Correct 2 ms 844 KB Output is correct
37 Correct 4 ms 944 KB Output is correct
38 Correct 1 ms 204 KB Output is correct
39 Correct 4 ms 1100 KB Output is correct
40 Correct 2 ms 836 KB Output is correct
41 Correct 3 ms 844 KB Output is correct
42 Correct 4 ms 840 KB Output is correct
43 Correct 3 ms 1356 KB Output is correct
44 Correct 1 ms 204 KB Output is correct
45 Correct 3 ms 716 KB Output is correct
46 Correct 4 ms 844 KB Output is correct
47 Correct 1 ms 204 KB Output is correct
48 Correct 3 ms 844 KB Output is correct
49 Correct 2 ms 716 KB Output is correct
50 Correct 2 ms 844 KB Output is correct
51 Correct 3 ms 716 KB Output is correct
52 Correct 2 ms 820 KB Output is correct
53 Correct 3 ms 716 KB Output is correct
54 Correct 3 ms 972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 312 KB Output is correct
7 Correct 1 ms 320 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 0 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 316 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 308 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 1 ms 204 KB Output is correct
26 Correct 89 ms 17212 KB Output is correct
27 Correct 160 ms 23480 KB Output is correct
28 Correct 2 ms 844 KB Output is correct
29 Correct 1 ms 204 KB Output is correct
30 Correct 1 ms 204 KB Output is correct
31 Correct 102 ms 16768 KB Output is correct
32 Correct 2 ms 844 KB Output is correct
33 Correct 147 ms 34212 KB Output is correct
34 Correct 171 ms 22160 KB Output is correct
35 Correct 2 ms 844 KB Output is correct
36 Correct 130 ms 18776 KB Output is correct
37 Correct 3 ms 844 KB Output is correct
38 Correct 2 ms 844 KB Output is correct
39 Correct 86 ms 17084 KB Output is correct
40 Correct 3 ms 1348 KB Output is correct
41 Correct 113 ms 16960 KB Output is correct
42 Correct 122 ms 22532 KB Output is correct
43 Correct 1 ms 332 KB Output is correct
44 Correct 123 ms 34860 KB Output is correct
45 Correct 138 ms 26952 KB Output is correct
46 Correct 3 ms 844 KB Output is correct
47 Correct 4 ms 972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 89 ms 17156 KB Output is correct
2 Correct 61 ms 12616 KB Output is correct
3 Correct 3 ms 716 KB Output is correct
4 Correct 2 ms 844 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 2 ms 844 KB Output is correct
8 Correct 132 ms 19124 KB Output is correct
9 Correct 2 ms 844 KB Output is correct
10 Correct 106 ms 16768 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 117 ms 16792 KB Output is correct
13 Correct 121 ms 15892 KB Output is correct
14 Correct 89 ms 12096 KB Output is correct
15 Correct 78 ms 17084 KB Output is correct
16 Correct 3 ms 824 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 79 ms 13484 KB Output is correct
19 Correct 98 ms 21692 KB Output is correct
20 Correct 3 ms 844 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 112 ms 16184 KB Output is correct
23 Correct 3 ms 844 KB Output is correct
24 Correct 138 ms 16268 KB Output is correct
25 Correct 101 ms 18500 KB Output is correct
26 Correct 2 ms 716 KB Output is correct
27 Correct 2 ms 844 KB Output is correct
28 Correct 3 ms 836 KB Output is correct
29 Correct 2 ms 844 KB Output is correct
30 Correct 3 ms 716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 312 KB Output is correct
7 Correct 1 ms 320 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 0 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 316 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 308 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 1 ms 204 KB Output is correct
26 Correct 4 ms 716 KB Output is correct
27 Correct 3 ms 844 KB Output is correct
28 Correct 2 ms 972 KB Output is correct
29 Correct 3 ms 588 KB Output is correct
30 Correct 4 ms 972 KB Output is correct
31 Correct 1 ms 204 KB Output is correct
32 Correct 3 ms 1100 KB Output is correct
33 Correct 1 ms 204 KB Output is correct
34 Correct 2 ms 844 KB Output is correct
35 Correct 2 ms 588 KB Output is correct
36 Correct 2 ms 844 KB Output is correct
37 Correct 4 ms 944 KB Output is correct
38 Correct 1 ms 204 KB Output is correct
39 Correct 4 ms 1100 KB Output is correct
40 Correct 2 ms 836 KB Output is correct
41 Correct 3 ms 844 KB Output is correct
42 Correct 4 ms 840 KB Output is correct
43 Correct 3 ms 1356 KB Output is correct
44 Correct 1 ms 204 KB Output is correct
45 Correct 3 ms 716 KB Output is correct
46 Correct 4 ms 844 KB Output is correct
47 Correct 1 ms 204 KB Output is correct
48 Correct 3 ms 844 KB Output is correct
49 Correct 2 ms 716 KB Output is correct
50 Correct 2 ms 844 KB Output is correct
51 Correct 3 ms 716 KB Output is correct
52 Correct 2 ms 820 KB Output is correct
53 Correct 3 ms 716 KB Output is correct
54 Correct 3 ms 972 KB Output is correct
55 Correct 1 ms 204 KB Output is correct
56 Correct 89 ms 17212 KB Output is correct
57 Correct 160 ms 23480 KB Output is correct
58 Correct 2 ms 844 KB Output is correct
59 Correct 1 ms 204 KB Output is correct
60 Correct 1 ms 204 KB Output is correct
61 Correct 102 ms 16768 KB Output is correct
62 Correct 2 ms 844 KB Output is correct
63 Correct 147 ms 34212 KB Output is correct
64 Correct 171 ms 22160 KB Output is correct
65 Correct 2 ms 844 KB Output is correct
66 Correct 130 ms 18776 KB Output is correct
67 Correct 3 ms 844 KB Output is correct
68 Correct 2 ms 844 KB Output is correct
69 Correct 86 ms 17084 KB Output is correct
70 Correct 3 ms 1348 KB Output is correct
71 Correct 113 ms 16960 KB Output is correct
72 Correct 122 ms 22532 KB Output is correct
73 Correct 1 ms 332 KB Output is correct
74 Correct 123 ms 34860 KB Output is correct
75 Correct 138 ms 26952 KB Output is correct
76 Correct 3 ms 844 KB Output is correct
77 Correct 4 ms 972 KB Output is correct
78 Correct 89 ms 17156 KB Output is correct
79 Correct 61 ms 12616 KB Output is correct
80 Correct 3 ms 716 KB Output is correct
81 Correct 2 ms 844 KB Output is correct
82 Correct 1 ms 204 KB Output is correct
83 Correct 1 ms 204 KB Output is correct
84 Correct 2 ms 844 KB Output is correct
85 Correct 132 ms 19124 KB Output is correct
86 Correct 2 ms 844 KB Output is correct
87 Correct 106 ms 16768 KB Output is correct
88 Correct 1 ms 204 KB Output is correct
89 Correct 117 ms 16792 KB Output is correct
90 Correct 121 ms 15892 KB Output is correct
91 Correct 89 ms 12096 KB Output is correct
92 Correct 78 ms 17084 KB Output is correct
93 Correct 3 ms 824 KB Output is correct
94 Correct 1 ms 204 KB Output is correct
95 Correct 79 ms 13484 KB Output is correct
96 Correct 98 ms 21692 KB Output is correct
97 Correct 3 ms 844 KB Output is correct
98 Correct 1 ms 332 KB Output is correct
99 Correct 112 ms 16184 KB Output is correct
100 Correct 3 ms 844 KB Output is correct
101 Correct 138 ms 16268 KB Output is correct
102 Correct 101 ms 18500 KB Output is correct
103 Correct 2 ms 716 KB Output is correct
104 Correct 2 ms 844 KB Output is correct
105 Correct 3 ms 836 KB Output is correct
106 Correct 2 ms 844 KB Output is correct
107 Correct 3 ms 716 KB Output is correct
108 Correct 1041 ms 80716 KB Output is correct
109 Correct 922 ms 125380 KB Output is correct
110 Correct 874 ms 152372 KB Output is correct
111 Correct 721 ms 114080 KB Output is correct
112 Correct 681 ms 90880 KB Output is correct
113 Correct 500 ms 64044 KB Output is correct
114 Correct 727 ms 81444 KB Output is correct
115 Correct 739 ms 81424 KB Output is correct
116 Correct 1137 ms 138428 KB Output is correct
117 Correct 585 ms 57784 KB Output is correct
118 Correct 722 ms 79764 KB Output is correct
119 Correct 583 ms 57796 KB Output is correct
120 Correct 622 ms 92084 KB Output is correct
121 Correct 603 ms 57856 KB Output is correct
122 Correct 1295 ms 145840 KB Output is correct
123 Correct 400 ms 59044 KB Output is correct
124 Correct 620 ms 57012 KB Output is correct