Submission #987113

# Submission time Handle Problem Language Result Execution time Memory
987113 2024-05-21T22:30:10 Z OAleksa Mergers (JOI19_mergers) C++14
100 / 100
1668 ms 188176 KB
#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
const int N = 5e5 + 69;
int n, k, a[N], ans, cc[N], is[N], have[N];
vector<int> g[N], c[N];
vector<int> t[N];
map<int, int> cnt[N];
void dfs(int v, int p) {
	cnt[v][a[v]] = 1;
	if (c[a[v]].size() == 1)
		have[v] = 1;
	for (auto u : g[v]) {
		if (u == p)
			continue;
		dfs(u, v);
		int o = have[v];
		if (cnt[u].size() > cnt[v].size()) {
			have[v] += have[u];
			swap(cnt[u], cnt[v]);
		}
		else
			o = 0;
		for (auto j : cnt[u]) {
			cnt[v][j.f] += j.s;
			have[v] += (cnt[v][j.f] == (int)c[j.f].size());
		}
		cnt[u].clear();
		have[v] -= o;
	}
	assert(have[v] <= cnt[v].size());
	if (have[v] == cnt[v].size())
		cc[v] = 1;
}
void build(int v, int p, int c) {
	int x = c;
	if (cc[v] && v > 1) {
		t[v].push_back(c);
		t[c].push_back(v);
		x = v;
	}
	for (auto u : g[v]) {
		if (u == p)
			continue;
		build(u, v, x);
	}
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int tt = 1;
  //cin >> tt;
  while (tt--) {
  	cin >> n >> k;
  	for (int i = 1;i <= n - 1;i++) {
  		int a, b;
  		cin >> a >> b;
  		g[a].push_back(b);
  		g[b].push_back(a);
  	}
  	for (int i = 1;i <= n;i++) {
  		cin >> a[i];
  		c[a[i]].push_back(i);
  	}
  	dfs(1, 0);
  	build(1, 0, 1);
 		int o = 0;
 		assert(cc[1]);
 		for (int i = 1;i <= n;i++) {
 			if (cc[i] && (int)t[i].size() == 1)
				o += 1;		
  	}
  	ans = (o + 1) / 2;
  	cout << ans << '\n';
  }
  return 0; 
}

Compilation message

In file included from /usr/include/c++/10/cassert:44,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:33,
                 from mergers.cpp:1:
mergers.cpp: In function 'void dfs(int, int)':
mergers.cpp:32:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::map<int, int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   32 |  assert(have[v] <= cnt[v].size());
      |         ~~~~~~~~^~~~~~~~~~~~~~~~
mergers.cpp:33:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::map<int, int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   33 |  if (have[v] == cnt[v].size())
      |      ~~~~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 26 ms 62044 KB Output is correct
2 Correct 19 ms 62184 KB Output is correct
3 Correct 17 ms 60252 KB Output is correct
4 Correct 16 ms 60080 KB Output is correct
5 Correct 18 ms 59996 KB Output is correct
6 Correct 17 ms 59996 KB Output is correct
7 Correct 17 ms 59996 KB Output is correct
8 Correct 17 ms 60112 KB Output is correct
9 Correct 20 ms 62044 KB Output is correct
10 Correct 19 ms 62200 KB Output is correct
11 Correct 18 ms 64092 KB Output is correct
12 Correct 19 ms 64092 KB Output is correct
13 Correct 20 ms 64344 KB Output is correct
14 Correct 20 ms 64092 KB Output is correct
15 Correct 16 ms 64092 KB Output is correct
16 Correct 20 ms 64092 KB Output is correct
17 Correct 15 ms 64212 KB Output is correct
18 Correct 16 ms 64088 KB Output is correct
19 Correct 15 ms 64092 KB Output is correct
20 Correct 19 ms 64240 KB Output is correct
21 Correct 19 ms 64092 KB Output is correct
22 Correct 19 ms 64340 KB Output is correct
23 Correct 18 ms 64092 KB Output is correct
24 Correct 18 ms 64092 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 62044 KB Output is correct
2 Correct 19 ms 62184 KB Output is correct
3 Correct 17 ms 60252 KB Output is correct
4 Correct 16 ms 60080 KB Output is correct
5 Correct 18 ms 59996 KB Output is correct
6 Correct 17 ms 59996 KB Output is correct
7 Correct 17 ms 59996 KB Output is correct
8 Correct 17 ms 60112 KB Output is correct
9 Correct 20 ms 62044 KB Output is correct
10 Correct 19 ms 62200 KB Output is correct
11 Correct 18 ms 64092 KB Output is correct
12 Correct 19 ms 64092 KB Output is correct
13 Correct 20 ms 64344 KB Output is correct
14 Correct 20 ms 64092 KB Output is correct
15 Correct 16 ms 64092 KB Output is correct
16 Correct 20 ms 64092 KB Output is correct
17 Correct 15 ms 64212 KB Output is correct
18 Correct 16 ms 64088 KB Output is correct
19 Correct 15 ms 64092 KB Output is correct
20 Correct 19 ms 64240 KB Output is correct
21 Correct 19 ms 64092 KB Output is correct
22 Correct 19 ms 64340 KB Output is correct
23 Correct 18 ms 64092 KB Output is correct
24 Correct 18 ms 64092 KB Output is correct
25 Correct 19 ms 64092 KB Output is correct
26 Correct 21 ms 64348 KB Output is correct
27 Correct 20 ms 64348 KB Output is correct
28 Correct 19 ms 64856 KB Output is correct
29 Correct 22 ms 64604 KB Output is correct
30 Correct 21 ms 64348 KB Output is correct
31 Correct 20 ms 64092 KB Output is correct
32 Correct 25 ms 64860 KB Output is correct
33 Correct 20 ms 64092 KB Output is correct
34 Correct 20 ms 64348 KB Output is correct
35 Correct 22 ms 64608 KB Output is correct
36 Correct 20 ms 64348 KB Output is correct
37 Correct 22 ms 64348 KB Output is correct
38 Correct 19 ms 64092 KB Output is correct
39 Correct 22 ms 64480 KB Output is correct
40 Correct 21 ms 64344 KB Output is correct
41 Correct 17 ms 64344 KB Output is correct
42 Correct 17 ms 64348 KB Output is correct
43 Correct 21 ms 64860 KB Output is correct
44 Correct 16 ms 64088 KB Output is correct
45 Correct 22 ms 64600 KB Output is correct
46 Correct 24 ms 64432 KB Output is correct
47 Correct 19 ms 64092 KB Output is correct
48 Correct 17 ms 64348 KB Output is correct
49 Correct 21 ms 64604 KB Output is correct
50 Correct 21 ms 64756 KB Output is correct
51 Correct 20 ms 64344 KB Output is correct
52 Correct 20 ms 64344 KB Output is correct
53 Correct 20 ms 64348 KB Output is correct
54 Correct 20 ms 64184 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 62044 KB Output is correct
2 Correct 19 ms 62184 KB Output is correct
3 Correct 17 ms 60252 KB Output is correct
4 Correct 16 ms 60080 KB Output is correct
5 Correct 18 ms 59996 KB Output is correct
6 Correct 17 ms 59996 KB Output is correct
7 Correct 17 ms 59996 KB Output is correct
8 Correct 17 ms 60112 KB Output is correct
9 Correct 20 ms 62044 KB Output is correct
10 Correct 19 ms 62200 KB Output is correct
11 Correct 18 ms 64092 KB Output is correct
12 Correct 19 ms 64092 KB Output is correct
13 Correct 20 ms 64344 KB Output is correct
14 Correct 20 ms 64092 KB Output is correct
15 Correct 16 ms 64092 KB Output is correct
16 Correct 20 ms 64092 KB Output is correct
17 Correct 15 ms 64212 KB Output is correct
18 Correct 16 ms 64088 KB Output is correct
19 Correct 15 ms 64092 KB Output is correct
20 Correct 19 ms 64240 KB Output is correct
21 Correct 19 ms 64092 KB Output is correct
22 Correct 19 ms 64340 KB Output is correct
23 Correct 18 ms 64092 KB Output is correct
24 Correct 18 ms 64092 KB Output is correct
25 Correct 19 ms 64092 KB Output is correct
26 Correct 68 ms 68556 KB Output is correct
27 Correct 83 ms 68000 KB Output is correct
28 Correct 20 ms 64344 KB Output is correct
29 Correct 16 ms 64088 KB Output is correct
30 Correct 19 ms 64092 KB Output is correct
31 Correct 100 ms 68044 KB Output is correct
32 Correct 20 ms 64348 KB Output is correct
33 Correct 129 ms 88012 KB Output is correct
34 Correct 77 ms 67876 KB Output is correct
35 Correct 20 ms 64376 KB Output is correct
36 Correct 75 ms 70308 KB Output is correct
37 Correct 17 ms 64344 KB Output is correct
38 Correct 17 ms 64344 KB Output is correct
39 Correct 62 ms 68168 KB Output is correct
40 Correct 20 ms 64856 KB Output is correct
41 Correct 67 ms 67924 KB Output is correct
42 Correct 97 ms 74576 KB Output is correct
43 Correct 19 ms 64088 KB Output is correct
44 Correct 117 ms 88720 KB Output is correct
45 Correct 95 ms 79440 KB Output is correct
46 Correct 17 ms 64344 KB Output is correct
47 Correct 21 ms 64344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 63 ms 68240 KB Output is correct
2 Correct 133 ms 79048 KB Output is correct
3 Correct 18 ms 64348 KB Output is correct
4 Correct 16 ms 64348 KB Output is correct
5 Correct 15 ms 64088 KB Output is correct
6 Correct 16 ms 64168 KB Output is correct
7 Correct 18 ms 64184 KB Output is correct
8 Correct 124 ms 71748 KB Output is correct
9 Correct 21 ms 64344 KB Output is correct
10 Correct 71 ms 67924 KB Output is correct
11 Correct 15 ms 64092 KB Output is correct
12 Correct 64 ms 68048 KB Output is correct
13 Correct 137 ms 71764 KB Output is correct
14 Correct 191 ms 78416 KB Output is correct
15 Correct 78 ms 68172 KB Output is correct
16 Correct 21 ms 64348 KB Output is correct
17 Correct 19 ms 64092 KB Output is correct
18 Correct 141 ms 77600 KB Output is correct
19 Correct 192 ms 87120 KB Output is correct
20 Correct 21 ms 64344 KB Output is correct
21 Correct 18 ms 64252 KB Output is correct
22 Correct 107 ms 72316 KB Output is correct
23 Correct 18 ms 64348 KB Output is correct
24 Correct 80 ms 68944 KB Output is correct
25 Correct 203 ms 84240 KB Output is correct
26 Correct 17 ms 64604 KB Output is correct
27 Correct 22 ms 64856 KB Output is correct
28 Correct 17 ms 64348 KB Output is correct
29 Correct 16 ms 64204 KB Output is correct
30 Correct 21 ms 64344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 62044 KB Output is correct
2 Correct 19 ms 62184 KB Output is correct
3 Correct 17 ms 60252 KB Output is correct
4 Correct 16 ms 60080 KB Output is correct
5 Correct 18 ms 59996 KB Output is correct
6 Correct 17 ms 59996 KB Output is correct
7 Correct 17 ms 59996 KB Output is correct
8 Correct 17 ms 60112 KB Output is correct
9 Correct 20 ms 62044 KB Output is correct
10 Correct 19 ms 62200 KB Output is correct
11 Correct 18 ms 64092 KB Output is correct
12 Correct 19 ms 64092 KB Output is correct
13 Correct 20 ms 64344 KB Output is correct
14 Correct 20 ms 64092 KB Output is correct
15 Correct 16 ms 64092 KB Output is correct
16 Correct 20 ms 64092 KB Output is correct
17 Correct 15 ms 64212 KB Output is correct
18 Correct 16 ms 64088 KB Output is correct
19 Correct 15 ms 64092 KB Output is correct
20 Correct 19 ms 64240 KB Output is correct
21 Correct 19 ms 64092 KB Output is correct
22 Correct 19 ms 64340 KB Output is correct
23 Correct 18 ms 64092 KB Output is correct
24 Correct 18 ms 64092 KB Output is correct
25 Correct 19 ms 64092 KB Output is correct
26 Correct 21 ms 64348 KB Output is correct
27 Correct 20 ms 64348 KB Output is correct
28 Correct 19 ms 64856 KB Output is correct
29 Correct 22 ms 64604 KB Output is correct
30 Correct 21 ms 64348 KB Output is correct
31 Correct 20 ms 64092 KB Output is correct
32 Correct 25 ms 64860 KB Output is correct
33 Correct 20 ms 64092 KB Output is correct
34 Correct 20 ms 64348 KB Output is correct
35 Correct 22 ms 64608 KB Output is correct
36 Correct 20 ms 64348 KB Output is correct
37 Correct 22 ms 64348 KB Output is correct
38 Correct 19 ms 64092 KB Output is correct
39 Correct 22 ms 64480 KB Output is correct
40 Correct 21 ms 64344 KB Output is correct
41 Correct 17 ms 64344 KB Output is correct
42 Correct 17 ms 64348 KB Output is correct
43 Correct 21 ms 64860 KB Output is correct
44 Correct 16 ms 64088 KB Output is correct
45 Correct 22 ms 64600 KB Output is correct
46 Correct 24 ms 64432 KB Output is correct
47 Correct 19 ms 64092 KB Output is correct
48 Correct 17 ms 64348 KB Output is correct
49 Correct 21 ms 64604 KB Output is correct
50 Correct 21 ms 64756 KB Output is correct
51 Correct 20 ms 64344 KB Output is correct
52 Correct 20 ms 64344 KB Output is correct
53 Correct 20 ms 64348 KB Output is correct
54 Correct 20 ms 64184 KB Output is correct
55 Correct 19 ms 64092 KB Output is correct
56 Correct 68 ms 68556 KB Output is correct
57 Correct 83 ms 68000 KB Output is correct
58 Correct 20 ms 64344 KB Output is correct
59 Correct 16 ms 64088 KB Output is correct
60 Correct 19 ms 64092 KB Output is correct
61 Correct 100 ms 68044 KB Output is correct
62 Correct 20 ms 64348 KB Output is correct
63 Correct 129 ms 88012 KB Output is correct
64 Correct 77 ms 67876 KB Output is correct
65 Correct 20 ms 64376 KB Output is correct
66 Correct 75 ms 70308 KB Output is correct
67 Correct 17 ms 64344 KB Output is correct
68 Correct 17 ms 64344 KB Output is correct
69 Correct 62 ms 68168 KB Output is correct
70 Correct 20 ms 64856 KB Output is correct
71 Correct 67 ms 67924 KB Output is correct
72 Correct 97 ms 74576 KB Output is correct
73 Correct 19 ms 64088 KB Output is correct
74 Correct 117 ms 88720 KB Output is correct
75 Correct 95 ms 79440 KB Output is correct
76 Correct 17 ms 64344 KB Output is correct
77 Correct 21 ms 64344 KB Output is correct
78 Correct 63 ms 68240 KB Output is correct
79 Correct 133 ms 79048 KB Output is correct
80 Correct 18 ms 64348 KB Output is correct
81 Correct 16 ms 64348 KB Output is correct
82 Correct 15 ms 64088 KB Output is correct
83 Correct 16 ms 64168 KB Output is correct
84 Correct 18 ms 64184 KB Output is correct
85 Correct 124 ms 71748 KB Output is correct
86 Correct 21 ms 64344 KB Output is correct
87 Correct 71 ms 67924 KB Output is correct
88 Correct 15 ms 64092 KB Output is correct
89 Correct 64 ms 68048 KB Output is correct
90 Correct 137 ms 71764 KB Output is correct
91 Correct 191 ms 78416 KB Output is correct
92 Correct 78 ms 68172 KB Output is correct
93 Correct 21 ms 64348 KB Output is correct
94 Correct 19 ms 64092 KB Output is correct
95 Correct 141 ms 77600 KB Output is correct
96 Correct 192 ms 87120 KB Output is correct
97 Correct 21 ms 64344 KB Output is correct
98 Correct 18 ms 64252 KB Output is correct
99 Correct 107 ms 72316 KB Output is correct
100 Correct 18 ms 64348 KB Output is correct
101 Correct 80 ms 68944 KB Output is correct
102 Correct 203 ms 84240 KB Output is correct
103 Correct 17 ms 64604 KB Output is correct
104 Correct 22 ms 64856 KB Output is correct
105 Correct 17 ms 64348 KB Output is correct
106 Correct 16 ms 64204 KB Output is correct
107 Correct 21 ms 64344 KB Output is correct
108 Correct 695 ms 100740 KB Output is correct
109 Correct 928 ms 135404 KB Output is correct
110 Correct 942 ms 168116 KB Output is correct
111 Correct 1668 ms 188176 KB Output is correct
112 Correct 1229 ms 161624 KB Output is correct
113 Correct 1040 ms 133896 KB Output is correct
114 Correct 614 ms 85296 KB Output is correct
115 Correct 648 ms 84232 KB Output is correct
116 Correct 856 ms 92980 KB Output is correct
117 Correct 1526 ms 148348 KB Output is correct
118 Correct 590 ms 93008 KB Output is correct
119 Correct 1369 ms 148380 KB Output is correct
120 Correct 1540 ms 179228 KB Output is correct
121 Correct 1597 ms 148432 KB Output is correct
122 Correct 1274 ms 118640 KB Output is correct
123 Correct 1126 ms 151524 KB Output is correct
124 Correct 1461 ms 141252 KB Output is correct