Submission #417468

# Submission time Handle Problem Language Result Execution time Memory
417468 2021-06-03T18:41:43 Z shivensinha4 Mergers (JOI19_mergers) C++17
100 / 100
1283 ms 159636 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);
	vector<bool> rem(n);
	
	int ans = 0, oc = 0;
	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];
				rem[i] = true;
			}
			
			if (mp[i].size() > mp[p].size()) swap(mp[p], 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 (!rem[i] and deg[i] == 1) odd++;
	cout << (odd+1)/2 << endl;
}

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:39:6: warning: unused variable 'ans' [-Wunused-variable]
   39 |  int ans = 0, oc = 0;
      |      ^~~
mergers.cpp:39:15: warning: unused variable 'oc' [-Wunused-variable]
   39 |  int ans = 0, oc = 0;
      |               ^~
# 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 0 ms 332 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 1 ms 204 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 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 312 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 204 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 0 ms 332 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 1 ms 204 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 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 312 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 1 ms 204 KB Output is correct
26 Correct 3 ms 716 KB Output is correct
27 Correct 2 ms 844 KB Output is correct
28 Correct 2 ms 844 KB Output is correct
29 Correct 2 ms 716 KB Output is correct
30 Correct 4 ms 972 KB Output is correct
31 Correct 1 ms 204 KB Output is correct
32 Correct 2 ms 1100 KB Output is correct
33 Correct 1 ms 204 KB Output is correct
34 Correct 2 ms 832 KB Output is correct
35 Correct 2 ms 716 KB Output is correct
36 Correct 2 ms 844 KB Output is correct
37 Correct 4 ms 976 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 716 KB Output is correct
41 Correct 2 ms 844 KB Output is correct
42 Correct 3 ms 832 KB Output is correct
43 Correct 3 ms 1356 KB Output is correct
44 Correct 1 ms 312 KB Output is correct
45 Correct 2 ms 708 KB Output is correct
46 Correct 3 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 788 KB Output is correct
52 Correct 2 ms 844 KB Output is correct
53 Correct 3 ms 716 KB Output is correct
54 Correct 4 ms 968 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 0 ms 332 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 1 ms 204 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 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 312 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 1 ms 204 KB Output is correct
26 Correct 74 ms 18084 KB Output is correct
27 Correct 132 ms 24504 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 88 ms 17884 KB Output is correct
32 Correct 2 ms 844 KB Output is correct
33 Correct 112 ms 35152 KB Output is correct
34 Correct 115 ms 23220 KB Output is correct
35 Correct 2 ms 844 KB Output is correct
36 Correct 105 ms 19760 KB Output is correct
37 Correct 2 ms 836 KB Output is correct
38 Correct 2 ms 844 KB Output is correct
39 Correct 78 ms 18080 KB Output is correct
40 Correct 3 ms 1360 KB Output is correct
41 Correct 100 ms 17832 KB Output is correct
42 Correct 120 ms 23580 KB Output is correct
43 Correct 1 ms 308 KB Output is correct
44 Correct 127 ms 36004 KB Output is correct
45 Correct 118 ms 28012 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 76 ms 16712 KB Output is correct
2 Correct 56 ms 11964 KB Output is correct
3 Correct 2 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 3 ms 844 KB Output is correct
8 Correct 113 ms 20564 KB Output is correct
9 Correct 2 ms 844 KB Output is correct
10 Correct 90 ms 17896 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 96 ms 17948 KB Output is correct
13 Correct 104 ms 17176 KB Output is correct
14 Correct 70 ms 13484 KB Output is correct
15 Correct 73 ms 18148 KB Output is correct
16 Correct 3 ms 844 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 65 ms 14844 KB Output is correct
19 Correct 77 ms 22980 KB Output is correct
20 Correct 4 ms 844 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 108 ms 17500 KB Output is correct
23 Correct 3 ms 844 KB Output is correct
24 Correct 90 ms 17520 KB Output is correct
25 Correct 76 ms 19780 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 844 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 0 ms 332 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 1 ms 204 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 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 312 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 1 ms 204 KB Output is correct
26 Correct 3 ms 716 KB Output is correct
27 Correct 2 ms 844 KB Output is correct
28 Correct 2 ms 844 KB Output is correct
29 Correct 2 ms 716 KB Output is correct
30 Correct 4 ms 972 KB Output is correct
31 Correct 1 ms 204 KB Output is correct
32 Correct 2 ms 1100 KB Output is correct
33 Correct 1 ms 204 KB Output is correct
34 Correct 2 ms 832 KB Output is correct
35 Correct 2 ms 716 KB Output is correct
36 Correct 2 ms 844 KB Output is correct
37 Correct 4 ms 976 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 716 KB Output is correct
41 Correct 2 ms 844 KB Output is correct
42 Correct 3 ms 832 KB Output is correct
43 Correct 3 ms 1356 KB Output is correct
44 Correct 1 ms 312 KB Output is correct
45 Correct 2 ms 708 KB Output is correct
46 Correct 3 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 788 KB Output is correct
52 Correct 2 ms 844 KB Output is correct
53 Correct 3 ms 716 KB Output is correct
54 Correct 4 ms 968 KB Output is correct
55 Correct 1 ms 204 KB Output is correct
56 Correct 74 ms 18084 KB Output is correct
57 Correct 132 ms 24504 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 88 ms 17884 KB Output is correct
62 Correct 2 ms 844 KB Output is correct
63 Correct 112 ms 35152 KB Output is correct
64 Correct 115 ms 23220 KB Output is correct
65 Correct 2 ms 844 KB Output is correct
66 Correct 105 ms 19760 KB Output is correct
67 Correct 2 ms 836 KB Output is correct
68 Correct 2 ms 844 KB Output is correct
69 Correct 78 ms 18080 KB Output is correct
70 Correct 3 ms 1360 KB Output is correct
71 Correct 100 ms 17832 KB Output is correct
72 Correct 120 ms 23580 KB Output is correct
73 Correct 1 ms 308 KB Output is correct
74 Correct 127 ms 36004 KB Output is correct
75 Correct 118 ms 28012 KB Output is correct
76 Correct 3 ms 844 KB Output is correct
77 Correct 4 ms 972 KB Output is correct
78 Correct 76 ms 16712 KB Output is correct
79 Correct 56 ms 11964 KB Output is correct
80 Correct 2 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 3 ms 844 KB Output is correct
85 Correct 113 ms 20564 KB Output is correct
86 Correct 2 ms 844 KB Output is correct
87 Correct 90 ms 17896 KB Output is correct
88 Correct 1 ms 204 KB Output is correct
89 Correct 96 ms 17948 KB Output is correct
90 Correct 104 ms 17176 KB Output is correct
91 Correct 70 ms 13484 KB Output is correct
92 Correct 73 ms 18148 KB Output is correct
93 Correct 3 ms 844 KB Output is correct
94 Correct 1 ms 204 KB Output is correct
95 Correct 65 ms 14844 KB Output is correct
96 Correct 77 ms 22980 KB Output is correct
97 Correct 4 ms 844 KB Output is correct
98 Correct 1 ms 332 KB Output is correct
99 Correct 108 ms 17500 KB Output is correct
100 Correct 3 ms 844 KB Output is correct
101 Correct 90 ms 17520 KB Output is correct
102 Correct 76 ms 19780 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 844 KB Output is correct
106 Correct 2 ms 844 KB Output is correct
107 Correct 3 ms 716 KB Output is correct
108 Correct 873 ms 90288 KB Output is correct
109 Correct 794 ms 132628 KB Output is correct
110 Correct 772 ms 159636 KB Output is correct
111 Correct 580 ms 123720 KB Output is correct
112 Correct 573 ms 100164 KB Output is correct
113 Correct 464 ms 73772 KB Output is correct
114 Correct 710 ms 88696 KB Output is correct
115 Correct 689 ms 88772 KB Output is correct
116 Correct 1136 ms 147756 KB Output is correct
117 Correct 626 ms 67668 KB Output is correct
118 Correct 754 ms 87852 KB Output is correct
119 Correct 612 ms 67848 KB Output is correct
120 Correct 646 ms 101960 KB Output is correct
121 Correct 593 ms 67572 KB Output is correct
122 Correct 1283 ms 155628 KB Output is correct
123 Correct 432 ms 69144 KB Output is correct
124 Correct 643 ms 66720 KB Output is correct