답안 #337090

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
337090 2020-12-18T09:18:22 Z alishahali1382 Mergers (JOI19_mergers) C++14
100 / 100
1183 ms 134892 KB
#include <bits/stdc++.h>
#pragma GCC optimize ("O2,unroll-loops")
//#pragma GCC optimize("no-stack-protector,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef pair<ll, ll> pll;
#define debug(x) cerr<<#x<<'='<<(x)<<endl;
#define debugp(x) cerr<<#x<<"= {"<<(x.first)<<", "<<(x.second)<<"}"<<endl;
#define debug2(x, y) cerr<<"{"<<#x<<", "<<#y<<"} = {"<<(x)<<", "<<(y)<<"}"<<endl;
#define debugv(v) {cerr<<#v<<" : ";for (auto x:v) cerr<<x<<' ';cerr<<endl;}
#define all(x) x.begin(), x.end()
#define pb push_back
#define kill(x) return cout<<x<<'\n', 0;

const int inf=1000000010;
const ll INF=10000000000000010LL;
const int mod=1000000007;
const int MAXN=500010, LOG=20;

int n, m, k, u, v, x, y, t, a, b, ans;
int par[LOG][MAXN], mark[MAXN];
int stt[MAXN], fnt[MAXN], timer=1;
int S[MAXN], dsu[MAXN], deg[MAXN];
vector<int> G[MAXN];
vector<pii> vec[MAXN];

void dfs(int node){
	stt[node]=timer++;
	for (int v:G[node]) if (v!=par[0][node]){
		par[0][v]=node;
		dfs(v);
	}
	fnt[node]=timer;
}
int Lca(int x, int y){
	if (fnt[y]<=fnt[x]) return x;
	for (int i=LOG-1; ~i; i--) if (fnt[y]>fnt[par[i][x]]) x=par[i][x];
	return par[0][x];
}
int dfs2(int node){
	for (int v:G[node]) if (v!=par[0][node]) mark[node]+=dfs2(v);
	return ++mark[node];
}
int get(int x){ return (dsu[x]==x?x:dsu[x]=get(dsu[x]));}

int main(){
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	cin>>n>>k;
	for (int i=1; i<n; i++){
		cin>>u>>v;
		G[u].pb(v);
		G[v].pb(u);
	}
	par[0][1]=1;
	dfs(1);
	for (int i=1; i<LOG; i++) for (int j=1; j<=n; j++) par[i][j]=par[i-1][par[i-1][j]];
	for (int i=1; i<=n; i++) cin>>S[i], vec[S[i]].pb({stt[i], i});
	for (int i=1; i<=k; i++){
		sort(all(vec[i]));
		mark[Lca(vec[i][0].second, vec[i].back().second)]-=vec[i].size();
	}
	dfs2(1);
	iota(dsu+1, dsu+n+1, 1);
	for (int i=2; i<=n; i++) if (mark[i]) dsu[get(par[0][i])]=get(i);
	for (int i=2; i<=n; i++) if (!mark[i]){
		deg[get(par[0][i])]++;
		deg[get(i)]++;
	}
	for (int i=1; i<=n; i++) ans+=(deg[i]==1);
	debug(ans)
	cout<<(ans+1)/2<<"\n";
	
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 24044 KB Output is correct
2 Correct 17 ms 24044 KB Output is correct
3 Correct 17 ms 24064 KB Output is correct
4 Correct 17 ms 24064 KB Output is correct
5 Correct 17 ms 24044 KB Output is correct
6 Correct 17 ms 24044 KB Output is correct
7 Correct 17 ms 24044 KB Output is correct
8 Correct 17 ms 24044 KB Output is correct
9 Correct 17 ms 24044 KB Output is correct
10 Correct 18 ms 24044 KB Output is correct
11 Correct 17 ms 24044 KB Output is correct
12 Correct 17 ms 24044 KB Output is correct
13 Correct 17 ms 24044 KB Output is correct
14 Correct 17 ms 24064 KB Output is correct
15 Correct 17 ms 24056 KB Output is correct
16 Correct 17 ms 24052 KB Output is correct
17 Correct 17 ms 24044 KB Output is correct
18 Correct 17 ms 24044 KB Output is correct
19 Correct 17 ms 24172 KB Output is correct
20 Correct 16 ms 24044 KB Output is correct
21 Correct 17 ms 24044 KB Output is correct
22 Correct 17 ms 24044 KB Output is correct
23 Correct 17 ms 23964 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 24064 KB Output is correct
4 Correct 17 ms 24064 KB Output is correct
5 Correct 17 ms 24044 KB Output is correct
6 Correct 17 ms 24044 KB Output is correct
7 Correct 17 ms 24044 KB Output is correct
8 Correct 17 ms 24044 KB Output is correct
9 Correct 17 ms 24044 KB Output is correct
10 Correct 18 ms 24044 KB Output is correct
11 Correct 17 ms 24044 KB Output is correct
12 Correct 17 ms 24044 KB Output is correct
13 Correct 17 ms 24044 KB Output is correct
14 Correct 17 ms 24064 KB Output is correct
15 Correct 17 ms 24056 KB Output is correct
16 Correct 17 ms 24052 KB Output is correct
17 Correct 17 ms 24044 KB Output is correct
18 Correct 17 ms 24044 KB Output is correct
19 Correct 17 ms 24172 KB Output is correct
20 Correct 16 ms 24044 KB Output is correct
21 Correct 17 ms 24044 KB Output is correct
22 Correct 17 ms 24044 KB Output is correct
23 Correct 17 ms 23964 KB Output is correct
24 Correct 17 ms 24044 KB Output is correct
25 Correct 17 ms 24044 KB Output is correct
26 Correct 19 ms 24556 KB Output is correct
27 Correct 19 ms 24556 KB Output is correct
28 Correct 19 ms 24684 KB Output is correct
29 Correct 19 ms 24556 KB Output is correct
30 Correct 19 ms 24556 KB Output is correct
31 Correct 17 ms 24044 KB Output is correct
32 Correct 19 ms 24684 KB Output is correct
33 Correct 17 ms 24064 KB Output is correct
34 Correct 18 ms 24576 KB Output is correct
35 Correct 19 ms 24556 KB Output is correct
36 Correct 18 ms 24556 KB Output is correct
37 Correct 19 ms 24556 KB Output is correct
38 Correct 17 ms 24044 KB Output is correct
39 Correct 19 ms 24556 KB Output is correct
40 Correct 19 ms 24556 KB Output is correct
41 Correct 19 ms 24556 KB Output is correct
42 Correct 20 ms 24556 KB Output is correct
43 Correct 18 ms 24684 KB Output is correct
44 Correct 17 ms 24044 KB Output is correct
45 Correct 19 ms 24556 KB Output is correct
46 Correct 19 ms 24616 KB Output is correct
47 Correct 18 ms 24044 KB Output is correct
48 Correct 19 ms 24556 KB Output is correct
49 Correct 19 ms 24556 KB Output is correct
50 Correct 19 ms 24556 KB Output is correct
51 Correct 19 ms 24556 KB Output is correct
52 Correct 18 ms 24556 KB Output is correct
53 Correct 19 ms 24556 KB Output is correct
54 Correct 20 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 24064 KB Output is correct
4 Correct 17 ms 24064 KB Output is correct
5 Correct 17 ms 24044 KB Output is correct
6 Correct 17 ms 24044 KB Output is correct
7 Correct 17 ms 24044 KB Output is correct
8 Correct 17 ms 24044 KB Output is correct
9 Correct 17 ms 24044 KB Output is correct
10 Correct 18 ms 24044 KB Output is correct
11 Correct 17 ms 24044 KB Output is correct
12 Correct 17 ms 24044 KB Output is correct
13 Correct 17 ms 24044 KB Output is correct
14 Correct 17 ms 24064 KB Output is correct
15 Correct 17 ms 24056 KB Output is correct
16 Correct 17 ms 24052 KB Output is correct
17 Correct 17 ms 24044 KB Output is correct
18 Correct 17 ms 24044 KB Output is correct
19 Correct 17 ms 24172 KB Output is correct
20 Correct 16 ms 24044 KB Output is correct
21 Correct 17 ms 24044 KB Output is correct
22 Correct 17 ms 24044 KB Output is correct
23 Correct 17 ms 23964 KB Output is correct
24 Correct 17 ms 24044 KB Output is correct
25 Correct 17 ms 24044 KB Output is correct
26 Correct 83 ms 39908 KB Output is correct
27 Correct 115 ms 39532 KB Output is correct
28 Correct 19 ms 24556 KB Output is correct
29 Correct 17 ms 24044 KB Output is correct
30 Correct 18 ms 24044 KB Output is correct
31 Correct 112 ms 39532 KB Output is correct
32 Correct 19 ms 24556 KB Output is correct
33 Correct 129 ms 45164 KB Output is correct
34 Correct 113 ms 39532 KB Output is correct
35 Correct 23 ms 24684 KB Output is correct
36 Correct 126 ms 40172 KB Output is correct
37 Correct 19 ms 24556 KB Output is correct
38 Correct 19 ms 24556 KB Output is correct
39 Correct 82 ms 39780 KB Output is correct
40 Correct 19 ms 24684 KB Output is correct
41 Correct 117 ms 39532 KB Output is correct
42 Correct 128 ms 41324 KB Output is correct
43 Correct 17 ms 24044 KB Output is correct
44 Correct 131 ms 45280 KB Output is correct
45 Correct 121 ms 42476 KB Output is correct
46 Correct 18 ms 24556 KB Output is correct
47 Correct 19 ms 24556 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 78 ms 38372 KB Output is correct
2 Correct 94 ms 40804 KB Output is correct
3 Correct 19 ms 24428 KB Output is correct
4 Correct 19 ms 24556 KB Output is correct
5 Correct 17 ms 24044 KB Output is correct
6 Correct 17 ms 24044 KB Output is correct
7 Correct 18 ms 24556 KB Output is correct
8 Correct 183 ms 40940 KB Output is correct
9 Correct 19 ms 24556 KB Output is correct
10 Correct 115 ms 40172 KB Output is correct
11 Correct 17 ms 24044 KB Output is correct
12 Correct 126 ms 40172 KB Output is correct
13 Correct 158 ms 41068 KB Output is correct
14 Correct 128 ms 42348 KB Output is correct
15 Correct 85 ms 39780 KB Output is correct
16 Correct 19 ms 24556 KB Output is correct
17 Correct 17 ms 24044 KB Output is correct
18 Correct 119 ms 42340 KB Output is correct
19 Correct 149 ms 45420 KB Output is correct
20 Correct 19 ms 24556 KB Output is correct
21 Correct 18 ms 24172 KB Output is correct
22 Correct 152 ms 41188 KB Output is correct
23 Correct 19 ms 24556 KB Output is correct
24 Correct 127 ms 40684 KB Output is correct
25 Correct 137 ms 44396 KB Output is correct
26 Correct 18 ms 24556 KB Output is correct
27 Correct 19 ms 24556 KB Output is correct
28 Correct 19 ms 24556 KB Output is correct
29 Correct 18 ms 24556 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 24064 KB Output is correct
4 Correct 17 ms 24064 KB Output is correct
5 Correct 17 ms 24044 KB Output is correct
6 Correct 17 ms 24044 KB Output is correct
7 Correct 17 ms 24044 KB Output is correct
8 Correct 17 ms 24044 KB Output is correct
9 Correct 17 ms 24044 KB Output is correct
10 Correct 18 ms 24044 KB Output is correct
11 Correct 17 ms 24044 KB Output is correct
12 Correct 17 ms 24044 KB Output is correct
13 Correct 17 ms 24044 KB Output is correct
14 Correct 17 ms 24064 KB Output is correct
15 Correct 17 ms 24056 KB Output is correct
16 Correct 17 ms 24052 KB Output is correct
17 Correct 17 ms 24044 KB Output is correct
18 Correct 17 ms 24044 KB Output is correct
19 Correct 17 ms 24172 KB Output is correct
20 Correct 16 ms 24044 KB Output is correct
21 Correct 17 ms 24044 KB Output is correct
22 Correct 17 ms 24044 KB Output is correct
23 Correct 17 ms 23964 KB Output is correct
24 Correct 17 ms 24044 KB Output is correct
25 Correct 17 ms 24044 KB Output is correct
26 Correct 19 ms 24556 KB Output is correct
27 Correct 19 ms 24556 KB Output is correct
28 Correct 19 ms 24684 KB Output is correct
29 Correct 19 ms 24556 KB Output is correct
30 Correct 19 ms 24556 KB Output is correct
31 Correct 17 ms 24044 KB Output is correct
32 Correct 19 ms 24684 KB Output is correct
33 Correct 17 ms 24064 KB Output is correct
34 Correct 18 ms 24576 KB Output is correct
35 Correct 19 ms 24556 KB Output is correct
36 Correct 18 ms 24556 KB Output is correct
37 Correct 19 ms 24556 KB Output is correct
38 Correct 17 ms 24044 KB Output is correct
39 Correct 19 ms 24556 KB Output is correct
40 Correct 19 ms 24556 KB Output is correct
41 Correct 19 ms 24556 KB Output is correct
42 Correct 20 ms 24556 KB Output is correct
43 Correct 18 ms 24684 KB Output is correct
44 Correct 17 ms 24044 KB Output is correct
45 Correct 19 ms 24556 KB Output is correct
46 Correct 19 ms 24616 KB Output is correct
47 Correct 18 ms 24044 KB Output is correct
48 Correct 19 ms 24556 KB Output is correct
49 Correct 19 ms 24556 KB Output is correct
50 Correct 19 ms 24556 KB Output is correct
51 Correct 19 ms 24556 KB Output is correct
52 Correct 18 ms 24556 KB Output is correct
53 Correct 19 ms 24556 KB Output is correct
54 Correct 20 ms 24556 KB Output is correct
55 Correct 17 ms 24044 KB Output is correct
56 Correct 83 ms 39908 KB Output is correct
57 Correct 115 ms 39532 KB Output is correct
58 Correct 19 ms 24556 KB Output is correct
59 Correct 17 ms 24044 KB Output is correct
60 Correct 18 ms 24044 KB Output is correct
61 Correct 112 ms 39532 KB Output is correct
62 Correct 19 ms 24556 KB Output is correct
63 Correct 129 ms 45164 KB Output is correct
64 Correct 113 ms 39532 KB Output is correct
65 Correct 23 ms 24684 KB Output is correct
66 Correct 126 ms 40172 KB Output is correct
67 Correct 19 ms 24556 KB Output is correct
68 Correct 19 ms 24556 KB Output is correct
69 Correct 82 ms 39780 KB Output is correct
70 Correct 19 ms 24684 KB Output is correct
71 Correct 117 ms 39532 KB Output is correct
72 Correct 128 ms 41324 KB Output is correct
73 Correct 17 ms 24044 KB Output is correct
74 Correct 131 ms 45280 KB Output is correct
75 Correct 121 ms 42476 KB Output is correct
76 Correct 18 ms 24556 KB Output is correct
77 Correct 19 ms 24556 KB Output is correct
78 Correct 78 ms 38372 KB Output is correct
79 Correct 94 ms 40804 KB Output is correct
80 Correct 19 ms 24428 KB Output is correct
81 Correct 19 ms 24556 KB Output is correct
82 Correct 17 ms 24044 KB Output is correct
83 Correct 17 ms 24044 KB Output is correct
84 Correct 18 ms 24556 KB Output is correct
85 Correct 183 ms 40940 KB Output is correct
86 Correct 19 ms 24556 KB Output is correct
87 Correct 115 ms 40172 KB Output is correct
88 Correct 17 ms 24044 KB Output is correct
89 Correct 126 ms 40172 KB Output is correct
90 Correct 158 ms 41068 KB Output is correct
91 Correct 128 ms 42348 KB Output is correct
92 Correct 85 ms 39780 KB Output is correct
93 Correct 19 ms 24556 KB Output is correct
94 Correct 17 ms 24044 KB Output is correct
95 Correct 119 ms 42340 KB Output is correct
96 Correct 149 ms 45420 KB Output is correct
97 Correct 19 ms 24556 KB Output is correct
98 Correct 18 ms 24172 KB Output is correct
99 Correct 152 ms 41188 KB Output is correct
100 Correct 19 ms 24556 KB Output is correct
101 Correct 127 ms 40684 KB Output is correct
102 Correct 137 ms 44396 KB Output is correct
103 Correct 18 ms 24556 KB Output is correct
104 Correct 19 ms 24556 KB Output is correct
105 Correct 19 ms 24556 KB Output is correct
106 Correct 18 ms 24556 KB Output is correct
107 Correct 19 ms 24556 KB Output is correct
108 Correct 942 ms 111000 KB Output is correct
109 Correct 809 ms 115000 KB Output is correct
110 Correct 818 ms 123860 KB Output is correct
111 Correct 979 ms 134892 KB Output is correct
112 Correct 927 ms 127180 KB Output is correct
113 Correct 730 ms 116568 KB Output is correct
114 Correct 718 ms 100736 KB Output is correct
115 Correct 714 ms 100716 KB Output is correct
116 Correct 1055 ms 107884 KB Output is correct
117 Correct 859 ms 116716 KB Output is correct
118 Correct 691 ms 103276 KB Output is correct
119 Correct 876 ms 116844 KB Output is correct
120 Correct 915 ms 127724 KB Output is correct
121 Correct 857 ms 116716 KB Output is correct
122 Correct 1183 ms 110956 KB Output is correct
123 Correct 616 ms 118240 KB Output is correct
124 Correct 937 ms 114636 KB Output is correct