답안 #416568

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
416568 2021-06-02T16:05:16 Z amirmohammad_nezami Mergers (JOI19_mergers) C++14
100 / 100
1270 ms 144572 KB
//                                                          In the name of God 
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define lc 2 * v
#define rc 2 * v + 1
#define mid (s + e) / 2
#define _sz(e) (int)e.size()
#define _all(e) e.begin() , e.end()
#define _lcm(a , b) (1ll * a * b) / __gcd(a , b)
#define ll long long
#define int long long
#define ld long double
#define pii pair <int , int>
#define pcc pair <char , char>
#define pll pair <long long , long long>
#define piii pair <int , pair <int , int>>
#define FAST ios::sync_with_stdio(0);cin.tie(0)
#pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,avmini,avmini2")

const int maxn = 5e5 + 10 , N = 2e6 + 10 , SQ = 320 , base = 800287 , mod = 1e9 + 7 , INF = 1e18 + 10 , lg = 22;
const ld eps = 1e-4;

int n , k , s[maxn] , mn_st[maxn] , mx_ft[maxn], st[maxn] , ft[maxn] , tme;
int mx_sub[maxn] , mn_sub[maxn] , mark[maxn] , cnt[maxn];
vector <int> edges[maxn];

void dfs1(int v , int p = -1) {
	st[v] = tme++;
	if(mn_st[s[v]] == -1) {
		mn_st[s[v]] = st[v];
	}	

	for (auto u : edges[v]) {
		if(u != p) {
			dfs1(u , v);
		}
	}
	ft[v] = tme;
	mx_ft[s[v]] = ft[v];
}

void dfs2(int v , int p = -1) {
	mn_sub[v] = mn_st[s[v]] , mx_sub[v] = mx_ft[s[v]];
	for (auto u : edges[v]) {
		if(u != p) {
			dfs2(u , v);
			mn_sub[v] = min(mn_sub[v] , mn_sub[u]);
			mx_sub[v] = max(mx_sub[v] , mx_sub[u]);
		}
	}

	if(p != -1 && mn_sub[v] == st[v] && mx_sub[v] == ft[v]) {
		mark[v] = 1;
	}
}

void dfs3(int v , int p = -1 , int root = 0) {
	for (auto u : edges[v]) {
		if(u != p) {
			if(mark[u]) {
				cnt[root]++ , cnt[u]++;
				dfs3(u , v , u);
			}
			else {
				dfs3(u , v , root);
			}
		}
	}
}

int32_t main() {	
	FAST;
	cin >> n >> k;
	for (int i = 0; i < n - 1; ++i) {
		int x , y; cin >> x >> y;
		x-- , y--;
		edges[x].PB(y);
		edges[y].PB(x);
	}
	for (int i = 0; i < n; ++i) {
		cin >> s[i]; s[i]--;
	}
	for (int i = 0; i < k; ++i) {
		mn_st[i] = -1;
	}

	dfs1(0);
	dfs2(0);
	dfs3(0);
	
	int ans = 0;
	for (int i = 0; i < n; ++i) {
		ans += (cnt[i] == 1);
	}
	cout << (ans + 1) / 2 << '\n';
} 

//  Mistakes:
//  * Read the problem carefully.
//  * Check maxn.
//  * Overflows.
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 12048 KB Output is correct
2 Correct 8 ms 12108 KB Output is correct
3 Correct 8 ms 12064 KB Output is correct
4 Correct 9 ms 12060 KB Output is correct
5 Correct 11 ms 12100 KB Output is correct
6 Correct 9 ms 12108 KB Output is correct
7 Correct 8 ms 12108 KB Output is correct
8 Correct 7 ms 12108 KB Output is correct
9 Correct 10 ms 12088 KB Output is correct
10 Correct 10 ms 12064 KB Output is correct
11 Correct 9 ms 12140 KB Output is correct
12 Correct 8 ms 12108 KB Output is correct
13 Correct 7 ms 12060 KB Output is correct
14 Correct 7 ms 12108 KB Output is correct
15 Correct 10 ms 12108 KB Output is correct
16 Correct 11 ms 12076 KB Output is correct
17 Correct 8 ms 12064 KB Output is correct
18 Correct 8 ms 12056 KB Output is correct
19 Correct 9 ms 12064 KB Output is correct
20 Correct 8 ms 12108 KB Output is correct
21 Correct 8 ms 12108 KB Output is correct
22 Correct 11 ms 12128 KB Output is correct
23 Correct 8 ms 12108 KB Output is correct
24 Correct 9 ms 12064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 12048 KB Output is correct
2 Correct 8 ms 12108 KB Output is correct
3 Correct 8 ms 12064 KB Output is correct
4 Correct 9 ms 12060 KB Output is correct
5 Correct 11 ms 12100 KB Output is correct
6 Correct 9 ms 12108 KB Output is correct
7 Correct 8 ms 12108 KB Output is correct
8 Correct 7 ms 12108 KB Output is correct
9 Correct 10 ms 12088 KB Output is correct
10 Correct 10 ms 12064 KB Output is correct
11 Correct 9 ms 12140 KB Output is correct
12 Correct 8 ms 12108 KB Output is correct
13 Correct 7 ms 12060 KB Output is correct
14 Correct 7 ms 12108 KB Output is correct
15 Correct 10 ms 12108 KB Output is correct
16 Correct 11 ms 12076 KB Output is correct
17 Correct 8 ms 12064 KB Output is correct
18 Correct 8 ms 12056 KB Output is correct
19 Correct 9 ms 12064 KB Output is correct
20 Correct 8 ms 12108 KB Output is correct
21 Correct 8 ms 12108 KB Output is correct
22 Correct 11 ms 12128 KB Output is correct
23 Correct 8 ms 12108 KB Output is correct
24 Correct 9 ms 12064 KB Output is correct
25 Correct 8 ms 12108 KB Output is correct
26 Correct 10 ms 12476 KB Output is correct
27 Correct 10 ms 12364 KB Output is correct
28 Correct 10 ms 12620 KB Output is correct
29 Correct 10 ms 12452 KB Output is correct
30 Correct 11 ms 12364 KB Output is correct
31 Correct 7 ms 12108 KB Output is correct
32 Correct 10 ms 12592 KB Output is correct
33 Correct 7 ms 12068 KB Output is correct
34 Correct 10 ms 12368 KB Output is correct
35 Correct 13 ms 12492 KB Output is correct
36 Correct 9 ms 12364 KB Output is correct
37 Correct 11 ms 12380 KB Output is correct
38 Correct 8 ms 12052 KB Output is correct
39 Correct 12 ms 12388 KB Output is correct
40 Correct 11 ms 12364 KB Output is correct
41 Correct 9 ms 12364 KB Output is correct
42 Correct 9 ms 12400 KB Output is correct
43 Correct 9 ms 12588 KB Output is correct
44 Correct 8 ms 12108 KB Output is correct
45 Correct 11 ms 12448 KB Output is correct
46 Correct 10 ms 12364 KB Output is correct
47 Correct 10 ms 12108 KB Output is correct
48 Correct 10 ms 12364 KB Output is correct
49 Correct 9 ms 12492 KB Output is correct
50 Correct 9 ms 12716 KB Output is correct
51 Correct 9 ms 12364 KB Output is correct
52 Correct 24 ms 12324 KB Output is correct
53 Correct 11 ms 12364 KB Output is correct
54 Correct 11 ms 12332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 12048 KB Output is correct
2 Correct 8 ms 12108 KB Output is correct
3 Correct 8 ms 12064 KB Output is correct
4 Correct 9 ms 12060 KB Output is correct
5 Correct 11 ms 12100 KB Output is correct
6 Correct 9 ms 12108 KB Output is correct
7 Correct 8 ms 12108 KB Output is correct
8 Correct 7 ms 12108 KB Output is correct
9 Correct 10 ms 12088 KB Output is correct
10 Correct 10 ms 12064 KB Output is correct
11 Correct 9 ms 12140 KB Output is correct
12 Correct 8 ms 12108 KB Output is correct
13 Correct 7 ms 12060 KB Output is correct
14 Correct 7 ms 12108 KB Output is correct
15 Correct 10 ms 12108 KB Output is correct
16 Correct 11 ms 12076 KB Output is correct
17 Correct 8 ms 12064 KB Output is correct
18 Correct 8 ms 12056 KB Output is correct
19 Correct 9 ms 12064 KB Output is correct
20 Correct 8 ms 12108 KB Output is correct
21 Correct 8 ms 12108 KB Output is correct
22 Correct 11 ms 12128 KB Output is correct
23 Correct 8 ms 12108 KB Output is correct
24 Correct 9 ms 12064 KB Output is correct
25 Correct 14 ms 12068 KB Output is correct
26 Correct 103 ms 21264 KB Output is correct
27 Correct 143 ms 21108 KB Output is correct
28 Correct 9 ms 12364 KB Output is correct
29 Correct 8 ms 12108 KB Output is correct
30 Correct 8 ms 12064 KB Output is correct
31 Correct 122 ms 21508 KB Output is correct
32 Correct 8 ms 12364 KB Output is correct
33 Correct 155 ms 27860 KB Output is correct
34 Correct 132 ms 21124 KB Output is correct
35 Correct 17 ms 12364 KB Output is correct
36 Correct 131 ms 21668 KB Output is correct
37 Correct 9 ms 12364 KB Output is correct
38 Correct 12 ms 12296 KB Output is correct
39 Correct 83 ms 21300 KB Output is correct
40 Correct 9 ms 12592 KB Output is correct
41 Correct 195 ms 21056 KB Output is correct
42 Correct 177 ms 22956 KB Output is correct
43 Correct 8 ms 12108 KB Output is correct
44 Correct 159 ms 28056 KB Output is correct
45 Correct 138 ms 25504 KB Output is correct
46 Correct 11 ms 12328 KB Output is correct
47 Correct 12 ms 12332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 143 ms 21344 KB Output is correct
2 Correct 84 ms 24724 KB Output is correct
3 Correct 9 ms 12464 KB Output is correct
4 Correct 10 ms 12408 KB Output is correct
5 Correct 9 ms 12064 KB Output is correct
6 Correct 8 ms 12108 KB Output is correct
7 Correct 10 ms 12364 KB Output is correct
8 Correct 155 ms 23808 KB Output is correct
9 Correct 9 ms 12336 KB Output is correct
10 Correct 177 ms 22828 KB Output is correct
11 Correct 9 ms 12052 KB Output is correct
12 Correct 113 ms 22304 KB Output is correct
13 Correct 149 ms 23748 KB Output is correct
14 Correct 153 ms 24604 KB Output is correct
15 Correct 74 ms 21292 KB Output is correct
16 Correct 9 ms 12468 KB Output is correct
17 Correct 8 ms 12048 KB Output is correct
18 Correct 94 ms 24660 KB Output is correct
19 Correct 235 ms 37684 KB Output is correct
20 Correct 9 ms 12464 KB Output is correct
21 Correct 7 ms 12136 KB Output is correct
22 Correct 87 ms 23880 KB Output is correct
23 Correct 9 ms 12364 KB Output is correct
24 Correct 153 ms 22768 KB Output is correct
25 Correct 176 ms 34292 KB Output is correct
26 Correct 13 ms 12444 KB Output is correct
27 Correct 14 ms 12656 KB Output is correct
28 Correct 12 ms 12332 KB Output is correct
29 Correct 13 ms 12404 KB Output is correct
30 Correct 10 ms 12364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 12048 KB Output is correct
2 Correct 8 ms 12108 KB Output is correct
3 Correct 8 ms 12064 KB Output is correct
4 Correct 9 ms 12060 KB Output is correct
5 Correct 11 ms 12100 KB Output is correct
6 Correct 9 ms 12108 KB Output is correct
7 Correct 8 ms 12108 KB Output is correct
8 Correct 7 ms 12108 KB Output is correct
9 Correct 10 ms 12088 KB Output is correct
10 Correct 10 ms 12064 KB Output is correct
11 Correct 9 ms 12140 KB Output is correct
12 Correct 8 ms 12108 KB Output is correct
13 Correct 7 ms 12060 KB Output is correct
14 Correct 7 ms 12108 KB Output is correct
15 Correct 10 ms 12108 KB Output is correct
16 Correct 11 ms 12076 KB Output is correct
17 Correct 8 ms 12064 KB Output is correct
18 Correct 8 ms 12056 KB Output is correct
19 Correct 9 ms 12064 KB Output is correct
20 Correct 8 ms 12108 KB Output is correct
21 Correct 8 ms 12108 KB Output is correct
22 Correct 11 ms 12128 KB Output is correct
23 Correct 8 ms 12108 KB Output is correct
24 Correct 9 ms 12064 KB Output is correct
25 Correct 8 ms 12108 KB Output is correct
26 Correct 10 ms 12476 KB Output is correct
27 Correct 10 ms 12364 KB Output is correct
28 Correct 10 ms 12620 KB Output is correct
29 Correct 10 ms 12452 KB Output is correct
30 Correct 11 ms 12364 KB Output is correct
31 Correct 7 ms 12108 KB Output is correct
32 Correct 10 ms 12592 KB Output is correct
33 Correct 7 ms 12068 KB Output is correct
34 Correct 10 ms 12368 KB Output is correct
35 Correct 13 ms 12492 KB Output is correct
36 Correct 9 ms 12364 KB Output is correct
37 Correct 11 ms 12380 KB Output is correct
38 Correct 8 ms 12052 KB Output is correct
39 Correct 12 ms 12388 KB Output is correct
40 Correct 11 ms 12364 KB Output is correct
41 Correct 9 ms 12364 KB Output is correct
42 Correct 9 ms 12400 KB Output is correct
43 Correct 9 ms 12588 KB Output is correct
44 Correct 8 ms 12108 KB Output is correct
45 Correct 11 ms 12448 KB Output is correct
46 Correct 10 ms 12364 KB Output is correct
47 Correct 10 ms 12108 KB Output is correct
48 Correct 10 ms 12364 KB Output is correct
49 Correct 9 ms 12492 KB Output is correct
50 Correct 9 ms 12716 KB Output is correct
51 Correct 9 ms 12364 KB Output is correct
52 Correct 24 ms 12324 KB Output is correct
53 Correct 11 ms 12364 KB Output is correct
54 Correct 11 ms 12332 KB Output is correct
55 Correct 14 ms 12068 KB Output is correct
56 Correct 103 ms 21264 KB Output is correct
57 Correct 143 ms 21108 KB Output is correct
58 Correct 9 ms 12364 KB Output is correct
59 Correct 8 ms 12108 KB Output is correct
60 Correct 8 ms 12064 KB Output is correct
61 Correct 122 ms 21508 KB Output is correct
62 Correct 8 ms 12364 KB Output is correct
63 Correct 155 ms 27860 KB Output is correct
64 Correct 132 ms 21124 KB Output is correct
65 Correct 17 ms 12364 KB Output is correct
66 Correct 131 ms 21668 KB Output is correct
67 Correct 9 ms 12364 KB Output is correct
68 Correct 12 ms 12296 KB Output is correct
69 Correct 83 ms 21300 KB Output is correct
70 Correct 9 ms 12592 KB Output is correct
71 Correct 195 ms 21056 KB Output is correct
72 Correct 177 ms 22956 KB Output is correct
73 Correct 8 ms 12108 KB Output is correct
74 Correct 159 ms 28056 KB Output is correct
75 Correct 138 ms 25504 KB Output is correct
76 Correct 11 ms 12328 KB Output is correct
77 Correct 12 ms 12332 KB Output is correct
78 Correct 143 ms 21344 KB Output is correct
79 Correct 84 ms 24724 KB Output is correct
80 Correct 9 ms 12464 KB Output is correct
81 Correct 10 ms 12408 KB Output is correct
82 Correct 9 ms 12064 KB Output is correct
83 Correct 8 ms 12108 KB Output is correct
84 Correct 10 ms 12364 KB Output is correct
85 Correct 155 ms 23808 KB Output is correct
86 Correct 9 ms 12336 KB Output is correct
87 Correct 177 ms 22828 KB Output is correct
88 Correct 9 ms 12052 KB Output is correct
89 Correct 113 ms 22304 KB Output is correct
90 Correct 149 ms 23748 KB Output is correct
91 Correct 153 ms 24604 KB Output is correct
92 Correct 74 ms 21292 KB Output is correct
93 Correct 9 ms 12468 KB Output is correct
94 Correct 8 ms 12048 KB Output is correct
95 Correct 94 ms 24660 KB Output is correct
96 Correct 235 ms 37684 KB Output is correct
97 Correct 9 ms 12464 KB Output is correct
98 Correct 7 ms 12136 KB Output is correct
99 Correct 87 ms 23880 KB Output is correct
100 Correct 9 ms 12364 KB Output is correct
101 Correct 153 ms 22768 KB Output is correct
102 Correct 176 ms 34292 KB Output is correct
103 Correct 13 ms 12444 KB Output is correct
104 Correct 14 ms 12656 KB Output is correct
105 Correct 12 ms 12332 KB Output is correct
106 Correct 13 ms 12404 KB Output is correct
107 Correct 10 ms 12364 KB Output is correct
108 Correct 574 ms 72004 KB Output is correct
109 Correct 921 ms 73572 KB Output is correct
110 Correct 1022 ms 84612 KB Output is correct
111 Correct 1270 ms 144572 KB Output is correct
112 Correct 1022 ms 92444 KB Output is correct
113 Correct 589 ms 76064 KB Output is correct
114 Correct 841 ms 57960 KB Output is correct
115 Correct 865 ms 57960 KB Output is correct
116 Correct 955 ms 66440 KB Output is correct
117 Correct 1028 ms 75816 KB Output is correct
118 Correct 920 ms 62124 KB Output is correct
119 Correct 1040 ms 75960 KB Output is correct
120 Correct 1161 ms 128028 KB Output is correct
121 Correct 944 ms 75860 KB Output is correct
122 Correct 965 ms 72732 KB Output is correct
123 Correct 635 ms 76832 KB Output is correct
124 Correct 1217 ms 72576 KB Output is correct