답안 #375970

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
375970 2021-03-10T11:10:22 Z pit4h Mergers (JOI19_mergers) C++14
100 / 100
840 ms 77420 KB
#include<bits/stdc++.h>
#define st first
#define nd second
#define mp make_pair
#ifndef LOCAL
#define cerr if(0)cerr
#endif
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using pii = pair<int, int>;

const int MAXN = 5e5+1;
int n, k, s[MAXN];
vector<int> g[MAXN];
int pre[MAXN], subt[MAXN], nr;
int maxp[MAXN], minp[MAXN];
int max_subt[MAXN], min_subt[MAXN];
bool bad[MAXN];
int cnt[MAXN], last;
int leaves;
void dfs(int x, int p) {
	pre[x] = ++nr;
	subt[x] = 1;
	for(int i: g[x]) {
		if(i!=p) {
			dfs(i, x);
			subt[x] += subt[i];
		}
	}
}
void solve(int x, int p) {
	max_subt[x] = maxp[s[x]];
	min_subt[x] = minp[s[x]];
	for(int i: g[x]) {
		if(i!=p) {
			solve(i, x);
			if(bad[i]) {
				bad[x] = 1;
				cnt[x]++;
			}
			max_subt[x] = max(max_subt[x], max_subt[i]);
			min_subt[x] = min(min_subt[x], min_subt[i]);
		}
	}
	if(x!=1 && (min_subt[x] >= pre[x] && max_subt[x] <= pre[x]+subt[x]-1)) {
		if(!bad[x]) leaves++;
		bad[x] = 1;
		last = p;
	}
	else if(cnt[x] > 1) {
		last = 0;
	}
}
int main() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin>>n>>k;
	for(int i=0; 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>>s[i];
	}
	dfs(1, 1);
	for(int i=1; i<=n; ++i) {
		maxp[s[i]] = max(maxp[s[i]], pre[i]);	
		minp[s[i]] = min(minp[s[i]], pre[i]);
		if(minp[s[i]]==0) minp[s[i]] = pre[i];
	}
	solve(1, 1);
	if(leaves>0) {
		if(last!=0) {
			leaves++;
		}
		cout<<(leaves-1)/2+1<<'\n';
	}
	else cout<<0<<'\n';
}
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 12268 KB Output is correct
2 Correct 9 ms 12140 KB Output is correct
3 Correct 9 ms 12140 KB Output is correct
4 Correct 9 ms 12140 KB Output is correct
5 Correct 10 ms 12140 KB Output is correct
6 Correct 9 ms 12140 KB Output is correct
7 Correct 10 ms 12140 KB Output is correct
8 Correct 9 ms 12140 KB Output is correct
9 Correct 9 ms 12140 KB Output is correct
10 Correct 8 ms 12140 KB Output is correct
11 Correct 11 ms 12140 KB Output is correct
12 Correct 10 ms 12140 KB Output is correct
13 Correct 10 ms 12140 KB Output is correct
14 Correct 9 ms 12140 KB Output is correct
15 Correct 9 ms 12140 KB Output is correct
16 Correct 9 ms 12140 KB Output is correct
17 Correct 9 ms 12268 KB Output is correct
18 Correct 9 ms 12268 KB Output is correct
19 Correct 9 ms 12140 KB Output is correct
20 Correct 9 ms 12140 KB Output is correct
21 Correct 9 ms 12140 KB Output is correct
22 Correct 9 ms 12140 KB Output is correct
23 Correct 9 ms 12160 KB Output is correct
24 Correct 9 ms 12140 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 12268 KB Output is correct
2 Correct 9 ms 12140 KB Output is correct
3 Correct 9 ms 12140 KB Output is correct
4 Correct 9 ms 12140 KB Output is correct
5 Correct 10 ms 12140 KB Output is correct
6 Correct 9 ms 12140 KB Output is correct
7 Correct 10 ms 12140 KB Output is correct
8 Correct 9 ms 12140 KB Output is correct
9 Correct 9 ms 12140 KB Output is correct
10 Correct 8 ms 12140 KB Output is correct
11 Correct 11 ms 12140 KB Output is correct
12 Correct 10 ms 12140 KB Output is correct
13 Correct 10 ms 12140 KB Output is correct
14 Correct 9 ms 12140 KB Output is correct
15 Correct 9 ms 12140 KB Output is correct
16 Correct 9 ms 12140 KB Output is correct
17 Correct 9 ms 12268 KB Output is correct
18 Correct 9 ms 12268 KB Output is correct
19 Correct 9 ms 12140 KB Output is correct
20 Correct 9 ms 12140 KB Output is correct
21 Correct 9 ms 12140 KB Output is correct
22 Correct 9 ms 12140 KB Output is correct
23 Correct 9 ms 12160 KB Output is correct
24 Correct 9 ms 12140 KB Output is correct
25 Correct 9 ms 12140 KB Output is correct
26 Correct 11 ms 12396 KB Output is correct
27 Correct 10 ms 12396 KB Output is correct
28 Correct 12 ms 12672 KB Output is correct
29 Correct 12 ms 12396 KB Output is correct
30 Correct 11 ms 12396 KB Output is correct
31 Correct 9 ms 12268 KB Output is correct
32 Correct 10 ms 12544 KB Output is correct
33 Correct 9 ms 12160 KB Output is correct
34 Correct 10 ms 12396 KB Output is correct
35 Correct 10 ms 12396 KB Output is correct
36 Correct 10 ms 12396 KB Output is correct
37 Correct 13 ms 12396 KB Output is correct
38 Correct 9 ms 12140 KB Output is correct
39 Correct 10 ms 12416 KB Output is correct
40 Correct 11 ms 12268 KB Output is correct
41 Correct 12 ms 12396 KB Output is correct
42 Correct 10 ms 12396 KB Output is correct
43 Correct 10 ms 12524 KB Output is correct
44 Correct 9 ms 12140 KB Output is correct
45 Correct 10 ms 12396 KB Output is correct
46 Correct 11 ms 12396 KB Output is correct
47 Correct 9 ms 12140 KB Output is correct
48 Correct 10 ms 12396 KB Output is correct
49 Correct 10 ms 12396 KB Output is correct
50 Correct 10 ms 12396 KB Output is correct
51 Correct 11 ms 12396 KB Output is correct
52 Correct 10 ms 12396 KB Output is correct
53 Correct 10 ms 12396 KB Output is correct
54 Correct 10 ms 12268 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 12268 KB Output is correct
2 Correct 9 ms 12140 KB Output is correct
3 Correct 9 ms 12140 KB Output is correct
4 Correct 9 ms 12140 KB Output is correct
5 Correct 10 ms 12140 KB Output is correct
6 Correct 9 ms 12140 KB Output is correct
7 Correct 10 ms 12140 KB Output is correct
8 Correct 9 ms 12140 KB Output is correct
9 Correct 9 ms 12140 KB Output is correct
10 Correct 8 ms 12140 KB Output is correct
11 Correct 11 ms 12140 KB Output is correct
12 Correct 10 ms 12140 KB Output is correct
13 Correct 10 ms 12140 KB Output is correct
14 Correct 9 ms 12140 KB Output is correct
15 Correct 9 ms 12140 KB Output is correct
16 Correct 9 ms 12140 KB Output is correct
17 Correct 9 ms 12268 KB Output is correct
18 Correct 9 ms 12268 KB Output is correct
19 Correct 9 ms 12140 KB Output is correct
20 Correct 9 ms 12140 KB Output is correct
21 Correct 9 ms 12140 KB Output is correct
22 Correct 9 ms 12140 KB Output is correct
23 Correct 9 ms 12160 KB Output is correct
24 Correct 9 ms 12140 KB Output is correct
25 Correct 9 ms 12140 KB Output is correct
26 Correct 66 ms 19044 KB Output is correct
27 Correct 89 ms 18796 KB Output is correct
28 Correct 10 ms 12396 KB Output is correct
29 Correct 9 ms 12140 KB Output is correct
30 Correct 9 ms 12140 KB Output is correct
31 Correct 90 ms 19308 KB Output is correct
32 Correct 10 ms 12396 KB Output is correct
33 Correct 105 ms 25964 KB Output is correct
34 Correct 88 ms 18924 KB Output is correct
35 Correct 11 ms 12396 KB Output is correct
36 Correct 100 ms 19948 KB Output is correct
37 Correct 10 ms 12268 KB Output is correct
38 Correct 10 ms 12396 KB Output is correct
39 Correct 60 ms 19044 KB Output is correct
40 Correct 10 ms 12524 KB Output is correct
41 Correct 110 ms 18796 KB Output is correct
42 Correct 99 ms 21640 KB Output is correct
43 Correct 9 ms 12140 KB Output is correct
44 Correct 114 ms 26220 KB Output is correct
45 Correct 102 ms 22508 KB Output is correct
46 Correct 10 ms 12396 KB Output is correct
47 Correct 10 ms 12268 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 61 ms 17636 KB Output is correct
2 Correct 64 ms 18532 KB Output is correct
3 Correct 10 ms 12396 KB Output is correct
4 Correct 10 ms 12396 KB Output is correct
5 Correct 9 ms 12140 KB Output is correct
6 Correct 9 ms 12140 KB Output is correct
7 Correct 10 ms 12292 KB Output is correct
8 Correct 95 ms 19948 KB Output is correct
9 Correct 10 ms 12396 KB Output is correct
10 Correct 98 ms 19436 KB Output is correct
11 Correct 9 ms 12140 KB Output is correct
12 Correct 108 ms 19436 KB Output is correct
13 Correct 112 ms 19948 KB Output is correct
14 Correct 94 ms 20332 KB Output is correct
15 Correct 77 ms 19072 KB Output is correct
16 Correct 11 ms 12396 KB Output is correct
17 Correct 9 ms 12140 KB Output is correct
18 Correct 63 ms 20196 KB Output is correct
19 Correct 124 ms 24428 KB Output is correct
20 Correct 10 ms 12396 KB Output is correct
21 Correct 9 ms 12140 KB Output is correct
22 Correct 65 ms 19812 KB Output is correct
23 Correct 11 ms 12396 KB Output is correct
24 Correct 101 ms 19532 KB Output is correct
25 Correct 128 ms 23148 KB Output is correct
26 Correct 12 ms 12400 KB Output is correct
27 Correct 10 ms 12396 KB Output is correct
28 Correct 11 ms 12396 KB Output is correct
29 Correct 10 ms 12396 KB Output is correct
30 Correct 10 ms 12396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 12268 KB Output is correct
2 Correct 9 ms 12140 KB Output is correct
3 Correct 9 ms 12140 KB Output is correct
4 Correct 9 ms 12140 KB Output is correct
5 Correct 10 ms 12140 KB Output is correct
6 Correct 9 ms 12140 KB Output is correct
7 Correct 10 ms 12140 KB Output is correct
8 Correct 9 ms 12140 KB Output is correct
9 Correct 9 ms 12140 KB Output is correct
10 Correct 8 ms 12140 KB Output is correct
11 Correct 11 ms 12140 KB Output is correct
12 Correct 10 ms 12140 KB Output is correct
13 Correct 10 ms 12140 KB Output is correct
14 Correct 9 ms 12140 KB Output is correct
15 Correct 9 ms 12140 KB Output is correct
16 Correct 9 ms 12140 KB Output is correct
17 Correct 9 ms 12268 KB Output is correct
18 Correct 9 ms 12268 KB Output is correct
19 Correct 9 ms 12140 KB Output is correct
20 Correct 9 ms 12140 KB Output is correct
21 Correct 9 ms 12140 KB Output is correct
22 Correct 9 ms 12140 KB Output is correct
23 Correct 9 ms 12160 KB Output is correct
24 Correct 9 ms 12140 KB Output is correct
25 Correct 9 ms 12140 KB Output is correct
26 Correct 11 ms 12396 KB Output is correct
27 Correct 10 ms 12396 KB Output is correct
28 Correct 12 ms 12672 KB Output is correct
29 Correct 12 ms 12396 KB Output is correct
30 Correct 11 ms 12396 KB Output is correct
31 Correct 9 ms 12268 KB Output is correct
32 Correct 10 ms 12544 KB Output is correct
33 Correct 9 ms 12160 KB Output is correct
34 Correct 10 ms 12396 KB Output is correct
35 Correct 10 ms 12396 KB Output is correct
36 Correct 10 ms 12396 KB Output is correct
37 Correct 13 ms 12396 KB Output is correct
38 Correct 9 ms 12140 KB Output is correct
39 Correct 10 ms 12416 KB Output is correct
40 Correct 11 ms 12268 KB Output is correct
41 Correct 12 ms 12396 KB Output is correct
42 Correct 10 ms 12396 KB Output is correct
43 Correct 10 ms 12524 KB Output is correct
44 Correct 9 ms 12140 KB Output is correct
45 Correct 10 ms 12396 KB Output is correct
46 Correct 11 ms 12396 KB Output is correct
47 Correct 9 ms 12140 KB Output is correct
48 Correct 10 ms 12396 KB Output is correct
49 Correct 10 ms 12396 KB Output is correct
50 Correct 10 ms 12396 KB Output is correct
51 Correct 11 ms 12396 KB Output is correct
52 Correct 10 ms 12396 KB Output is correct
53 Correct 10 ms 12396 KB Output is correct
54 Correct 10 ms 12268 KB Output is correct
55 Correct 9 ms 12140 KB Output is correct
56 Correct 66 ms 19044 KB Output is correct
57 Correct 89 ms 18796 KB Output is correct
58 Correct 10 ms 12396 KB Output is correct
59 Correct 9 ms 12140 KB Output is correct
60 Correct 9 ms 12140 KB Output is correct
61 Correct 90 ms 19308 KB Output is correct
62 Correct 10 ms 12396 KB Output is correct
63 Correct 105 ms 25964 KB Output is correct
64 Correct 88 ms 18924 KB Output is correct
65 Correct 11 ms 12396 KB Output is correct
66 Correct 100 ms 19948 KB Output is correct
67 Correct 10 ms 12268 KB Output is correct
68 Correct 10 ms 12396 KB Output is correct
69 Correct 60 ms 19044 KB Output is correct
70 Correct 10 ms 12524 KB Output is correct
71 Correct 110 ms 18796 KB Output is correct
72 Correct 99 ms 21640 KB Output is correct
73 Correct 9 ms 12140 KB Output is correct
74 Correct 114 ms 26220 KB Output is correct
75 Correct 102 ms 22508 KB Output is correct
76 Correct 10 ms 12396 KB Output is correct
77 Correct 10 ms 12268 KB Output is correct
78 Correct 61 ms 17636 KB Output is correct
79 Correct 64 ms 18532 KB Output is correct
80 Correct 10 ms 12396 KB Output is correct
81 Correct 10 ms 12396 KB Output is correct
82 Correct 9 ms 12140 KB Output is correct
83 Correct 9 ms 12140 KB Output is correct
84 Correct 10 ms 12292 KB Output is correct
85 Correct 95 ms 19948 KB Output is correct
86 Correct 10 ms 12396 KB Output is correct
87 Correct 98 ms 19436 KB Output is correct
88 Correct 9 ms 12140 KB Output is correct
89 Correct 108 ms 19436 KB Output is correct
90 Correct 112 ms 19948 KB Output is correct
91 Correct 94 ms 20332 KB Output is correct
92 Correct 77 ms 19072 KB Output is correct
93 Correct 11 ms 12396 KB Output is correct
94 Correct 9 ms 12140 KB Output is correct
95 Correct 63 ms 20196 KB Output is correct
96 Correct 124 ms 24428 KB Output is correct
97 Correct 10 ms 12396 KB Output is correct
98 Correct 9 ms 12140 KB Output is correct
99 Correct 65 ms 19812 KB Output is correct
100 Correct 11 ms 12396 KB Output is correct
101 Correct 101 ms 19532 KB Output is correct
102 Correct 128 ms 23148 KB Output is correct
103 Correct 12 ms 12400 KB Output is correct
104 Correct 10 ms 12396 KB Output is correct
105 Correct 11 ms 12396 KB Output is correct
106 Correct 10 ms 12396 KB Output is correct
107 Correct 10 ms 12396 KB Output is correct
108 Correct 418 ms 51532 KB Output is correct
109 Correct 767 ms 66424 KB Output is correct
110 Correct 747 ms 74984 KB Output is correct
111 Correct 840 ms 77420 KB Output is correct
112 Correct 747 ms 67820 KB Output is correct
113 Correct 435 ms 53588 KB Output is correct
114 Correct 626 ms 45908 KB Output is correct
115 Correct 671 ms 45728 KB Output is correct
116 Correct 647 ms 50924 KB Output is correct
117 Correct 652 ms 54380 KB Output is correct
118 Correct 633 ms 48364 KB Output is correct
119 Correct 688 ms 54380 KB Output is correct
120 Correct 729 ms 68588 KB Output is correct
121 Correct 664 ms 54536 KB Output is correct
122 Correct 690 ms 52940 KB Output is correct
123 Correct 423 ms 53980 KB Output is correct
124 Correct 739 ms 53612 KB Output is correct