답안 #616356

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
616356 2022-08-01T04:27:45 Z CSQ31 Mergers (JOI19_mergers) C++17
100 / 100
1716 ms 212960 KB
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define sz(a) (int)(a.size())
#define all(a) a.begin(),a.end()
#define lb lower_bound
#define ub upper_bound
#define owo ios_base::sync_with_stdio(0);cin.tie(0);
#define MOD (ll)(998244353)
#define INF (ll)(1e18)
#define debug(...) fprintf(stderr, __VA_ARGS__),fflush(stderr)
#define time__(d) for(long blockTime = 0; (blockTime == 0 ? (blockTime=clock()) != 0 : false);\
debug("%s time : %.4fs\n", d, (double)(clock() - blockTime) / CLOCKS_PER_SEC))
typedef long long int ll;
typedef long double ld;
typedef pair<ll,ll> PII;
typedef pair<int,int> pii;
typedef vector<vector<int>> vii;
typedef vector<vector<ll>> VII;
const int MAXN = 5e5+5;
int tin[MAXN],tout[MAXN],dep[MAXN],par[21][MAXN],c[MAXN];
vector<int>adj[MAXN];
int timer = 0;
void dfs(int v,int u){
	tin[v] = ++timer;
	for(int x:adj[v]){
	    if(x == u)continue;	
	    par[0][x] = v;
	    dep[x] = dep[v]+1;
	    for(int i=1;i<=20;i++){
			par[i][x] = par[i-1][par[i-1][x]];
		}
		dfs(x,v);
	}
	tout[v] = timer;
}
bool ancestor(int v,int u){return(tin[v] >= tin[u] && tout[u] >= tout[v]);}//is u an ancestor of v?
int lca(int v,int u){
	if(dep[v] > dep[u])swap(u,v);
	int dist = dep[u]-dep[v];
	for(int i=0;dist;i++,dist>>=1){
		if(dist&1)u = par[i][u];
	}
	if(u == v)return u;
	for(int i=20;i>=0;i--){
		if(par[i][v] != par[i][u]){
			u = par[i][u];
			v = par[i][v];
		}
	}
	return par[0][v];
}
vector<int>color[MAXN];
int lc[MAXN],p[MAXN];
int find(int x){
	if(p[x] == x)return x;
	return p[x] = find(p[x]);
}
void unite(int a,int b){
	a = find(a);
	b = find(b);
	if(a==b)return;
	if(dep[a] < dep[b])swap(a,b);
	p[a] = b;
	if(dep[lc[a]] < dep[lc[b]])lc[b] = lc[a];	
}
void dfs2(int v,int u){
	int tar = find(v);
	tar = dep[lc[tar]];
	int cur = v;
	//cout<<"node"<<" "<<cur<<'\n';
	while(dep[cur] > tar){
		int pp = find(cur);
		if(dep[pp] <= tar)break;
		unite(cur,par[0][pp]);
		//cout<<cur<<" "<<pp<<'\n';
		cur = par[0][pp];
	}
	//cout<<'\n';
	for(int x:adj[v]){
		if(x==u)continue;
		dfs2(x,v);
		
	}
	
}
int main()
{
  owo
	int n,k;
	cin>>n>>k;
	for(int i=0;i<n-1;i++){
		int v,u;
		cin>>v>>u;
		v--;u--;
		adj[v].pb(u);
		adj[u].pb(v);
	}
	for(int i=0;i<n;i++){
		cin>>c[i];
		c[i]--;
		color[c[i]].pb(i);
		p[i] = i;
	}
	dfs(0,-1);
	for(int i=0;i<k;i++){
		int v = -1;
		for(int x:color[i]){
			if(v == -1)v = x;
			else v = lca(x,v);
		}
		for(int x:color[i])lc[x] = v;
		//cout<<i<<" "<<v<<'\n';
	}
	//for(int i=0;i<n;i++)cout<<lc[i]<<" ";
	//cout<<'\n';
	dfs2(0,-1);
	//for(int i=0;i<n;i++)cout<<find(i)<<" ";
	vector<set<int>>s(n);
	for(int i=0;i<n;i++){
		for(int x:adj[i]){
			if(find(x) != find(i)){
				s[find(x)].insert(find(i));
				s[find(i)].insert(find(x));
			}
			
		}
		
	}
	int ans = 0;
	for(int i=0;i<n;i++){
		if(find(i) == i && sz(s[i]) == 1)ans++;
	}
	cout<<(ans+1)/2;
	
	
}
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 24124 KB Output is correct
2 Correct 12 ms 23968 KB Output is correct
3 Correct 11 ms 23924 KB Output is correct
4 Correct 12 ms 24020 KB Output is correct
5 Correct 12 ms 23948 KB Output is correct
6 Correct 12 ms 23952 KB Output is correct
7 Correct 14 ms 23820 KB Output is correct
8 Correct 14 ms 24020 KB Output is correct
9 Correct 12 ms 23972 KB Output is correct
10 Correct 13 ms 24020 KB Output is correct
11 Correct 12 ms 23956 KB Output is correct
12 Correct 12 ms 24020 KB Output is correct
13 Correct 12 ms 23892 KB Output is correct
14 Correct 12 ms 24020 KB Output is correct
15 Correct 12 ms 24020 KB Output is correct
16 Correct 12 ms 23892 KB Output is correct
17 Correct 13 ms 23948 KB Output is correct
18 Correct 14 ms 23968 KB Output is correct
19 Correct 13 ms 24020 KB Output is correct
20 Correct 13 ms 23976 KB Output is correct
21 Correct 14 ms 23956 KB Output is correct
22 Correct 13 ms 24020 KB Output is correct
23 Correct 13 ms 24020 KB Output is correct
24 Correct 12 ms 24020 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 24124 KB Output is correct
2 Correct 12 ms 23968 KB Output is correct
3 Correct 11 ms 23924 KB Output is correct
4 Correct 12 ms 24020 KB Output is correct
5 Correct 12 ms 23948 KB Output is correct
6 Correct 12 ms 23952 KB Output is correct
7 Correct 14 ms 23820 KB Output is correct
8 Correct 14 ms 24020 KB Output is correct
9 Correct 12 ms 23972 KB Output is correct
10 Correct 13 ms 24020 KB Output is correct
11 Correct 12 ms 23956 KB Output is correct
12 Correct 12 ms 24020 KB Output is correct
13 Correct 12 ms 23892 KB Output is correct
14 Correct 12 ms 24020 KB Output is correct
15 Correct 12 ms 24020 KB Output is correct
16 Correct 12 ms 23892 KB Output is correct
17 Correct 13 ms 23948 KB Output is correct
18 Correct 14 ms 23968 KB Output is correct
19 Correct 13 ms 24020 KB Output is correct
20 Correct 13 ms 23976 KB Output is correct
21 Correct 14 ms 23956 KB Output is correct
22 Correct 13 ms 24020 KB Output is correct
23 Correct 13 ms 24020 KB Output is correct
24 Correct 12 ms 24020 KB Output is correct
25 Correct 13 ms 23964 KB Output is correct
26 Correct 15 ms 24788 KB Output is correct
27 Correct 15 ms 24660 KB Output is correct
28 Correct 15 ms 24920 KB Output is correct
29 Correct 19 ms 24972 KB Output is correct
30 Correct 15 ms 24616 KB Output is correct
31 Correct 13 ms 23984 KB Output is correct
32 Correct 18 ms 24916 KB Output is correct
33 Correct 12 ms 23952 KB Output is correct
34 Correct 13 ms 24628 KB Output is correct
35 Correct 14 ms 24992 KB Output is correct
36 Correct 13 ms 24580 KB Output is correct
37 Correct 14 ms 24592 KB Output is correct
38 Correct 12 ms 23956 KB Output is correct
39 Correct 15 ms 24684 KB Output is correct
40 Correct 15 ms 24564 KB Output is correct
41 Correct 18 ms 24652 KB Output is correct
42 Correct 17 ms 24660 KB Output is correct
43 Correct 14 ms 24868 KB Output is correct
44 Correct 12 ms 23996 KB Output is correct
45 Correct 14 ms 24788 KB Output is correct
46 Correct 18 ms 24664 KB Output is correct
47 Correct 13 ms 24020 KB Output is correct
48 Correct 14 ms 24584 KB Output is correct
49 Correct 15 ms 24988 KB Output is correct
50 Correct 18 ms 25052 KB Output is correct
51 Correct 15 ms 24660 KB Output is correct
52 Correct 14 ms 24608 KB Output is correct
53 Correct 14 ms 24660 KB Output is correct
54 Correct 13 ms 24556 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 24124 KB Output is correct
2 Correct 12 ms 23968 KB Output is correct
3 Correct 11 ms 23924 KB Output is correct
4 Correct 12 ms 24020 KB Output is correct
5 Correct 12 ms 23948 KB Output is correct
6 Correct 12 ms 23952 KB Output is correct
7 Correct 14 ms 23820 KB Output is correct
8 Correct 14 ms 24020 KB Output is correct
9 Correct 12 ms 23972 KB Output is correct
10 Correct 13 ms 24020 KB Output is correct
11 Correct 12 ms 23956 KB Output is correct
12 Correct 12 ms 24020 KB Output is correct
13 Correct 12 ms 23892 KB Output is correct
14 Correct 12 ms 24020 KB Output is correct
15 Correct 12 ms 24020 KB Output is correct
16 Correct 12 ms 23892 KB Output is correct
17 Correct 13 ms 23948 KB Output is correct
18 Correct 14 ms 23968 KB Output is correct
19 Correct 13 ms 24020 KB Output is correct
20 Correct 13 ms 23976 KB Output is correct
21 Correct 14 ms 23956 KB Output is correct
22 Correct 13 ms 24020 KB Output is correct
23 Correct 13 ms 24020 KB Output is correct
24 Correct 12 ms 24020 KB Output is correct
25 Correct 12 ms 24020 KB Output is correct
26 Correct 77 ms 44772 KB Output is correct
27 Correct 104 ms 44436 KB Output is correct
28 Correct 14 ms 24660 KB Output is correct
29 Correct 12 ms 24036 KB Output is correct
30 Correct 12 ms 24020 KB Output is correct
31 Correct 123 ms 44344 KB Output is correct
32 Correct 15 ms 24664 KB Output is correct
33 Correct 151 ms 53040 KB Output is correct
34 Correct 111 ms 44496 KB Output is correct
35 Correct 14 ms 24612 KB Output is correct
36 Correct 114 ms 45296 KB Output is correct
37 Correct 14 ms 24660 KB Output is correct
38 Correct 15 ms 24624 KB Output is correct
39 Correct 77 ms 44608 KB Output is correct
40 Correct 14 ms 24916 KB Output is correct
41 Correct 121 ms 44300 KB Output is correct
42 Correct 136 ms 47180 KB Output is correct
43 Correct 16 ms 23980 KB Output is correct
44 Correct 140 ms 53308 KB Output is correct
45 Correct 133 ms 48868 KB Output is correct
46 Correct 14 ms 24612 KB Output is correct
47 Correct 14 ms 24548 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 82 ms 44804 KB Output is correct
2 Correct 169 ms 57008 KB Output is correct
3 Correct 15 ms 24784 KB Output is correct
4 Correct 14 ms 24660 KB Output is correct
5 Correct 13 ms 24020 KB Output is correct
6 Correct 16 ms 24040 KB Output is correct
7 Correct 16 ms 24560 KB Output is correct
8 Correct 140 ms 46788 KB Output is correct
9 Correct 16 ms 24612 KB Output is correct
10 Correct 118 ms 44648 KB Output is correct
11 Correct 13 ms 23964 KB Output is correct
12 Correct 127 ms 44568 KB Output is correct
13 Correct 130 ms 47644 KB Output is correct
14 Correct 193 ms 56676 KB Output is correct
15 Correct 81 ms 44620 KB Output is correct
16 Correct 15 ms 24660 KB Output is correct
17 Correct 12 ms 24020 KB Output is correct
18 Correct 146 ms 54924 KB Output is correct
19 Correct 168 ms 61444 KB Output is correct
20 Correct 16 ms 24788 KB Output is correct
21 Correct 12 ms 24020 KB Output is correct
22 Correct 112 ms 47708 KB Output is correct
23 Correct 18 ms 24600 KB Output is correct
24 Correct 136 ms 45812 KB Output is correct
25 Correct 164 ms 59860 KB Output is correct
26 Correct 15 ms 24992 KB Output is correct
27 Correct 16 ms 25044 KB Output is correct
28 Correct 16 ms 24660 KB Output is correct
29 Correct 15 ms 24660 KB Output is correct
30 Correct 17 ms 24608 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 24124 KB Output is correct
2 Correct 12 ms 23968 KB Output is correct
3 Correct 11 ms 23924 KB Output is correct
4 Correct 12 ms 24020 KB Output is correct
5 Correct 12 ms 23948 KB Output is correct
6 Correct 12 ms 23952 KB Output is correct
7 Correct 14 ms 23820 KB Output is correct
8 Correct 14 ms 24020 KB Output is correct
9 Correct 12 ms 23972 KB Output is correct
10 Correct 13 ms 24020 KB Output is correct
11 Correct 12 ms 23956 KB Output is correct
12 Correct 12 ms 24020 KB Output is correct
13 Correct 12 ms 23892 KB Output is correct
14 Correct 12 ms 24020 KB Output is correct
15 Correct 12 ms 24020 KB Output is correct
16 Correct 12 ms 23892 KB Output is correct
17 Correct 13 ms 23948 KB Output is correct
18 Correct 14 ms 23968 KB Output is correct
19 Correct 13 ms 24020 KB Output is correct
20 Correct 13 ms 23976 KB Output is correct
21 Correct 14 ms 23956 KB Output is correct
22 Correct 13 ms 24020 KB Output is correct
23 Correct 13 ms 24020 KB Output is correct
24 Correct 12 ms 24020 KB Output is correct
25 Correct 13 ms 23964 KB Output is correct
26 Correct 15 ms 24788 KB Output is correct
27 Correct 15 ms 24660 KB Output is correct
28 Correct 15 ms 24920 KB Output is correct
29 Correct 19 ms 24972 KB Output is correct
30 Correct 15 ms 24616 KB Output is correct
31 Correct 13 ms 23984 KB Output is correct
32 Correct 18 ms 24916 KB Output is correct
33 Correct 12 ms 23952 KB Output is correct
34 Correct 13 ms 24628 KB Output is correct
35 Correct 14 ms 24992 KB Output is correct
36 Correct 13 ms 24580 KB Output is correct
37 Correct 14 ms 24592 KB Output is correct
38 Correct 12 ms 23956 KB Output is correct
39 Correct 15 ms 24684 KB Output is correct
40 Correct 15 ms 24564 KB Output is correct
41 Correct 18 ms 24652 KB Output is correct
42 Correct 17 ms 24660 KB Output is correct
43 Correct 14 ms 24868 KB Output is correct
44 Correct 12 ms 23996 KB Output is correct
45 Correct 14 ms 24788 KB Output is correct
46 Correct 18 ms 24664 KB Output is correct
47 Correct 13 ms 24020 KB Output is correct
48 Correct 14 ms 24584 KB Output is correct
49 Correct 15 ms 24988 KB Output is correct
50 Correct 18 ms 25052 KB Output is correct
51 Correct 15 ms 24660 KB Output is correct
52 Correct 14 ms 24608 KB Output is correct
53 Correct 14 ms 24660 KB Output is correct
54 Correct 13 ms 24556 KB Output is correct
55 Correct 12 ms 24020 KB Output is correct
56 Correct 77 ms 44772 KB Output is correct
57 Correct 104 ms 44436 KB Output is correct
58 Correct 14 ms 24660 KB Output is correct
59 Correct 12 ms 24036 KB Output is correct
60 Correct 12 ms 24020 KB Output is correct
61 Correct 123 ms 44344 KB Output is correct
62 Correct 15 ms 24664 KB Output is correct
63 Correct 151 ms 53040 KB Output is correct
64 Correct 111 ms 44496 KB Output is correct
65 Correct 14 ms 24612 KB Output is correct
66 Correct 114 ms 45296 KB Output is correct
67 Correct 14 ms 24660 KB Output is correct
68 Correct 15 ms 24624 KB Output is correct
69 Correct 77 ms 44608 KB Output is correct
70 Correct 14 ms 24916 KB Output is correct
71 Correct 121 ms 44300 KB Output is correct
72 Correct 136 ms 47180 KB Output is correct
73 Correct 16 ms 23980 KB Output is correct
74 Correct 140 ms 53308 KB Output is correct
75 Correct 133 ms 48868 KB Output is correct
76 Correct 14 ms 24612 KB Output is correct
77 Correct 14 ms 24548 KB Output is correct
78 Correct 82 ms 44804 KB Output is correct
79 Correct 169 ms 57008 KB Output is correct
80 Correct 15 ms 24784 KB Output is correct
81 Correct 14 ms 24660 KB Output is correct
82 Correct 13 ms 24020 KB Output is correct
83 Correct 16 ms 24040 KB Output is correct
84 Correct 16 ms 24560 KB Output is correct
85 Correct 140 ms 46788 KB Output is correct
86 Correct 16 ms 24612 KB Output is correct
87 Correct 118 ms 44648 KB Output is correct
88 Correct 13 ms 23964 KB Output is correct
89 Correct 127 ms 44568 KB Output is correct
90 Correct 130 ms 47644 KB Output is correct
91 Correct 193 ms 56676 KB Output is correct
92 Correct 81 ms 44620 KB Output is correct
93 Correct 15 ms 24660 KB Output is correct
94 Correct 12 ms 24020 KB Output is correct
95 Correct 146 ms 54924 KB Output is correct
96 Correct 168 ms 61444 KB Output is correct
97 Correct 16 ms 24788 KB Output is correct
98 Correct 12 ms 24020 KB Output is correct
99 Correct 112 ms 47708 KB Output is correct
100 Correct 18 ms 24600 KB Output is correct
101 Correct 136 ms 45812 KB Output is correct
102 Correct 164 ms 59860 KB Output is correct
103 Correct 15 ms 24992 KB Output is correct
104 Correct 16 ms 25044 KB Output is correct
105 Correct 16 ms 24660 KB Output is correct
106 Correct 15 ms 24660 KB Output is correct
107 Correct 17 ms 24608 KB Output is correct
108 Correct 825 ms 138268 KB Output is correct
109 Correct 1163 ms 147716 KB Output is correct
110 Correct 1095 ms 161172 KB Output is correct
111 Correct 1573 ms 212960 KB Output is correct
112 Correct 1716 ms 183012 KB Output is correct
113 Correct 1458 ms 179588 KB Output is correct
114 Correct 892 ms 125928 KB Output is correct
115 Correct 848 ms 125976 KB Output is correct
116 Correct 1135 ms 130660 KB Output is correct
117 Correct 1445 ms 188888 KB Output is correct
118 Correct 865 ms 126008 KB Output is correct
119 Correct 1235 ms 188768 KB Output is correct
120 Correct 1541 ms 205672 KB Output is correct
121 Correct 1216 ms 188740 KB Output is correct
122 Correct 1050 ms 143744 KB Output is correct
123 Correct 1298 ms 190428 KB Output is correct
124 Correct 1054 ms 169584 KB Output is correct