답안 #376969

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
376969 2021-03-12T13:25:45 Z Kevin_Zhang_TW Mergers (JOI19_mergers) C++17
100 / 100
1237 ms 130820 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long ;
#define pb emplace_back
#define AI(i) begin(i), end(i)
template<class T> bool chmin(T &a, T b) { return b < a && (a = b, true); }
template<class T> bool chmax(T &a, T b) { return a < b && (a = b, true); }
#ifdef KEV
#define DE(args...) kout("[ " + string(#args) + " ] = ", args)
void kout() { cerr << endl; }
template<class T, class ...U> void kout(T a, U ...b) { cerr << a << ' ', kout(b...); }
template<class T> void debug(T l, T r) { while (l != r) cerr << *l << " \n"[next(l) == r], ++l; }
#else
#define DE(...) 0
#define debug(...) 0
#endif
const int MAX_N = 500010, MAX_K = 20;
 
int n, k, belong[MAX_N];
vector<int> edge[MAX_N];
vector<int> state[MAX_N];
ll sum[MAX_N];
 
int anc[MAX_K][MAX_N], in[MAX_N], out[MAX_N];
bool isanc(int a, int b) { return in[a] <= in[b] && out[a] >= out[b]; }
void dfs(int x, int lst = 1) {
	static int t;
	anc[0][x] = lst;
	in[x] = ++t;
	for (int u : edge[x]) if (u != lst)
		dfs(u, x);
	out[x] = t;
}
 
void dfs2(int x, int lst = -1) {
	for (int u : edge[x]) if (u != lst) {
		dfs2(u, x);
		sum[x] += sum[u];
	}
}
 
int dp[MAX_N], res;
 
int dfs3(int x, int lst = -1) {
	int son = 0;
	for (int u : edge[x]) if (u != lst)
		son += dfs3(u, x);
 
	if (son == 0 && sum[x] == 0 && lst != -1) {
		++son;
		DE(x);
	}
 
	chmax(res, son);
 
	if (lst != -1 && sum[x] == 0) {
		chmax(res, son + 1);
	}
 
	return dp[x] = son;
}
int getlca(int a, int b) {
	for (int d = MAX_K-1;d >= 0;--d)
		if (!isanc(anc[d][a], b))
			a = anc[d][a];
	return isanc(a, b) ? a : anc[0][a];
}
 
int32_t main() {
	ios_base::sync_with_stdio(0), cin.tie(0);
	cin >> n >> k;
	for (int a, b, i = 1;i < n;++i) {
		cin >> a >> b;
		edge[a].pb(b), edge[b].pb(a);
	}
	for (int i = 1;i <= n;++i) {
		cin >> belong[i];
		state[ belong[i] ].pb(i);
	}
	int rt = 1;
 
	if (k == 1) return cout << 0 << '\n', 0;
 
 
	dfs(rt);
	for (int d = 1;d < MAX_K;++d)
		for (int i = 1;i <= n;++i)
			anc[d][i] = anc[d-1][ anc[d-1][i] ];
 
	for (int i = 1;i <= k;++i) {
		if (state[i].size() > 1) {
			sort(AI(state[i]), [&](int a, int b) {
					return in[a] < in[b];
					});
			for (int x : state[i])
				++sum[x];
			sum[ getlca(state[i][0], state[i].back()) ] -= state[i].size();
		}
	}
 
	dfs2(rt);
 
	dfs3(rt);
	res = (res+1) / 2;
	cout << res << '\n';
}

Compilation message

mergers.cpp: In function 'int dfs3(int, int)':
mergers.cpp:14:17: warning: statement has no effect [-Wunused-value]
   14 | #define DE(...) 0
      |                 ^
mergers.cpp:51:3: note: in expansion of macro 'DE'
   51 |   DE(x);
      |   ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 24044 KB Output is correct
2 Correct 19 ms 24044 KB Output is correct
3 Correct 18 ms 24044 KB Output is correct
4 Correct 17 ms 24044 KB Output is correct
5 Correct 17 ms 24044 KB Output is correct
6 Correct 17 ms 23788 KB Output is correct
7 Correct 17 ms 23788 KB Output is correct
8 Correct 17 ms 24044 KB Output is correct
9 Correct 17 ms 24044 KB Output is correct
10 Correct 18 ms 24044 KB Output is correct
11 Correct 16 ms 24044 KB Output is correct
12 Correct 17 ms 24044 KB Output is correct
13 Correct 17 ms 24064 KB Output is correct
14 Correct 17 ms 24044 KB Output is correct
15 Correct 17 ms 24044 KB Output is correct
16 Correct 17 ms 24044 KB Output is correct
17 Correct 17 ms 24044 KB Output is correct
18 Correct 17 ms 24044 KB Output is correct
19 Correct 17 ms 24044 KB Output is correct
20 Correct 18 ms 24044 KB Output is correct
21 Correct 17 ms 24044 KB Output is correct
22 Correct 16 ms 24044 KB Output is correct
23 Correct 16 ms 24064 KB Output is correct
24 Correct 16 ms 24044 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 24044 KB Output is correct
2 Correct 19 ms 24044 KB Output is correct
3 Correct 18 ms 24044 KB Output is correct
4 Correct 17 ms 24044 KB Output is correct
5 Correct 17 ms 24044 KB Output is correct
6 Correct 17 ms 23788 KB Output is correct
7 Correct 17 ms 23788 KB Output is correct
8 Correct 17 ms 24044 KB Output is correct
9 Correct 17 ms 24044 KB Output is correct
10 Correct 18 ms 24044 KB Output is correct
11 Correct 16 ms 24044 KB Output is correct
12 Correct 17 ms 24044 KB Output is correct
13 Correct 17 ms 24064 KB Output is correct
14 Correct 17 ms 24044 KB Output is correct
15 Correct 17 ms 24044 KB Output is correct
16 Correct 17 ms 24044 KB Output is correct
17 Correct 17 ms 24044 KB Output is correct
18 Correct 17 ms 24044 KB Output is correct
19 Correct 17 ms 24044 KB Output is correct
20 Correct 18 ms 24044 KB Output is correct
21 Correct 17 ms 24044 KB Output is correct
22 Correct 16 ms 24044 KB Output is correct
23 Correct 16 ms 24064 KB Output is correct
24 Correct 16 ms 24044 KB Output is correct
25 Correct 16 ms 24044 KB Output is correct
26 Correct 20 ms 24556 KB Output is correct
27 Correct 19 ms 24428 KB Output is correct
28 Correct 21 ms 24684 KB Output is correct
29 Correct 23 ms 24556 KB Output is correct
30 Correct 20 ms 24428 KB Output is correct
31 Correct 18 ms 24044 KB Output is correct
32 Correct 19 ms 24684 KB Output is correct
33 Correct 17 ms 24044 KB Output is correct
34 Correct 18 ms 24428 KB Output is correct
35 Correct 19 ms 24556 KB Output is correct
36 Correct 18 ms 24428 KB Output is correct
37 Correct 19 ms 24556 KB Output is correct
38 Correct 17 ms 24044 KB Output is correct
39 Correct 19 ms 24556 KB Output is correct
40 Correct 18 ms 24044 KB Output is correct
41 Correct 19 ms 24556 KB Output is correct
42 Correct 19 ms 24556 KB Output is correct
43 Correct 19 ms 24044 KB Output is correct
44 Correct 17 ms 24044 KB Output is correct
45 Correct 19 ms 24556 KB Output is correct
46 Correct 20 ms 24556 KB Output is correct
47 Correct 20 ms 24044 KB Output is correct
48 Correct 19 ms 24428 KB Output is correct
49 Correct 19 ms 24556 KB Output is correct
50 Correct 19 ms 24556 KB Output is correct
51 Correct 19 ms 24428 KB Output is correct
52 Correct 18 ms 24556 KB Output is correct
53 Correct 19 ms 24556 KB Output is correct
54 Correct 19 ms 24428 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 24044 KB Output is correct
2 Correct 19 ms 24044 KB Output is correct
3 Correct 18 ms 24044 KB Output is correct
4 Correct 17 ms 24044 KB Output is correct
5 Correct 17 ms 24044 KB Output is correct
6 Correct 17 ms 23788 KB Output is correct
7 Correct 17 ms 23788 KB Output is correct
8 Correct 17 ms 24044 KB Output is correct
9 Correct 17 ms 24044 KB Output is correct
10 Correct 18 ms 24044 KB Output is correct
11 Correct 16 ms 24044 KB Output is correct
12 Correct 17 ms 24044 KB Output is correct
13 Correct 17 ms 24064 KB Output is correct
14 Correct 17 ms 24044 KB Output is correct
15 Correct 17 ms 24044 KB Output is correct
16 Correct 17 ms 24044 KB Output is correct
17 Correct 17 ms 24044 KB Output is correct
18 Correct 17 ms 24044 KB Output is correct
19 Correct 17 ms 24044 KB Output is correct
20 Correct 18 ms 24044 KB Output is correct
21 Correct 17 ms 24044 KB Output is correct
22 Correct 16 ms 24044 KB Output is correct
23 Correct 16 ms 24064 KB Output is correct
24 Correct 16 ms 24044 KB Output is correct
25 Correct 18 ms 24044 KB Output is correct
26 Correct 84 ms 39268 KB Output is correct
27 Correct 114 ms 39020 KB Output is correct
28 Correct 19 ms 24428 KB Output is correct
29 Correct 17 ms 24044 KB Output is correct
30 Correct 17 ms 24064 KB Output is correct
31 Correct 120 ms 39020 KB Output is correct
32 Correct 19 ms 24556 KB Output is correct
33 Correct 140 ms 46188 KB Output is correct
34 Correct 118 ms 39020 KB Output is correct
35 Correct 20 ms 24428 KB Output is correct
36 Correct 153 ms 39660 KB Output is correct
37 Correct 18 ms 24044 KB Output is correct
38 Correct 19 ms 24428 KB Output is correct
39 Correct 80 ms 39416 KB Output is correct
40 Correct 18 ms 24044 KB Output is correct
41 Correct 71 ms 29032 KB Output is correct
42 Correct 148 ms 41324 KB Output is correct
43 Correct 17 ms 24044 KB Output is correct
44 Correct 70 ms 28960 KB Output is correct
45 Correct 125 ms 42732 KB Output is correct
46 Correct 18 ms 24684 KB Output is correct
47 Correct 19 ms 24428 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 82 ms 39652 KB Output is correct
2 Correct 87 ms 41828 KB Output is correct
3 Correct 20 ms 24684 KB Output is correct
4 Correct 18 ms 24428 KB Output is correct
5 Correct 17 ms 24044 KB Output is correct
6 Correct 17 ms 24044 KB Output is correct
7 Correct 18 ms 24428 KB Output is correct
8 Correct 208 ms 40172 KB Output is correct
9 Correct 19 ms 24428 KB Output is correct
10 Correct 121 ms 39148 KB Output is correct
11 Correct 18 ms 24044 KB Output is correct
12 Correct 128 ms 39020 KB Output is correct
13 Correct 188 ms 39916 KB Output is correct
14 Correct 129 ms 41544 KB Output is correct
15 Correct 85 ms 39396 KB Output is correct
16 Correct 19 ms 24556 KB Output is correct
17 Correct 18 ms 24044 KB Output is correct
18 Correct 104 ms 41572 KB Output is correct
19 Correct 160 ms 45572 KB Output is correct
20 Correct 20 ms 24556 KB Output is correct
21 Correct 17 ms 24044 KB Output is correct
22 Correct 152 ms 40336 KB Output is correct
23 Correct 19 ms 24428 KB Output is correct
24 Correct 170 ms 39276 KB Output is correct
25 Correct 125 ms 44140 KB Output is correct
26 Correct 18 ms 24556 KB Output is correct
27 Correct 19 ms 24556 KB Output is correct
28 Correct 19 ms 24448 KB Output is correct
29 Correct 21 ms 24556 KB Output is correct
30 Correct 19 ms 24556 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 24044 KB Output is correct
2 Correct 19 ms 24044 KB Output is correct
3 Correct 18 ms 24044 KB Output is correct
4 Correct 17 ms 24044 KB Output is correct
5 Correct 17 ms 24044 KB Output is correct
6 Correct 17 ms 23788 KB Output is correct
7 Correct 17 ms 23788 KB Output is correct
8 Correct 17 ms 24044 KB Output is correct
9 Correct 17 ms 24044 KB Output is correct
10 Correct 18 ms 24044 KB Output is correct
11 Correct 16 ms 24044 KB Output is correct
12 Correct 17 ms 24044 KB Output is correct
13 Correct 17 ms 24064 KB Output is correct
14 Correct 17 ms 24044 KB Output is correct
15 Correct 17 ms 24044 KB Output is correct
16 Correct 17 ms 24044 KB Output is correct
17 Correct 17 ms 24044 KB Output is correct
18 Correct 17 ms 24044 KB Output is correct
19 Correct 17 ms 24044 KB Output is correct
20 Correct 18 ms 24044 KB Output is correct
21 Correct 17 ms 24044 KB Output is correct
22 Correct 16 ms 24044 KB Output is correct
23 Correct 16 ms 24064 KB Output is correct
24 Correct 16 ms 24044 KB Output is correct
25 Correct 16 ms 24044 KB Output is correct
26 Correct 20 ms 24556 KB Output is correct
27 Correct 19 ms 24428 KB Output is correct
28 Correct 21 ms 24684 KB Output is correct
29 Correct 23 ms 24556 KB Output is correct
30 Correct 20 ms 24428 KB Output is correct
31 Correct 18 ms 24044 KB Output is correct
32 Correct 19 ms 24684 KB Output is correct
33 Correct 17 ms 24044 KB Output is correct
34 Correct 18 ms 24428 KB Output is correct
35 Correct 19 ms 24556 KB Output is correct
36 Correct 18 ms 24428 KB Output is correct
37 Correct 19 ms 24556 KB Output is correct
38 Correct 17 ms 24044 KB Output is correct
39 Correct 19 ms 24556 KB Output is correct
40 Correct 18 ms 24044 KB Output is correct
41 Correct 19 ms 24556 KB Output is correct
42 Correct 19 ms 24556 KB Output is correct
43 Correct 19 ms 24044 KB Output is correct
44 Correct 17 ms 24044 KB Output is correct
45 Correct 19 ms 24556 KB Output is correct
46 Correct 20 ms 24556 KB Output is correct
47 Correct 20 ms 24044 KB Output is correct
48 Correct 19 ms 24428 KB Output is correct
49 Correct 19 ms 24556 KB Output is correct
50 Correct 19 ms 24556 KB Output is correct
51 Correct 19 ms 24428 KB Output is correct
52 Correct 18 ms 24556 KB Output is correct
53 Correct 19 ms 24556 KB Output is correct
54 Correct 19 ms 24428 KB Output is correct
55 Correct 18 ms 24044 KB Output is correct
56 Correct 84 ms 39268 KB Output is correct
57 Correct 114 ms 39020 KB Output is correct
58 Correct 19 ms 24428 KB Output is correct
59 Correct 17 ms 24044 KB Output is correct
60 Correct 17 ms 24064 KB Output is correct
61 Correct 120 ms 39020 KB Output is correct
62 Correct 19 ms 24556 KB Output is correct
63 Correct 140 ms 46188 KB Output is correct
64 Correct 118 ms 39020 KB Output is correct
65 Correct 20 ms 24428 KB Output is correct
66 Correct 153 ms 39660 KB Output is correct
67 Correct 18 ms 24044 KB Output is correct
68 Correct 19 ms 24428 KB Output is correct
69 Correct 80 ms 39416 KB Output is correct
70 Correct 18 ms 24044 KB Output is correct
71 Correct 71 ms 29032 KB Output is correct
72 Correct 148 ms 41324 KB Output is correct
73 Correct 17 ms 24044 KB Output is correct
74 Correct 70 ms 28960 KB Output is correct
75 Correct 125 ms 42732 KB Output is correct
76 Correct 18 ms 24684 KB Output is correct
77 Correct 19 ms 24428 KB Output is correct
78 Correct 82 ms 39652 KB Output is correct
79 Correct 87 ms 41828 KB Output is correct
80 Correct 20 ms 24684 KB Output is correct
81 Correct 18 ms 24428 KB Output is correct
82 Correct 17 ms 24044 KB Output is correct
83 Correct 17 ms 24044 KB Output is correct
84 Correct 18 ms 24428 KB Output is correct
85 Correct 208 ms 40172 KB Output is correct
86 Correct 19 ms 24428 KB Output is correct
87 Correct 121 ms 39148 KB Output is correct
88 Correct 18 ms 24044 KB Output is correct
89 Correct 128 ms 39020 KB Output is correct
90 Correct 188 ms 39916 KB Output is correct
91 Correct 129 ms 41544 KB Output is correct
92 Correct 85 ms 39396 KB Output is correct
93 Correct 19 ms 24556 KB Output is correct
94 Correct 18 ms 24044 KB Output is correct
95 Correct 104 ms 41572 KB Output is correct
96 Correct 160 ms 45572 KB Output is correct
97 Correct 20 ms 24556 KB Output is correct
98 Correct 17 ms 24044 KB Output is correct
99 Correct 152 ms 40336 KB Output is correct
100 Correct 19 ms 24428 KB Output is correct
101 Correct 170 ms 39276 KB Output is correct
102 Correct 125 ms 44140 KB Output is correct
103 Correct 18 ms 24556 KB Output is correct
104 Correct 19 ms 24556 KB Output is correct
105 Correct 19 ms 24448 KB Output is correct
106 Correct 21 ms 24556 KB Output is correct
107 Correct 19 ms 24556 KB Output is correct
108 Correct 956 ms 99768 KB Output is correct
109 Correct 939 ms 112128 KB Output is correct
110 Correct 404 ms 44764 KB Output is correct
111 Correct 1014 ms 130820 KB Output is correct
112 Correct 906 ms 120928 KB Output is correct
113 Correct 686 ms 107348 KB Output is correct
114 Correct 787 ms 94216 KB Output is correct
115 Correct 417 ms 45312 KB Output is correct
116 Correct 1101 ms 96836 KB Output is correct
117 Correct 869 ms 107884 KB Output is correct
118 Correct 755 ms 94316 KB Output is correct
119 Correct 880 ms 108140 KB Output is correct
120 Correct 929 ms 121836 KB Output is correct
121 Correct 846 ms 107884 KB Output is correct
122 Correct 1237 ms 101740 KB Output is correct
123 Correct 565 ms 105436 KB Output is correct
124 Correct 985 ms 106604 KB Output is correct