답안 #987111

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
987111 2024-05-21T22:27:41 Z OAleksa Mergers (JOI19_mergers) C++14
70 / 100
1593 ms 262144 KB
#include <bits/stdc++.h>
#define f first
#define s second
#define int long long
using namespace std;
const int N = 5e5 + 69;
const int K = 20;
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());
		}
		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(long long int, long long int)':
mergers.cpp:33:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::map<long long int, long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   33 |  assert(have[v] <= cnt[v].size());
      |         ~~~~~~~~^~~~~~~~~~~~~~~~
mergers.cpp:34:14: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::map<long long int, long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   34 |  if (have[v] == cnt[v].size())
      |      ~~~~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 36 ms 64088 KB Output is correct
2 Correct 20 ms 64088 KB Output is correct
3 Correct 18 ms 64092 KB Output is correct
4 Correct 19 ms 64088 KB Output is correct
5 Correct 19 ms 64212 KB Output is correct
6 Correct 23 ms 64092 KB Output is correct
7 Correct 19 ms 64092 KB Output is correct
8 Correct 18 ms 64272 KB Output is correct
9 Correct 21 ms 64092 KB Output is correct
10 Correct 20 ms 64092 KB Output is correct
11 Correct 21 ms 64088 KB Output is correct
12 Correct 21 ms 64228 KB Output is correct
13 Correct 19 ms 64092 KB Output is correct
14 Correct 19 ms 64088 KB Output is correct
15 Correct 21 ms 64088 KB Output is correct
16 Correct 15 ms 62044 KB Output is correct
17 Correct 19 ms 64088 KB Output is correct
18 Correct 16 ms 64092 KB Output is correct
19 Correct 18 ms 64092 KB Output is correct
20 Correct 18 ms 64092 KB Output is correct
21 Correct 20 ms 64092 KB Output is correct
22 Correct 18 ms 64088 KB Output is correct
23 Correct 16 ms 64092 KB Output is correct
24 Correct 19 ms 64344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 36 ms 64088 KB Output is correct
2 Correct 20 ms 64088 KB Output is correct
3 Correct 18 ms 64092 KB Output is correct
4 Correct 19 ms 64088 KB Output is correct
5 Correct 19 ms 64212 KB Output is correct
6 Correct 23 ms 64092 KB Output is correct
7 Correct 19 ms 64092 KB Output is correct
8 Correct 18 ms 64272 KB Output is correct
9 Correct 21 ms 64092 KB Output is correct
10 Correct 20 ms 64092 KB Output is correct
11 Correct 21 ms 64088 KB Output is correct
12 Correct 21 ms 64228 KB Output is correct
13 Correct 19 ms 64092 KB Output is correct
14 Correct 19 ms 64088 KB Output is correct
15 Correct 21 ms 64088 KB Output is correct
16 Correct 15 ms 62044 KB Output is correct
17 Correct 19 ms 64088 KB Output is correct
18 Correct 16 ms 64092 KB Output is correct
19 Correct 18 ms 64092 KB Output is correct
20 Correct 18 ms 64092 KB Output is correct
21 Correct 20 ms 64092 KB Output is correct
22 Correct 18 ms 64088 KB Output is correct
23 Correct 16 ms 64092 KB Output is correct
24 Correct 19 ms 64344 KB Output is correct
25 Correct 19 ms 64092 KB Output is correct
26 Correct 18 ms 64988 KB Output is correct
27 Correct 17 ms 64604 KB Output is correct
28 Correct 23 ms 65112 KB Output is correct
29 Correct 24 ms 65368 KB Output is correct
30 Correct 18 ms 64900 KB Output is correct
31 Correct 16 ms 64092 KB Output is correct
32 Correct 22 ms 65116 KB Output is correct
33 Correct 19 ms 64088 KB Output is correct
34 Correct 20 ms 64604 KB Output is correct
35 Correct 22 ms 65468 KB Output is correct
36 Correct 20 ms 64604 KB Output is correct
37 Correct 19 ms 64856 KB Output is correct
38 Correct 15 ms 64088 KB Output is correct
39 Correct 23 ms 65140 KB Output is correct
40 Correct 20 ms 64604 KB Output is correct
41 Correct 18 ms 64604 KB Output is correct
42 Correct 25 ms 64752 KB Output is correct
43 Correct 22 ms 65016 KB Output is correct
44 Correct 19 ms 64092 KB Output is correct
45 Correct 19 ms 65116 KB Output is correct
46 Correct 22 ms 64860 KB Output is correct
47 Correct 18 ms 64244 KB Output is correct
48 Correct 21 ms 64604 KB Output is correct
49 Correct 22 ms 64860 KB Output is correct
50 Correct 19 ms 65116 KB Output is correct
51 Correct 21 ms 64604 KB Output is correct
52 Correct 20 ms 64604 KB Output is correct
53 Correct 18 ms 64604 KB Output is correct
54 Correct 22 ms 65112 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 36 ms 64088 KB Output is correct
2 Correct 20 ms 64088 KB Output is correct
3 Correct 18 ms 64092 KB Output is correct
4 Correct 19 ms 64088 KB Output is correct
5 Correct 19 ms 64212 KB Output is correct
6 Correct 23 ms 64092 KB Output is correct
7 Correct 19 ms 64092 KB Output is correct
8 Correct 18 ms 64272 KB Output is correct
9 Correct 21 ms 64092 KB Output is correct
10 Correct 20 ms 64092 KB Output is correct
11 Correct 21 ms 64088 KB Output is correct
12 Correct 21 ms 64228 KB Output is correct
13 Correct 19 ms 64092 KB Output is correct
14 Correct 19 ms 64088 KB Output is correct
15 Correct 21 ms 64088 KB Output is correct
16 Correct 15 ms 62044 KB Output is correct
17 Correct 19 ms 64088 KB Output is correct
18 Correct 16 ms 64092 KB Output is correct
19 Correct 18 ms 64092 KB Output is correct
20 Correct 18 ms 64092 KB Output is correct
21 Correct 20 ms 64092 KB Output is correct
22 Correct 18 ms 64088 KB Output is correct
23 Correct 16 ms 64092 KB Output is correct
24 Correct 19 ms 64344 KB Output is correct
25 Correct 19 ms 64092 KB Output is correct
26 Correct 115 ms 75144 KB Output is correct
27 Correct 120 ms 83908 KB Output is correct
28 Correct 20 ms 64604 KB Output is correct
29 Correct 15 ms 64092 KB Output is correct
30 Correct 18 ms 64092 KB Output is correct
31 Correct 105 ms 74952 KB Output is correct
32 Correct 21 ms 64608 KB Output is correct
33 Correct 138 ms 90484 KB Output is correct
34 Correct 178 ms 82436 KB Output is correct
35 Correct 21 ms 64348 KB Output is correct
36 Correct 100 ms 76260 KB Output is correct
37 Correct 21 ms 64348 KB Output is correct
38 Correct 16 ms 64604 KB Output is correct
39 Correct 100 ms 75152 KB Output is correct
40 Correct 17 ms 64856 KB Output is correct
41 Correct 79 ms 74984 KB Output is correct
42 Correct 100 ms 79696 KB Output is correct
43 Correct 21 ms 64092 KB Output is correct
44 Correct 116 ms 90836 KB Output is correct
45 Correct 134 ms 85604 KB Output is correct
46 Correct 21 ms 64604 KB Output is correct
47 Correct 21 ms 64856 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 95 ms 75208 KB Output is correct
2 Correct 196 ms 87936 KB Output is correct
3 Correct 22 ms 64856 KB Output is correct
4 Correct 21 ms 64604 KB Output is correct
5 Correct 19 ms 64264 KB Output is correct
6 Correct 18 ms 64092 KB Output is correct
7 Correct 20 ms 64644 KB Output is correct
8 Correct 154 ms 95060 KB Output is correct
9 Correct 21 ms 64496 KB Output is correct
10 Correct 84 ms 77052 KB Output is correct
11 Correct 19 ms 64088 KB Output is correct
12 Correct 103 ms 76372 KB Output is correct
13 Correct 199 ms 94480 KB Output is correct
14 Correct 223 ms 115284 KB Output is correct
15 Correct 86 ms 76608 KB Output is correct
16 Correct 21 ms 64700 KB Output is correct
17 Correct 19 ms 64084 KB Output is correct
18 Correct 191 ms 89028 KB Output is correct
19 Correct 186 ms 99664 KB Output is correct
20 Correct 22 ms 64860 KB Output is correct
21 Correct 18 ms 64092 KB Output is correct
22 Correct 139 ms 81552 KB Output is correct
23 Correct 18 ms 64604 KB Output is correct
24 Correct 127 ms 80020 KB Output is correct
25 Correct 185 ms 98900 KB Output is correct
26 Correct 22 ms 64860 KB Output is correct
27 Correct 22 ms 65104 KB Output is correct
28 Correct 22 ms 64600 KB Output is correct
29 Correct 21 ms 64600 KB Output is correct
30 Correct 22 ms 64604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 36 ms 64088 KB Output is correct
2 Correct 20 ms 64088 KB Output is correct
3 Correct 18 ms 64092 KB Output is correct
4 Correct 19 ms 64088 KB Output is correct
5 Correct 19 ms 64212 KB Output is correct
6 Correct 23 ms 64092 KB Output is correct
7 Correct 19 ms 64092 KB Output is correct
8 Correct 18 ms 64272 KB Output is correct
9 Correct 21 ms 64092 KB Output is correct
10 Correct 20 ms 64092 KB Output is correct
11 Correct 21 ms 64088 KB Output is correct
12 Correct 21 ms 64228 KB Output is correct
13 Correct 19 ms 64092 KB Output is correct
14 Correct 19 ms 64088 KB Output is correct
15 Correct 21 ms 64088 KB Output is correct
16 Correct 15 ms 62044 KB Output is correct
17 Correct 19 ms 64088 KB Output is correct
18 Correct 16 ms 64092 KB Output is correct
19 Correct 18 ms 64092 KB Output is correct
20 Correct 18 ms 64092 KB Output is correct
21 Correct 20 ms 64092 KB Output is correct
22 Correct 18 ms 64088 KB Output is correct
23 Correct 16 ms 64092 KB Output is correct
24 Correct 19 ms 64344 KB Output is correct
25 Correct 19 ms 64092 KB Output is correct
26 Correct 18 ms 64988 KB Output is correct
27 Correct 17 ms 64604 KB Output is correct
28 Correct 23 ms 65112 KB Output is correct
29 Correct 24 ms 65368 KB Output is correct
30 Correct 18 ms 64900 KB Output is correct
31 Correct 16 ms 64092 KB Output is correct
32 Correct 22 ms 65116 KB Output is correct
33 Correct 19 ms 64088 KB Output is correct
34 Correct 20 ms 64604 KB Output is correct
35 Correct 22 ms 65468 KB Output is correct
36 Correct 20 ms 64604 KB Output is correct
37 Correct 19 ms 64856 KB Output is correct
38 Correct 15 ms 64088 KB Output is correct
39 Correct 23 ms 65140 KB Output is correct
40 Correct 20 ms 64604 KB Output is correct
41 Correct 18 ms 64604 KB Output is correct
42 Correct 25 ms 64752 KB Output is correct
43 Correct 22 ms 65016 KB Output is correct
44 Correct 19 ms 64092 KB Output is correct
45 Correct 19 ms 65116 KB Output is correct
46 Correct 22 ms 64860 KB Output is correct
47 Correct 18 ms 64244 KB Output is correct
48 Correct 21 ms 64604 KB Output is correct
49 Correct 22 ms 64860 KB Output is correct
50 Correct 19 ms 65116 KB Output is correct
51 Correct 21 ms 64604 KB Output is correct
52 Correct 20 ms 64604 KB Output is correct
53 Correct 18 ms 64604 KB Output is correct
54 Correct 22 ms 65112 KB Output is correct
55 Correct 19 ms 64092 KB Output is correct
56 Correct 115 ms 75144 KB Output is correct
57 Correct 120 ms 83908 KB Output is correct
58 Correct 20 ms 64604 KB Output is correct
59 Correct 15 ms 64092 KB Output is correct
60 Correct 18 ms 64092 KB Output is correct
61 Correct 105 ms 74952 KB Output is correct
62 Correct 21 ms 64608 KB Output is correct
63 Correct 138 ms 90484 KB Output is correct
64 Correct 178 ms 82436 KB Output is correct
65 Correct 21 ms 64348 KB Output is correct
66 Correct 100 ms 76260 KB Output is correct
67 Correct 21 ms 64348 KB Output is correct
68 Correct 16 ms 64604 KB Output is correct
69 Correct 100 ms 75152 KB Output is correct
70 Correct 17 ms 64856 KB Output is correct
71 Correct 79 ms 74984 KB Output is correct
72 Correct 100 ms 79696 KB Output is correct
73 Correct 21 ms 64092 KB Output is correct
74 Correct 116 ms 90836 KB Output is correct
75 Correct 134 ms 85604 KB Output is correct
76 Correct 21 ms 64604 KB Output is correct
77 Correct 21 ms 64856 KB Output is correct
78 Correct 95 ms 75208 KB Output is correct
79 Correct 196 ms 87936 KB Output is correct
80 Correct 22 ms 64856 KB Output is correct
81 Correct 21 ms 64604 KB Output is correct
82 Correct 19 ms 64264 KB Output is correct
83 Correct 18 ms 64092 KB Output is correct
84 Correct 20 ms 64644 KB Output is correct
85 Correct 154 ms 95060 KB Output is correct
86 Correct 21 ms 64496 KB Output is correct
87 Correct 84 ms 77052 KB Output is correct
88 Correct 19 ms 64088 KB Output is correct
89 Correct 103 ms 76372 KB Output is correct
90 Correct 199 ms 94480 KB Output is correct
91 Correct 223 ms 115284 KB Output is correct
92 Correct 86 ms 76608 KB Output is correct
93 Correct 21 ms 64700 KB Output is correct
94 Correct 19 ms 64084 KB Output is correct
95 Correct 191 ms 89028 KB Output is correct
96 Correct 186 ms 99664 KB Output is correct
97 Correct 22 ms 64860 KB Output is correct
98 Correct 18 ms 64092 KB Output is correct
99 Correct 139 ms 81552 KB Output is correct
100 Correct 18 ms 64604 KB Output is correct
101 Correct 127 ms 80020 KB Output is correct
102 Correct 185 ms 98900 KB Output is correct
103 Correct 22 ms 64860 KB Output is correct
104 Correct 22 ms 65104 KB Output is correct
105 Correct 22 ms 64600 KB Output is correct
106 Correct 21 ms 64600 KB Output is correct
107 Correct 22 ms 64604 KB Output is correct
108 Correct 1107 ms 154784 KB Output is correct
109 Correct 1045 ms 169056 KB Output is correct
110 Correct 1016 ms 193532 KB Output is correct
111 Correct 1380 ms 254688 KB Output is correct
112 Correct 1593 ms 238592 KB Output is correct
113 Correct 1486 ms 191752 KB Output is correct
114 Correct 749 ms 131164 KB Output is correct
115 Correct 737 ms 131000 KB Output is correct
116 Correct 1151 ms 233464 KB Output is correct
117 Runtime error 1020 ms 262144 KB Execution killed with signal 9
118 Halted 0 ms 0 KB -