답안 #402992

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
402992 2021-05-12T16:03:37 Z ritul_kr_singh Mergers (JOI19_mergers) C++17
100 / 100
1330 ms 181876 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define sp << ' ' <<
#define nl << '\n'

const int MAXN = 5e5;
array<int, 20> p[MAXN];
vector<int> g[MAXN];
int t0[MAXN], d[MAXN], pre[MAXN], dfsTimer = 0;

void dfs0(int u, int par, int dep){
	t0[u] = dfsTimer++;
	p[u][0] = par, d[u] = dep;
	for(int i=0; i<19; ++i) p[u][i+1] = p[p[u][i]][i];

	for(int v : g[u]) if(v != par) dfs0(v, u, dep + 1);
}

int lca(int u, int v){
	if(d[u] < d[v]) swap(u, v);
	for(int i=0; i<20; ++i) if((d[u]-d[v]) & (1<<i)) u = p[u][i];
	if(u == v) return u;
	for(int i=19; i>=0; --i) if(p[u][i] != p[v][i]) u = p[u][i], v = p[v][i];
	return p[u][0];
}

vector<int> e(MAXN, -1);
int find(int u){
	return e[u] < 0 ? u : e[u] = find(e[u]);
}
void unite(int u, int v){
	u = find(u), v = find(v);
	if(u==v) return;
	if(e[u] > e[v]) swap(u, v);
	e[u] += e[v], e[v] = u;
}

void dfs1(int u){
	for(int v : g[u]){
		if(v == p[u][0]) continue;
		dfs1(v);
		pre[u] += pre[v];
		if(pre[v]) unite(u, v);
	}
}

signed main(){
	cin.tie(0)->sync_with_stdio(0);
	int n, k, u, v; cin >> n >> k;
	for(int i=1; i<n; ++i){
		cin >> u >> v; --u, --v;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	vector<int> c[k];
	for(u=0; u<n; ++u) cin >> v, c[v-1].push_back(u);

	dfs0(0, 0, 0);

	for(auto &i : c){
		sort(i.begin(), i.end(), [&](int x, int y){
			return t0[x] < t0[y];
		});
		i.push_back(i.front());
		for(int j=0; j+1<(int)i.size(); ++j){
			++pre[i[j]];
			++pre[i[j+1]];
			pre[lca(i[j], i[j+1])] -= 2;
		}
	}

	dfs1(0);

	vector<int> h(n);
	for(int u=0; u<n; ++u)
		for(int v : g[u])
			if(find(u) != find(v)) ++h[find(u)], ++h[find(v)];

	int ans = 0;
	for(int u=0; u<n; ++u){
		ans += find(u) == u && h[find(u)] == 2;
	}

	cout << (ans + 1) / 2;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 15996 KB Output is correct
2 Correct 11 ms 15988 KB Output is correct
3 Correct 9 ms 15948 KB Output is correct
4 Correct 9 ms 15948 KB Output is correct
5 Correct 9 ms 16000 KB Output is correct
6 Correct 9 ms 15948 KB Output is correct
7 Correct 9 ms 15916 KB Output is correct
8 Correct 9 ms 15948 KB Output is correct
9 Correct 9 ms 15956 KB Output is correct
10 Correct 9 ms 15948 KB Output is correct
11 Correct 9 ms 15948 KB Output is correct
12 Correct 9 ms 15948 KB Output is correct
13 Correct 9 ms 15948 KB Output is correct
14 Correct 11 ms 15948 KB Output is correct
15 Correct 11 ms 15948 KB Output is correct
16 Correct 9 ms 16004 KB Output is correct
17 Correct 9 ms 16000 KB Output is correct
18 Correct 9 ms 15944 KB Output is correct
19 Correct 9 ms 15948 KB Output is correct
20 Correct 9 ms 15948 KB Output is correct
21 Correct 9 ms 15948 KB Output is correct
22 Correct 9 ms 16004 KB Output is correct
23 Correct 9 ms 16076 KB Output is correct
24 Correct 10 ms 15976 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 15996 KB Output is correct
2 Correct 11 ms 15988 KB Output is correct
3 Correct 9 ms 15948 KB Output is correct
4 Correct 9 ms 15948 KB Output is correct
5 Correct 9 ms 16000 KB Output is correct
6 Correct 9 ms 15948 KB Output is correct
7 Correct 9 ms 15916 KB Output is correct
8 Correct 9 ms 15948 KB Output is correct
9 Correct 9 ms 15956 KB Output is correct
10 Correct 9 ms 15948 KB Output is correct
11 Correct 9 ms 15948 KB Output is correct
12 Correct 9 ms 15948 KB Output is correct
13 Correct 9 ms 15948 KB Output is correct
14 Correct 11 ms 15948 KB Output is correct
15 Correct 11 ms 15948 KB Output is correct
16 Correct 9 ms 16004 KB Output is correct
17 Correct 9 ms 16000 KB Output is correct
18 Correct 9 ms 15944 KB Output is correct
19 Correct 9 ms 15948 KB Output is correct
20 Correct 9 ms 15948 KB Output is correct
21 Correct 9 ms 15948 KB Output is correct
22 Correct 9 ms 16004 KB Output is correct
23 Correct 9 ms 16076 KB Output is correct
24 Correct 10 ms 15976 KB Output is correct
25 Correct 9 ms 16000 KB Output is correct
26 Correct 11 ms 16780 KB Output is correct
27 Correct 11 ms 16716 KB Output is correct
28 Correct 11 ms 16892 KB Output is correct
29 Correct 12 ms 16776 KB Output is correct
30 Correct 13 ms 16892 KB Output is correct
31 Correct 9 ms 16024 KB Output is correct
32 Correct 12 ms 16984 KB Output is correct
33 Correct 9 ms 15960 KB Output is correct
34 Correct 12 ms 16768 KB Output is correct
35 Correct 12 ms 16844 KB Output is correct
36 Correct 12 ms 16740 KB Output is correct
37 Correct 13 ms 16844 KB Output is correct
38 Correct 9 ms 15996 KB Output is correct
39 Correct 12 ms 16716 KB Output is correct
40 Correct 12 ms 16640 KB Output is correct
41 Correct 11 ms 16716 KB Output is correct
42 Correct 12 ms 16832 KB Output is correct
43 Correct 11 ms 16844 KB Output is correct
44 Correct 9 ms 15944 KB Output is correct
45 Correct 12 ms 16912 KB Output is correct
46 Correct 12 ms 16716 KB Output is correct
47 Correct 10 ms 15916 KB Output is correct
48 Correct 12 ms 16776 KB Output is correct
49 Correct 11 ms 16780 KB Output is correct
50 Correct 12 ms 16844 KB Output is correct
51 Correct 11 ms 16776 KB Output is correct
52 Correct 11 ms 16780 KB Output is correct
53 Correct 11 ms 16784 KB Output is correct
54 Correct 11 ms 16716 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 15996 KB Output is correct
2 Correct 11 ms 15988 KB Output is correct
3 Correct 9 ms 15948 KB Output is correct
4 Correct 9 ms 15948 KB Output is correct
5 Correct 9 ms 16000 KB Output is correct
6 Correct 9 ms 15948 KB Output is correct
7 Correct 9 ms 15916 KB Output is correct
8 Correct 9 ms 15948 KB Output is correct
9 Correct 9 ms 15956 KB Output is correct
10 Correct 9 ms 15948 KB Output is correct
11 Correct 9 ms 15948 KB Output is correct
12 Correct 9 ms 15948 KB Output is correct
13 Correct 9 ms 15948 KB Output is correct
14 Correct 11 ms 15948 KB Output is correct
15 Correct 11 ms 15948 KB Output is correct
16 Correct 9 ms 16004 KB Output is correct
17 Correct 9 ms 16000 KB Output is correct
18 Correct 9 ms 15944 KB Output is correct
19 Correct 9 ms 15948 KB Output is correct
20 Correct 9 ms 15948 KB Output is correct
21 Correct 9 ms 15948 KB Output is correct
22 Correct 9 ms 16004 KB Output is correct
23 Correct 9 ms 16076 KB Output is correct
24 Correct 10 ms 15976 KB Output is correct
25 Correct 9 ms 15996 KB Output is correct
26 Correct 121 ms 41228 KB Output is correct
27 Correct 170 ms 41028 KB Output is correct
28 Correct 11 ms 16716 KB Output is correct
29 Correct 9 ms 15948 KB Output is correct
30 Correct 9 ms 15996 KB Output is correct
31 Correct 160 ms 40900 KB Output is correct
32 Correct 12 ms 16716 KB Output is correct
33 Correct 213 ms 46116 KB Output is correct
34 Correct 180 ms 40936 KB Output is correct
35 Correct 12 ms 16716 KB Output is correct
36 Correct 164 ms 40900 KB Output is correct
37 Correct 12 ms 16752 KB Output is correct
38 Correct 14 ms 16716 KB Output is correct
39 Correct 118 ms 41124 KB Output is correct
40 Correct 11 ms 16908 KB Output is correct
41 Correct 155 ms 40696 KB Output is correct
42 Correct 160 ms 42260 KB Output is correct
43 Correct 9 ms 15948 KB Output is correct
44 Correct 174 ms 46388 KB Output is correct
45 Correct 181 ms 44640 KB Output is correct
46 Correct 11 ms 16716 KB Output is correct
47 Correct 12 ms 16656 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 113 ms 41232 KB Output is correct
2 Correct 109 ms 45960 KB Output is correct
3 Correct 12 ms 16844 KB Output is correct
4 Correct 12 ms 16656 KB Output is correct
5 Correct 9 ms 15948 KB Output is correct
6 Correct 9 ms 15928 KB Output is correct
7 Correct 12 ms 16748 KB Output is correct
8 Correct 166 ms 43944 KB Output is correct
9 Correct 12 ms 16716 KB Output is correct
10 Correct 146 ms 41200 KB Output is correct
11 Correct 10 ms 15948 KB Output is correct
12 Correct 139 ms 40640 KB Output is correct
13 Correct 154 ms 43844 KB Output is correct
14 Correct 137 ms 45632 KB Output is correct
15 Correct 115 ms 41284 KB Output is correct
16 Correct 12 ms 16768 KB Output is correct
17 Correct 10 ms 15996 KB Output is correct
18 Correct 122 ms 45676 KB Output is correct
19 Correct 157 ms 48296 KB Output is correct
20 Correct 12 ms 16780 KB Output is correct
21 Correct 10 ms 15924 KB Output is correct
22 Correct 129 ms 44020 KB Output is correct
23 Correct 12 ms 16780 KB Output is correct
24 Correct 150 ms 41692 KB Output is correct
25 Correct 163 ms 48004 KB Output is correct
26 Correct 11 ms 16864 KB Output is correct
27 Correct 12 ms 16884 KB Output is correct
28 Correct 12 ms 16716 KB Output is correct
29 Correct 12 ms 16684 KB Output is correct
30 Correct 11 ms 16844 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 15996 KB Output is correct
2 Correct 11 ms 15988 KB Output is correct
3 Correct 9 ms 15948 KB Output is correct
4 Correct 9 ms 15948 KB Output is correct
5 Correct 9 ms 16000 KB Output is correct
6 Correct 9 ms 15948 KB Output is correct
7 Correct 9 ms 15916 KB Output is correct
8 Correct 9 ms 15948 KB Output is correct
9 Correct 9 ms 15956 KB Output is correct
10 Correct 9 ms 15948 KB Output is correct
11 Correct 9 ms 15948 KB Output is correct
12 Correct 9 ms 15948 KB Output is correct
13 Correct 9 ms 15948 KB Output is correct
14 Correct 11 ms 15948 KB Output is correct
15 Correct 11 ms 15948 KB Output is correct
16 Correct 9 ms 16004 KB Output is correct
17 Correct 9 ms 16000 KB Output is correct
18 Correct 9 ms 15944 KB Output is correct
19 Correct 9 ms 15948 KB Output is correct
20 Correct 9 ms 15948 KB Output is correct
21 Correct 9 ms 15948 KB Output is correct
22 Correct 9 ms 16004 KB Output is correct
23 Correct 9 ms 16076 KB Output is correct
24 Correct 10 ms 15976 KB Output is correct
25 Correct 9 ms 16000 KB Output is correct
26 Correct 11 ms 16780 KB Output is correct
27 Correct 11 ms 16716 KB Output is correct
28 Correct 11 ms 16892 KB Output is correct
29 Correct 12 ms 16776 KB Output is correct
30 Correct 13 ms 16892 KB Output is correct
31 Correct 9 ms 16024 KB Output is correct
32 Correct 12 ms 16984 KB Output is correct
33 Correct 9 ms 15960 KB Output is correct
34 Correct 12 ms 16768 KB Output is correct
35 Correct 12 ms 16844 KB Output is correct
36 Correct 12 ms 16740 KB Output is correct
37 Correct 13 ms 16844 KB Output is correct
38 Correct 9 ms 15996 KB Output is correct
39 Correct 12 ms 16716 KB Output is correct
40 Correct 12 ms 16640 KB Output is correct
41 Correct 11 ms 16716 KB Output is correct
42 Correct 12 ms 16832 KB Output is correct
43 Correct 11 ms 16844 KB Output is correct
44 Correct 9 ms 15944 KB Output is correct
45 Correct 12 ms 16912 KB Output is correct
46 Correct 12 ms 16716 KB Output is correct
47 Correct 10 ms 15916 KB Output is correct
48 Correct 12 ms 16776 KB Output is correct
49 Correct 11 ms 16780 KB Output is correct
50 Correct 12 ms 16844 KB Output is correct
51 Correct 11 ms 16776 KB Output is correct
52 Correct 11 ms 16780 KB Output is correct
53 Correct 11 ms 16784 KB Output is correct
54 Correct 11 ms 16716 KB Output is correct
55 Correct 9 ms 15996 KB Output is correct
56 Correct 121 ms 41228 KB Output is correct
57 Correct 170 ms 41028 KB Output is correct
58 Correct 11 ms 16716 KB Output is correct
59 Correct 9 ms 15948 KB Output is correct
60 Correct 9 ms 15996 KB Output is correct
61 Correct 160 ms 40900 KB Output is correct
62 Correct 12 ms 16716 KB Output is correct
63 Correct 213 ms 46116 KB Output is correct
64 Correct 180 ms 40936 KB Output is correct
65 Correct 12 ms 16716 KB Output is correct
66 Correct 164 ms 40900 KB Output is correct
67 Correct 12 ms 16752 KB Output is correct
68 Correct 14 ms 16716 KB Output is correct
69 Correct 118 ms 41124 KB Output is correct
70 Correct 11 ms 16908 KB Output is correct
71 Correct 155 ms 40696 KB Output is correct
72 Correct 160 ms 42260 KB Output is correct
73 Correct 9 ms 15948 KB Output is correct
74 Correct 174 ms 46388 KB Output is correct
75 Correct 181 ms 44640 KB Output is correct
76 Correct 11 ms 16716 KB Output is correct
77 Correct 12 ms 16656 KB Output is correct
78 Correct 113 ms 41232 KB Output is correct
79 Correct 109 ms 45960 KB Output is correct
80 Correct 12 ms 16844 KB Output is correct
81 Correct 12 ms 16656 KB Output is correct
82 Correct 9 ms 15948 KB Output is correct
83 Correct 9 ms 15928 KB Output is correct
84 Correct 12 ms 16748 KB Output is correct
85 Correct 166 ms 43944 KB Output is correct
86 Correct 12 ms 16716 KB Output is correct
87 Correct 146 ms 41200 KB Output is correct
88 Correct 10 ms 15948 KB Output is correct
89 Correct 139 ms 40640 KB Output is correct
90 Correct 154 ms 43844 KB Output is correct
91 Correct 137 ms 45632 KB Output is correct
92 Correct 115 ms 41284 KB Output is correct
93 Correct 12 ms 16768 KB Output is correct
94 Correct 10 ms 15996 KB Output is correct
95 Correct 122 ms 45676 KB Output is correct
96 Correct 157 ms 48296 KB Output is correct
97 Correct 12 ms 16780 KB Output is correct
98 Correct 10 ms 15924 KB Output is correct
99 Correct 129 ms 44020 KB Output is correct
100 Correct 12 ms 16780 KB Output is correct
101 Correct 150 ms 41692 KB Output is correct
102 Correct 163 ms 48004 KB Output is correct
103 Correct 11 ms 16864 KB Output is correct
104 Correct 12 ms 16884 KB Output is correct
105 Correct 12 ms 16716 KB Output is correct
106 Correct 12 ms 16684 KB Output is correct
107 Correct 11 ms 16844 KB Output is correct
108 Correct 810 ms 155180 KB Output is correct
109 Correct 1230 ms 152052 KB Output is correct
110 Correct 1243 ms 161044 KB Output is correct
111 Correct 1330 ms 181876 KB Output is correct
112 Correct 1307 ms 178056 KB Output is correct
113 Correct 811 ms 166288 KB Output is correct
114 Correct 989 ms 140220 KB Output is correct
115 Correct 966 ms 140204 KB Output is correct
116 Correct 1213 ms 149136 KB Output is correct
117 Correct 1020 ms 166128 KB Output is correct
118 Correct 940 ms 141072 KB Output is correct
119 Correct 1020 ms 166000 KB Output is correct
120 Correct 1293 ms 178604 KB Output is correct
121 Correct 1094 ms 165924 KB Output is correct
122 Correct 1292 ms 159412 KB Output is correct
123 Correct 850 ms 166824 KB Output is correct
124 Correct 1169 ms 160236 KB Output is correct