답안 #1114246

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1114246 2024-11-18T12:24:32 Z Dan4Life Mergers (JOI19_mergers) C++17
100 / 100
905 ms 203084 KB
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) begin(a),end(a)
using ar2 = array<int,2>;
const int N = (int)5e5+10;
int n, k;
vector<int> adj[N];
int p[N], sz[N];
int a[N], tot[N], deg[N];
unordered_map<int,int> S[N];
 
int findSet(int i){return i==p[i]?i:p[i]=findSet(p[i]);}
void unionSet(int i, int j){
	int x = findSet(i), y = findSet(j);
	if(x==y) return;
	if(sz[x]<sz[y])swap(x,y);
	p[y] = x, sz[x]+=sz[y];
}
 
void dfs(int s, int p){
	S[s][a[s]]=1;
	if(S[s][a[s]]==tot[a[s]]) S[s].erase(S[s].find(a[s]));
	for(auto u : adj[s]){
		if(u==p) continue;
		dfs(u,s);
		if(sz(S[s]) < sz(S[u])) S[s].swap(S[u]);
		for(auto [v,w] : S[u]){
			S[s][v]+=w;
			if(S[s][v]==tot[v])S[s].erase(S[s].find(v));
		}
	}
	if(p and !!sz(S[s])) unionSet(s,p);
}
 
int32_t main(){
	ios_base::sync_with_stdio(false); cin.tie(0);
	cin >> n >> k;
	for(int i = 1; i <= n; i++) p[i]=i,sz[i]=1;
	for(int i = 1; i < n; i++){
		int a, b; cin >> a >> b;
		adj[a].pb(b); adj[b].pb(a);
	}
	for(int i = 1; i <= n; i++)
		cin >> a[i], tot[a[i]]++;
	dfs(1,0);
	for(int i = 1; i <= n; i++)
		for(int j : adj[i])
			if(findSet(i)!=findSet(j)) 
				deg[findSet(i)]++;
	int cnt = 1;
	for(int i = 1; i <= n; i++) cnt+=(deg[i]==1);
	cout << cnt/2 << "\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 49232 KB Output is correct
2 Correct 10 ms 49400 KB Output is correct
3 Correct 11 ms 49400 KB Output is correct
4 Correct 9 ms 49232 KB Output is correct
5 Correct 10 ms 49232 KB Output is correct
6 Correct 11 ms 49232 KB Output is correct
7 Correct 9 ms 45136 KB Output is correct
8 Correct 9 ms 45136 KB Output is correct
9 Correct 10 ms 45136 KB Output is correct
10 Correct 10 ms 47184 KB Output is correct
11 Correct 11 ms 47196 KB Output is correct
12 Correct 10 ms 49232 KB Output is correct
13 Correct 9 ms 47184 KB Output is correct
14 Correct 10 ms 47184 KB Output is correct
15 Correct 10 ms 49232 KB Output is correct
16 Correct 9 ms 45136 KB Output is correct
17 Correct 10 ms 47184 KB Output is correct
18 Correct 10 ms 49352 KB Output is correct
19 Correct 10 ms 47184 KB Output is correct
20 Correct 10 ms 47184 KB Output is correct
21 Correct 11 ms 49400 KB Output is correct
22 Correct 10 ms 43088 KB Output is correct
23 Correct 10 ms 39504 KB Output is correct
24 Correct 11 ms 41108 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 49232 KB Output is correct
2 Correct 10 ms 49400 KB Output is correct
3 Correct 11 ms 49400 KB Output is correct
4 Correct 9 ms 49232 KB Output is correct
5 Correct 10 ms 49232 KB Output is correct
6 Correct 11 ms 49232 KB Output is correct
7 Correct 9 ms 45136 KB Output is correct
8 Correct 9 ms 45136 KB Output is correct
9 Correct 10 ms 45136 KB Output is correct
10 Correct 10 ms 47184 KB Output is correct
11 Correct 11 ms 47196 KB Output is correct
12 Correct 10 ms 49232 KB Output is correct
13 Correct 9 ms 47184 KB Output is correct
14 Correct 10 ms 47184 KB Output is correct
15 Correct 10 ms 49232 KB Output is correct
16 Correct 9 ms 45136 KB Output is correct
17 Correct 10 ms 47184 KB Output is correct
18 Correct 10 ms 49352 KB Output is correct
19 Correct 10 ms 47184 KB Output is correct
20 Correct 10 ms 47184 KB Output is correct
21 Correct 11 ms 49400 KB Output is correct
22 Correct 10 ms 43088 KB Output is correct
23 Correct 10 ms 39504 KB Output is correct
24 Correct 11 ms 41108 KB Output is correct
25 Correct 10 ms 49404 KB Output is correct
26 Correct 11 ms 41552 KB Output is correct
27 Correct 14 ms 49744 KB Output is correct
28 Correct 12 ms 49744 KB Output is correct
29 Correct 12 ms 43600 KB Output is correct
30 Correct 13 ms 40272 KB Output is correct
31 Correct 9 ms 39504 KB Output is correct
32 Correct 12 ms 40272 KB Output is correct
33 Correct 11 ms 41040 KB Output is correct
34 Correct 12 ms 40272 KB Output is correct
35 Correct 14 ms 49744 KB Output is correct
36 Correct 11 ms 41552 KB Output is correct
37 Correct 12 ms 50016 KB Output is correct
38 Correct 15 ms 49232 KB Output is correct
39 Correct 14 ms 50016 KB Output is correct
40 Correct 11 ms 49828 KB Output is correct
41 Correct 12 ms 49744 KB Output is correct
42 Correct 13 ms 49744 KB Output is correct
43 Correct 13 ms 50256 KB Output is correct
44 Correct 10 ms 47184 KB Output is correct
45 Correct 11 ms 47696 KB Output is correct
46 Correct 14 ms 49744 KB Output is correct
47 Correct 10 ms 47496 KB Output is correct
48 Correct 12 ms 47836 KB Output is correct
49 Correct 13 ms 49744 KB Output is correct
50 Correct 13 ms 47760 KB Output is correct
51 Correct 11 ms 49744 KB Output is correct
52 Correct 12 ms 47696 KB Output is correct
53 Correct 13 ms 47696 KB Output is correct
54 Correct 14 ms 47964 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 49232 KB Output is correct
2 Correct 10 ms 49400 KB Output is correct
3 Correct 11 ms 49400 KB Output is correct
4 Correct 9 ms 49232 KB Output is correct
5 Correct 10 ms 49232 KB Output is correct
6 Correct 11 ms 49232 KB Output is correct
7 Correct 9 ms 45136 KB Output is correct
8 Correct 9 ms 45136 KB Output is correct
9 Correct 10 ms 45136 KB Output is correct
10 Correct 10 ms 47184 KB Output is correct
11 Correct 11 ms 47196 KB Output is correct
12 Correct 10 ms 49232 KB Output is correct
13 Correct 9 ms 47184 KB Output is correct
14 Correct 10 ms 47184 KB Output is correct
15 Correct 10 ms 49232 KB Output is correct
16 Correct 9 ms 45136 KB Output is correct
17 Correct 10 ms 47184 KB Output is correct
18 Correct 10 ms 49352 KB Output is correct
19 Correct 10 ms 47184 KB Output is correct
20 Correct 10 ms 47184 KB Output is correct
21 Correct 11 ms 49400 KB Output is correct
22 Correct 10 ms 43088 KB Output is correct
23 Correct 10 ms 39504 KB Output is correct
24 Correct 11 ms 41108 KB Output is correct
25 Correct 11 ms 47184 KB Output is correct
26 Correct 85 ms 64716 KB Output is correct
27 Correct 134 ms 69604 KB Output is correct
28 Correct 12 ms 47696 KB Output is correct
29 Correct 11 ms 45136 KB Output is correct
30 Correct 10 ms 47184 KB Output is correct
31 Correct 121 ms 66632 KB Output is correct
32 Correct 14 ms 49752 KB Output is correct
33 Correct 118 ms 76104 KB Output is correct
34 Correct 119 ms 70400 KB Output is correct
35 Correct 13 ms 49744 KB Output is correct
36 Correct 101 ms 65832 KB Output is correct
37 Correct 11 ms 49744 KB Output is correct
38 Correct 13 ms 49960 KB Output is correct
39 Correct 83 ms 64832 KB Output is correct
40 Correct 12 ms 50256 KB Output is correct
41 Correct 85 ms 64420 KB Output is correct
42 Correct 113 ms 70472 KB Output is correct
43 Correct 9 ms 49232 KB Output is correct
44 Correct 117 ms 76556 KB Output is correct
45 Correct 100 ms 71268 KB Output is correct
46 Correct 12 ms 49744 KB Output is correct
47 Correct 11 ms 45904 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 74 ms 64712 KB Output is correct
2 Correct 67 ms 63688 KB Output is correct
3 Correct 15 ms 49744 KB Output is correct
4 Correct 11 ms 49744 KB Output is correct
5 Correct 10 ms 49232 KB Output is correct
6 Correct 10 ms 49232 KB Output is correct
7 Correct 13 ms 49744 KB Output is correct
8 Correct 116 ms 68424 KB Output is correct
9 Correct 13 ms 49744 KB Output is correct
10 Correct 105 ms 66632 KB Output is correct
11 Correct 10 ms 47184 KB Output is correct
12 Correct 95 ms 66640 KB Output is correct
13 Correct 102 ms 66024 KB Output is correct
14 Correct 85 ms 61784 KB Output is correct
15 Correct 81 ms 66880 KB Output is correct
16 Correct 12 ms 45648 KB Output is correct
17 Correct 10 ms 49232 KB Output is correct
18 Correct 82 ms 64432 KB Output is correct
19 Correct 112 ms 66544 KB Output is correct
20 Correct 13 ms 49744 KB Output is correct
21 Correct 11 ms 49232 KB Output is correct
22 Correct 105 ms 68032 KB Output is correct
23 Correct 11 ms 47696 KB Output is correct
24 Correct 108 ms 67920 KB Output is correct
25 Correct 81 ms 69460 KB Output is correct
26 Correct 12 ms 49744 KB Output is correct
27 Correct 12 ms 49744 KB Output is correct
28 Correct 13 ms 49744 KB Output is correct
29 Correct 11 ms 45708 KB Output is correct
30 Correct 17 ms 49744 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 49232 KB Output is correct
2 Correct 10 ms 49400 KB Output is correct
3 Correct 11 ms 49400 KB Output is correct
4 Correct 9 ms 49232 KB Output is correct
5 Correct 10 ms 49232 KB Output is correct
6 Correct 11 ms 49232 KB Output is correct
7 Correct 9 ms 45136 KB Output is correct
8 Correct 9 ms 45136 KB Output is correct
9 Correct 10 ms 45136 KB Output is correct
10 Correct 10 ms 47184 KB Output is correct
11 Correct 11 ms 47196 KB Output is correct
12 Correct 10 ms 49232 KB Output is correct
13 Correct 9 ms 47184 KB Output is correct
14 Correct 10 ms 47184 KB Output is correct
15 Correct 10 ms 49232 KB Output is correct
16 Correct 9 ms 45136 KB Output is correct
17 Correct 10 ms 47184 KB Output is correct
18 Correct 10 ms 49352 KB Output is correct
19 Correct 10 ms 47184 KB Output is correct
20 Correct 10 ms 47184 KB Output is correct
21 Correct 11 ms 49400 KB Output is correct
22 Correct 10 ms 43088 KB Output is correct
23 Correct 10 ms 39504 KB Output is correct
24 Correct 11 ms 41108 KB Output is correct
25 Correct 10 ms 49404 KB Output is correct
26 Correct 11 ms 41552 KB Output is correct
27 Correct 14 ms 49744 KB Output is correct
28 Correct 12 ms 49744 KB Output is correct
29 Correct 12 ms 43600 KB Output is correct
30 Correct 13 ms 40272 KB Output is correct
31 Correct 9 ms 39504 KB Output is correct
32 Correct 12 ms 40272 KB Output is correct
33 Correct 11 ms 41040 KB Output is correct
34 Correct 12 ms 40272 KB Output is correct
35 Correct 14 ms 49744 KB Output is correct
36 Correct 11 ms 41552 KB Output is correct
37 Correct 12 ms 50016 KB Output is correct
38 Correct 15 ms 49232 KB Output is correct
39 Correct 14 ms 50016 KB Output is correct
40 Correct 11 ms 49828 KB Output is correct
41 Correct 12 ms 49744 KB Output is correct
42 Correct 13 ms 49744 KB Output is correct
43 Correct 13 ms 50256 KB Output is correct
44 Correct 10 ms 47184 KB Output is correct
45 Correct 11 ms 47696 KB Output is correct
46 Correct 14 ms 49744 KB Output is correct
47 Correct 10 ms 47496 KB Output is correct
48 Correct 12 ms 47836 KB Output is correct
49 Correct 13 ms 49744 KB Output is correct
50 Correct 13 ms 47760 KB Output is correct
51 Correct 11 ms 49744 KB Output is correct
52 Correct 12 ms 47696 KB Output is correct
53 Correct 13 ms 47696 KB Output is correct
54 Correct 14 ms 47964 KB Output is correct
55 Correct 11 ms 47184 KB Output is correct
56 Correct 85 ms 64716 KB Output is correct
57 Correct 134 ms 69604 KB Output is correct
58 Correct 12 ms 47696 KB Output is correct
59 Correct 11 ms 45136 KB Output is correct
60 Correct 10 ms 47184 KB Output is correct
61 Correct 121 ms 66632 KB Output is correct
62 Correct 14 ms 49752 KB Output is correct
63 Correct 118 ms 76104 KB Output is correct
64 Correct 119 ms 70400 KB Output is correct
65 Correct 13 ms 49744 KB Output is correct
66 Correct 101 ms 65832 KB Output is correct
67 Correct 11 ms 49744 KB Output is correct
68 Correct 13 ms 49960 KB Output is correct
69 Correct 83 ms 64832 KB Output is correct
70 Correct 12 ms 50256 KB Output is correct
71 Correct 85 ms 64420 KB Output is correct
72 Correct 113 ms 70472 KB Output is correct
73 Correct 9 ms 49232 KB Output is correct
74 Correct 117 ms 76556 KB Output is correct
75 Correct 100 ms 71268 KB Output is correct
76 Correct 12 ms 49744 KB Output is correct
77 Correct 11 ms 45904 KB Output is correct
78 Correct 74 ms 64712 KB Output is correct
79 Correct 67 ms 63688 KB Output is correct
80 Correct 15 ms 49744 KB Output is correct
81 Correct 11 ms 49744 KB Output is correct
82 Correct 10 ms 49232 KB Output is correct
83 Correct 10 ms 49232 KB Output is correct
84 Correct 13 ms 49744 KB Output is correct
85 Correct 116 ms 68424 KB Output is correct
86 Correct 13 ms 49744 KB Output is correct
87 Correct 105 ms 66632 KB Output is correct
88 Correct 10 ms 47184 KB Output is correct
89 Correct 95 ms 66640 KB Output is correct
90 Correct 102 ms 66024 KB Output is correct
91 Correct 85 ms 61784 KB Output is correct
92 Correct 81 ms 66880 KB Output is correct
93 Correct 12 ms 45648 KB Output is correct
94 Correct 10 ms 49232 KB Output is correct
95 Correct 82 ms 64432 KB Output is correct
96 Correct 112 ms 66544 KB Output is correct
97 Correct 13 ms 49744 KB Output is correct
98 Correct 11 ms 49232 KB Output is correct
99 Correct 105 ms 68032 KB Output is correct
100 Correct 11 ms 47696 KB Output is correct
101 Correct 108 ms 67920 KB Output is correct
102 Correct 81 ms 69460 KB Output is correct
103 Correct 12 ms 49744 KB Output is correct
104 Correct 12 ms 49744 KB Output is correct
105 Correct 13 ms 49744 KB Output is correct
106 Correct 11 ms 45708 KB Output is correct
107 Correct 17 ms 49744 KB Output is correct
108 Correct 655 ms 147380 KB Output is correct
109 Correct 761 ms 172556 KB Output is correct
110 Correct 748 ms 190724 KB Output is correct
111 Correct 643 ms 167352 KB Output is correct
112 Correct 706 ms 151748 KB Output is correct
113 Correct 448 ms 134968 KB Output is correct
114 Correct 557 ms 143688 KB Output is correct
115 Correct 591 ms 143436 KB Output is correct
116 Correct 841 ms 193096 KB Output is correct
117 Correct 524 ms 130460 KB Output is correct
118 Correct 572 ms 142408 KB Output is correct
119 Correct 536 ms 130216 KB Output is correct
120 Correct 635 ms 152912 KB Output is correct
121 Correct 532 ms 130248 KB Output is correct
122 Correct 905 ms 203084 KB Output is correct
123 Correct 388 ms 131768 KB Output is correct
124 Correct 524 ms 128712 KB Output is correct