답안 #376852

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
376852 2021-03-12T05:36:27 Z Kevin_Zhang_TW Mergers (JOI19_mergers) C++17
100 / 100
2522 ms 138528 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];
	}
}


vector<int> vir;
void dfs3(int x, int lst = -1) {

	int son = 0;
	for (int u : edge[x]) if (u != lst)
		if (sum[u] == 0) ++son;

	if (sum[x] == 0 && lst != -1) ++son;

	if (son) vir.pb(x);

	for (int u : edge[x]) if (u != lst)
		dfs3(u, x);
}
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);
	}

	if (k == 1) return cout << 0 << '\n', 0;

	int rt = 1;

	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);

	sort(AI(vir), [&](int a, int b) {
			return in[a] < in[b]; });

	vector<int> all;

	for (int i = 0;i < vir.size();++i) {
		all.pb(vir[i]);
		if (i+1 < vir.size())
			all.pb(getlca(vir[i], vir[i+1]));
   	}

	sort(AI(all), [&](int a, int b) {
			return in[a] < in[b]; });

	all.erase(unique(AI(all)), end(all));

	debug(AI(all));



	vector<int> stk;

	vector<int> cnt(n + 10);

	for (int u : all) {
		while (stk.size()) {
			int x = stk.back();
			DE(x, u);
			if (isanc(x, u)) break;
			stk.pop_back();
		}
		if (stk.size()) {
			int x = stk.back();
			++cnt[x], ++cnt[u];
		}
		stk.pb(u);
	}

	assert(all.size() != 1);

	int res = 0;

	for (int u : all)
		res += cnt[u] == 1;

	res = (res+1) / 2;

	cout << res << '\n';
}

Compilation message

mergers.cpp: In function 'int32_t main()':
mergers.cpp:105:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  105 |  for (int i = 0;i < vir.size();++i) {
      |                 ~~^~~~~~~~~~~~
mergers.cpp:107:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  107 |   if (i+1 < vir.size())
      |       ~~~~^~~~~~~~~~~~
mergers.cpp:15:20: warning: statement has no effect [-Wunused-value]
   15 | #define debug(...) 0
      |                    ^
mergers.cpp:116:2: note: in expansion of macro 'debug'
  116 |  debug(AI(all));
      |  ^~~~~
mergers.cpp:14:17: warning: statement has no effect [-Wunused-value]
   14 | #define DE(...) 0
      |                 ^
mergers.cpp:127:4: note: in expansion of macro 'DE'
  127 |    DE(x, u);
      |    ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 24044 KB Output is correct
2 Correct 17 ms 24044 KB Output is correct
3 Correct 17 ms 24044 KB Output is correct
4 Correct 17 ms 24044 KB Output is correct
5 Correct 18 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 24000 KB Output is correct
10 Correct 18 ms 24044 KB Output is correct
11 Correct 17 ms 24044 KB Output is correct
12 Correct 19 ms 24044 KB Output is correct
13 Correct 17 ms 24044 KB Output is correct
14 Correct 16 ms 24044 KB Output is correct
15 Correct 20 ms 24044 KB Output is correct
16 Correct 20 ms 24044 KB Output is correct
17 Correct 14 ms 24044 KB Output is correct
18 Correct 19 ms 24044 KB Output is correct
19 Correct 14 ms 24044 KB Output is correct
20 Correct 15 ms 24044 KB Output is correct
21 Correct 14 ms 24044 KB Output is correct
22 Correct 15 ms 24044 KB Output is correct
23 Correct 17 ms 24064 KB Output is correct
24 Correct 17 ms 24044 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 24044 KB Output is correct
2 Correct 17 ms 24044 KB Output is correct
3 Correct 17 ms 24044 KB Output is correct
4 Correct 17 ms 24044 KB Output is correct
5 Correct 18 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 24000 KB Output is correct
10 Correct 18 ms 24044 KB Output is correct
11 Correct 17 ms 24044 KB Output is correct
12 Correct 19 ms 24044 KB Output is correct
13 Correct 17 ms 24044 KB Output is correct
14 Correct 16 ms 24044 KB Output is correct
15 Correct 20 ms 24044 KB Output is correct
16 Correct 20 ms 24044 KB Output is correct
17 Correct 14 ms 24044 KB Output is correct
18 Correct 19 ms 24044 KB Output is correct
19 Correct 14 ms 24044 KB Output is correct
20 Correct 15 ms 24044 KB Output is correct
21 Correct 14 ms 24044 KB Output is correct
22 Correct 15 ms 24044 KB Output is correct
23 Correct 17 ms 24064 KB Output is correct
24 Correct 17 ms 24044 KB Output is correct
25 Correct 16 ms 24044 KB Output is correct
26 Correct 23 ms 24556 KB Output is correct
27 Correct 21 ms 24428 KB Output is correct
28 Correct 20 ms 24684 KB Output is correct
29 Correct 20 ms 24556 KB Output is correct
30 Correct 19 ms 24428 KB Output is correct
31 Correct 17 ms 24044 KB Output is correct
32 Correct 19 ms 24684 KB Output is correct
33 Correct 19 ms 24044 KB Output is correct
34 Correct 18 ms 24428 KB Output is correct
35 Correct 20 ms 24556 KB Output is correct
36 Correct 20 ms 24428 KB Output is correct
37 Correct 20 ms 24460 KB Output is correct
38 Correct 17 ms 24044 KB Output is correct
39 Correct 20 ms 24428 KB Output is correct
40 Correct 18 ms 23916 KB Output is correct
41 Correct 20 ms 24428 KB Output is correct
42 Correct 20 ms 24428 KB Output is correct
43 Correct 18 ms 23916 KB Output is correct
44 Correct 17 ms 24044 KB Output is correct
45 Correct 20 ms 24508 KB Output is correct
46 Correct 21 ms 24556 KB Output is correct
47 Correct 19 ms 24044 KB Output is correct
48 Correct 21 ms 24428 KB Output is correct
49 Correct 19 ms 24556 KB Output is correct
50 Correct 21 ms 24676 KB Output is correct
51 Correct 19 ms 24428 KB Output is correct
52 Correct 18 ms 24428 KB Output is correct
53 Correct 19 ms 24428 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 17 ms 24044 KB Output is correct
3 Correct 17 ms 24044 KB Output is correct
4 Correct 17 ms 24044 KB Output is correct
5 Correct 18 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 24000 KB Output is correct
10 Correct 18 ms 24044 KB Output is correct
11 Correct 17 ms 24044 KB Output is correct
12 Correct 19 ms 24044 KB Output is correct
13 Correct 17 ms 24044 KB Output is correct
14 Correct 16 ms 24044 KB Output is correct
15 Correct 20 ms 24044 KB Output is correct
16 Correct 20 ms 24044 KB Output is correct
17 Correct 14 ms 24044 KB Output is correct
18 Correct 19 ms 24044 KB Output is correct
19 Correct 14 ms 24044 KB Output is correct
20 Correct 15 ms 24044 KB Output is correct
21 Correct 14 ms 24044 KB Output is correct
22 Correct 15 ms 24044 KB Output is correct
23 Correct 17 ms 24064 KB Output is correct
24 Correct 17 ms 24044 KB Output is correct
25 Correct 17 ms 24044 KB Output is correct
26 Correct 84 ms 38408 KB Output is correct
27 Correct 143 ms 37996 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 23984 KB Output is correct
31 Correct 142 ms 37952 KB Output is correct
32 Correct 18 ms 24428 KB Output is correct
33 Correct 170 ms 45292 KB Output is correct
34 Correct 131 ms 38028 KB Output is correct
35 Correct 19 ms 24428 KB Output is correct
36 Correct 159 ms 38636 KB Output is correct
37 Correct 21 ms 23916 KB Output is correct
38 Correct 18 ms 24428 KB Output is correct
39 Correct 103 ms 38244 KB Output is correct
40 Correct 18 ms 23916 KB Output is correct
41 Correct 76 ms 28008 KB Output is correct
42 Correct 176 ms 40324 KB Output is correct
43 Correct 17 ms 24044 KB Output is correct
44 Correct 93 ms 27880 KB Output is correct
45 Correct 139 ms 41708 KB Output is correct
46 Correct 17 ms 24428 KB Output is correct
47 Correct 18 ms 24428 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 95 ms 38244 KB Output is correct
2 Correct 276 ms 41572 KB Output is correct
3 Correct 21 ms 24556 KB Output is correct
4 Correct 21 ms 24428 KB Output is correct
5 Correct 17 ms 24072 KB Output is correct
6 Correct 18 ms 24044 KB Output is correct
7 Correct 19 ms 24428 KB Output is correct
8 Correct 280 ms 39576 KB Output is correct
9 Correct 19 ms 24428 KB Output is correct
10 Correct 165 ms 38196 KB Output is correct
11 Correct 18 ms 24044 KB Output is correct
12 Correct 187 ms 38040 KB Output is correct
13 Correct 324 ms 39568 KB Output is correct
14 Correct 347 ms 41832 KB Output is correct
15 Correct 109 ms 38372 KB Output is correct
16 Correct 19 ms 24556 KB Output is correct
17 Correct 17 ms 24044 KB Output is correct
18 Correct 260 ms 41672 KB Output is correct
19 Correct 357 ms 46176 KB Output is correct
20 Correct 23 ms 24428 KB Output is correct
21 Correct 19 ms 24044 KB Output is correct
22 Correct 195 ms 39780 KB Output is correct
23 Correct 19 ms 24460 KB Output is correct
24 Correct 216 ms 38632 KB Output is correct
25 Correct 345 ms 44916 KB Output is correct
26 Correct 20 ms 24556 KB Output is correct
27 Correct 20 ms 24684 KB Output is correct
28 Correct 19 ms 24428 KB Output is correct
29 Correct 19 ms 24428 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 17 ms 24044 KB Output is correct
3 Correct 17 ms 24044 KB Output is correct
4 Correct 17 ms 24044 KB Output is correct
5 Correct 18 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 24000 KB Output is correct
10 Correct 18 ms 24044 KB Output is correct
11 Correct 17 ms 24044 KB Output is correct
12 Correct 19 ms 24044 KB Output is correct
13 Correct 17 ms 24044 KB Output is correct
14 Correct 16 ms 24044 KB Output is correct
15 Correct 20 ms 24044 KB Output is correct
16 Correct 20 ms 24044 KB Output is correct
17 Correct 14 ms 24044 KB Output is correct
18 Correct 19 ms 24044 KB Output is correct
19 Correct 14 ms 24044 KB Output is correct
20 Correct 15 ms 24044 KB Output is correct
21 Correct 14 ms 24044 KB Output is correct
22 Correct 15 ms 24044 KB Output is correct
23 Correct 17 ms 24064 KB Output is correct
24 Correct 17 ms 24044 KB Output is correct
25 Correct 16 ms 24044 KB Output is correct
26 Correct 23 ms 24556 KB Output is correct
27 Correct 21 ms 24428 KB Output is correct
28 Correct 20 ms 24684 KB Output is correct
29 Correct 20 ms 24556 KB Output is correct
30 Correct 19 ms 24428 KB Output is correct
31 Correct 17 ms 24044 KB Output is correct
32 Correct 19 ms 24684 KB Output is correct
33 Correct 19 ms 24044 KB Output is correct
34 Correct 18 ms 24428 KB Output is correct
35 Correct 20 ms 24556 KB Output is correct
36 Correct 20 ms 24428 KB Output is correct
37 Correct 20 ms 24460 KB Output is correct
38 Correct 17 ms 24044 KB Output is correct
39 Correct 20 ms 24428 KB Output is correct
40 Correct 18 ms 23916 KB Output is correct
41 Correct 20 ms 24428 KB Output is correct
42 Correct 20 ms 24428 KB Output is correct
43 Correct 18 ms 23916 KB Output is correct
44 Correct 17 ms 24044 KB Output is correct
45 Correct 20 ms 24508 KB Output is correct
46 Correct 21 ms 24556 KB Output is correct
47 Correct 19 ms 24044 KB Output is correct
48 Correct 21 ms 24428 KB Output is correct
49 Correct 19 ms 24556 KB Output is correct
50 Correct 21 ms 24676 KB Output is correct
51 Correct 19 ms 24428 KB Output is correct
52 Correct 18 ms 24428 KB Output is correct
53 Correct 19 ms 24428 KB Output is correct
54 Correct 19 ms 24428 KB Output is correct
55 Correct 17 ms 24044 KB Output is correct
56 Correct 84 ms 38408 KB Output is correct
57 Correct 143 ms 37996 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 23984 KB Output is correct
61 Correct 142 ms 37952 KB Output is correct
62 Correct 18 ms 24428 KB Output is correct
63 Correct 170 ms 45292 KB Output is correct
64 Correct 131 ms 38028 KB Output is correct
65 Correct 19 ms 24428 KB Output is correct
66 Correct 159 ms 38636 KB Output is correct
67 Correct 21 ms 23916 KB Output is correct
68 Correct 18 ms 24428 KB Output is correct
69 Correct 103 ms 38244 KB Output is correct
70 Correct 18 ms 23916 KB Output is correct
71 Correct 76 ms 28008 KB Output is correct
72 Correct 176 ms 40324 KB Output is correct
73 Correct 17 ms 24044 KB Output is correct
74 Correct 93 ms 27880 KB Output is correct
75 Correct 139 ms 41708 KB Output is correct
76 Correct 17 ms 24428 KB Output is correct
77 Correct 18 ms 24428 KB Output is correct
78 Correct 95 ms 38244 KB Output is correct
79 Correct 276 ms 41572 KB Output is correct
80 Correct 21 ms 24556 KB Output is correct
81 Correct 21 ms 24428 KB Output is correct
82 Correct 17 ms 24072 KB Output is correct
83 Correct 18 ms 24044 KB Output is correct
84 Correct 19 ms 24428 KB Output is correct
85 Correct 280 ms 39576 KB Output is correct
86 Correct 19 ms 24428 KB Output is correct
87 Correct 165 ms 38196 KB Output is correct
88 Correct 18 ms 24044 KB Output is correct
89 Correct 187 ms 38040 KB Output is correct
90 Correct 324 ms 39568 KB Output is correct
91 Correct 347 ms 41832 KB Output is correct
92 Correct 109 ms 38372 KB Output is correct
93 Correct 19 ms 24556 KB Output is correct
94 Correct 17 ms 24044 KB Output is correct
95 Correct 260 ms 41672 KB Output is correct
96 Correct 357 ms 46176 KB Output is correct
97 Correct 23 ms 24428 KB Output is correct
98 Correct 19 ms 24044 KB Output is correct
99 Correct 195 ms 39780 KB Output is correct
100 Correct 19 ms 24460 KB Output is correct
101 Correct 216 ms 38632 KB Output is correct
102 Correct 345 ms 44916 KB Output is correct
103 Correct 20 ms 24556 KB Output is correct
104 Correct 20 ms 24684 KB Output is correct
105 Correct 19 ms 24428 KB Output is correct
106 Correct 19 ms 24428 KB Output is correct
107 Correct 19 ms 24556 KB Output is correct
108 Correct 1101 ms 100140 KB Output is correct
109 Correct 968 ms 111400 KB Output is correct
110 Correct 415 ms 43868 KB Output is correct
111 Correct 2522 ms 138528 KB Output is correct
112 Correct 2432 ms 127892 KB Output is correct
113 Correct 1718 ms 111784 KB Output is correct
114 Correct 804 ms 93296 KB Output is correct
115 Correct 424 ms 44380 KB Output is correct
116 Correct 1117 ms 96492 KB Output is correct
117 Correct 2106 ms 118140 KB Output is correct
118 Correct 765 ms 98412 KB Output is correct
119 Correct 2087 ms 118008 KB Output is correct
120 Correct 2443 ms 133852 KB Output is correct
121 Correct 2071 ms 118268 KB Output is correct
122 Correct 1611 ms 107868 KB Output is correct
123 Correct 1796 ms 115588 KB Output is correct
124 Correct 1884 ms 114164 KB Output is correct