Submission #236814

# Submission time Handle Problem Language Result Execution time Memory
236814 2020-06-03T12:48:02 Z Knps4422 Mergers (JOI19_mergers) C++14
100 / 100
1701 ms 141864 KB
//#pragma optimization_level 3
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>ordset;
*/
#define fr first
#define sc second
#define vec vector
#define pb push_back
#define pii pair<int, int>
#define forn(x,y) for(int x = 1 ; x <= (int)y ; ++x)
#define all(x) (x).begin(),(x).end()
#define fast cin.tie(0);cout.tie(0);cin.sync_with_stdio(0);cout.sync_with_stdio(0);
 
using namespace std;
 
typedef long long ll;
typedef unsigned int uint;
typedef complex<int> point;
const int nmax = 500005;
const ll linf = 1e18;
const ll mod = 998244353;
const int inf = INT_MAX;
 
int n, k, tt, lvl[nmax];
vec < int > g[nmax];
int col[nmax];
int up[nmax][21];
int parent[2*nmax];
int siz[2*nmax];
int targ[nmax];
int tin[nmax],tout[nmax];
vec < int > cn[2*nmax];
int deg[2*nmax];
int find(int x){
	if(parent[x] == x)return x;
	return parent[x] = find(parent[x]);
}
void merge(int x , int y){
	x = find(x);
	y = find(y);
	if(x == y)return ;
	if(siz[x] < siz[y])swap(x,y);
	parent[y] = x;
	siz[x] += siz[y];
}
bool cmp(int x, int y){
	if(lvl[x] != lvl[y])
	return lvl[x] > lvl[y];
	return x < y;
}


void dfs(int x,int p)
{
	tin[x]=++tt;
    up[x][0]=p;
    lvl[x]=lvl[p]+1;
    for(int i=1;i<20;i++)up[x][i]=up[up[x][i-1]][i-1];
    for(int i=0;i<(int)g[x].size();i++)if(g[x][i]!=p)dfs(g[x][i],x);
	tout[x]=++tt;
}
bool anc(int x , int y){
	return tin[x] <= tin[y] && tout[x] >= tout[y];
}

int lca(int x,int y)
{
    if(anc(x,y))return x;
    if(anc(y,x))return y;
    for(int i=19;i>=0;i--)if(!anc(up[x][i],y))x=up[x][i];
    return up[x][0];
}

void rec(int nod){
	if(targ[nod] < lvl[nod]){
		merge(nod,up[nod][0]);
		targ[up[nod][0]] = min(targ[up[nod][0]],targ[nod]);
	}
}
int main(){
	cin >> n >> k;
	int a, b;
	forn(e,n-1){
		cin >> a >> b;
		g[a].pb(b);
		g[b].pb(a);
	}
	forn(i,n){
		cin >> col[i];
		cn[col[i]+n].pb(i);
		parent[i] = n + col[i];
	}
	forn(i,k){
		siz[n+i] = cn[n + i].size();
		parent[n+i] = n+i;
	}
	dfs(1,1);
	vec < int > vc;
	forn(i,n)vc.pb(i);
	sort(all(vc),cmp);
	forn(i,k){
		int t = cn[i+n][0];
		for(int j : cn[i+n]){
			t = lca(t,j);
		}
		for(int j : cn[n+i]){
			targ[j] = lvl[t];
		}
	}
	for(int i : vc)rec(i);
	forn(i,n){
		if(find(i) != find(up[i][0])){
			deg[find(i)]++;
			deg[find(up[i][0])]++;
		}
	}
	int lef = 0;
	forn(i,k){
		if(deg[i+n] == 1)lef++;
	}
	cout << (lef+1)/2 << '\n';;

}
# Verdict Execution time Memory Grader output
1 Correct 25 ms 35584 KB Output is correct
2 Correct 25 ms 35584 KB Output is correct
3 Correct 26 ms 35584 KB Output is correct
4 Correct 25 ms 35584 KB Output is correct
5 Correct 24 ms 35584 KB Output is correct
6 Correct 25 ms 35712 KB Output is correct
7 Correct 27 ms 35584 KB Output is correct
8 Correct 25 ms 35584 KB Output is correct
9 Correct 24 ms 35584 KB Output is correct
10 Correct 28 ms 35584 KB Output is correct
11 Correct 25 ms 35584 KB Output is correct
12 Correct 25 ms 35584 KB Output is correct
13 Correct 24 ms 35584 KB Output is correct
14 Correct 24 ms 35584 KB Output is correct
15 Correct 27 ms 35712 KB Output is correct
16 Correct 25 ms 35584 KB Output is correct
17 Correct 26 ms 35584 KB Output is correct
18 Correct 25 ms 35584 KB Output is correct
19 Correct 25 ms 35584 KB Output is correct
20 Correct 25 ms 35584 KB Output is correct
21 Correct 26 ms 35584 KB Output is correct
22 Correct 26 ms 35584 KB Output is correct
23 Correct 25 ms 35584 KB Output is correct
24 Correct 25 ms 35584 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 35584 KB Output is correct
2 Correct 25 ms 35584 KB Output is correct
3 Correct 26 ms 35584 KB Output is correct
4 Correct 25 ms 35584 KB Output is correct
5 Correct 24 ms 35584 KB Output is correct
6 Correct 25 ms 35712 KB Output is correct
7 Correct 27 ms 35584 KB Output is correct
8 Correct 25 ms 35584 KB Output is correct
9 Correct 24 ms 35584 KB Output is correct
10 Correct 28 ms 35584 KB Output is correct
11 Correct 25 ms 35584 KB Output is correct
12 Correct 25 ms 35584 KB Output is correct
13 Correct 24 ms 35584 KB Output is correct
14 Correct 24 ms 35584 KB Output is correct
15 Correct 27 ms 35712 KB Output is correct
16 Correct 25 ms 35584 KB Output is correct
17 Correct 26 ms 35584 KB Output is correct
18 Correct 25 ms 35584 KB Output is correct
19 Correct 25 ms 35584 KB Output is correct
20 Correct 25 ms 35584 KB Output is correct
21 Correct 26 ms 35584 KB Output is correct
22 Correct 26 ms 35584 KB Output is correct
23 Correct 25 ms 35584 KB Output is correct
24 Correct 25 ms 35584 KB Output is correct
25 Correct 27 ms 35712 KB Output is correct
26 Correct 29 ms 36096 KB Output is correct
27 Correct 30 ms 36224 KB Output is correct
28 Correct 30 ms 36224 KB Output is correct
29 Correct 29 ms 36224 KB Output is correct
30 Correct 29 ms 36096 KB Output is correct
31 Correct 25 ms 35584 KB Output is correct
32 Correct 29 ms 36352 KB Output is correct
33 Correct 25 ms 35712 KB Output is correct
34 Correct 30 ms 36096 KB Output is correct
35 Correct 29 ms 36224 KB Output is correct
36 Correct 29 ms 36096 KB Output is correct
37 Correct 30 ms 36096 KB Output is correct
38 Correct 25 ms 35584 KB Output is correct
39 Correct 30 ms 36096 KB Output is correct
40 Correct 29 ms 36096 KB Output is correct
41 Correct 29 ms 36096 KB Output is correct
42 Correct 31 ms 36096 KB Output is correct
43 Correct 30 ms 36224 KB Output is correct
44 Correct 25 ms 35584 KB Output is correct
45 Correct 29 ms 36224 KB Output is correct
46 Correct 31 ms 36096 KB Output is correct
47 Correct 25 ms 35584 KB Output is correct
48 Correct 29 ms 36096 KB Output is correct
49 Correct 29 ms 36224 KB Output is correct
50 Correct 29 ms 36224 KB Output is correct
51 Correct 29 ms 36096 KB Output is correct
52 Correct 29 ms 36096 KB Output is correct
53 Correct 29 ms 36096 KB Output is correct
54 Correct 29 ms 36144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 35584 KB Output is correct
2 Correct 25 ms 35584 KB Output is correct
3 Correct 26 ms 35584 KB Output is correct
4 Correct 25 ms 35584 KB Output is correct
5 Correct 24 ms 35584 KB Output is correct
6 Correct 25 ms 35712 KB Output is correct
7 Correct 27 ms 35584 KB Output is correct
8 Correct 25 ms 35584 KB Output is correct
9 Correct 24 ms 35584 KB Output is correct
10 Correct 28 ms 35584 KB Output is correct
11 Correct 25 ms 35584 KB Output is correct
12 Correct 25 ms 35584 KB Output is correct
13 Correct 24 ms 35584 KB Output is correct
14 Correct 24 ms 35584 KB Output is correct
15 Correct 27 ms 35712 KB Output is correct
16 Correct 25 ms 35584 KB Output is correct
17 Correct 26 ms 35584 KB Output is correct
18 Correct 25 ms 35584 KB Output is correct
19 Correct 25 ms 35584 KB Output is correct
20 Correct 25 ms 35584 KB Output is correct
21 Correct 26 ms 35584 KB Output is correct
22 Correct 26 ms 35584 KB Output is correct
23 Correct 25 ms 35584 KB Output is correct
24 Correct 25 ms 35584 KB Output is correct
25 Correct 26 ms 35584 KB Output is correct
26 Correct 182 ms 50948 KB Output is correct
27 Correct 223 ms 50416 KB Output is correct
28 Correct 29 ms 36096 KB Output is correct
29 Correct 25 ms 35584 KB Output is correct
30 Correct 25 ms 35584 KB Output is correct
31 Correct 224 ms 50504 KB Output is correct
32 Correct 28 ms 36088 KB Output is correct
33 Correct 240 ms 54644 KB Output is correct
34 Correct 222 ms 50708 KB Output is correct
35 Correct 29 ms 36096 KB Output is correct
36 Correct 235 ms 50684 KB Output is correct
37 Correct 29 ms 36084 KB Output is correct
38 Correct 29 ms 36096 KB Output is correct
39 Correct 185 ms 50684 KB Output is correct
40 Correct 29 ms 36224 KB Output is correct
41 Correct 219 ms 50284 KB Output is correct
42 Correct 233 ms 51804 KB Output is correct
43 Correct 25 ms 35584 KB Output is correct
44 Correct 240 ms 54772 KB Output is correct
45 Correct 252 ms 52740 KB Output is correct
46 Correct 29 ms 36096 KB Output is correct
47 Correct 31 ms 36096 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 177 ms 50816 KB Output is correct
2 Correct 223 ms 54488 KB Output is correct
3 Correct 29 ms 36224 KB Output is correct
4 Correct 29 ms 36096 KB Output is correct
5 Correct 27 ms 35584 KB Output is correct
6 Correct 25 ms 35584 KB Output is correct
7 Correct 29 ms 36096 KB Output is correct
8 Correct 265 ms 52080 KB Output is correct
9 Correct 31 ms 36224 KB Output is correct
10 Correct 239 ms 50552 KB Output is correct
11 Correct 25 ms 35584 KB Output is correct
12 Correct 235 ms 50416 KB Output is correct
13 Correct 255 ms 52084 KB Output is correct
14 Correct 243 ms 54256 KB Output is correct
15 Correct 179 ms 50688 KB Output is correct
16 Correct 29 ms 36096 KB Output is correct
17 Correct 27 ms 35712 KB Output is correct
18 Correct 211 ms 54252 KB Output is correct
19 Correct 274 ms 56688 KB Output is correct
20 Correct 29 ms 36224 KB Output is correct
21 Correct 27 ms 35584 KB Output is correct
22 Correct 229 ms 52452 KB Output is correct
23 Correct 29 ms 36096 KB Output is correct
24 Correct 235 ms 50928 KB Output is correct
25 Correct 258 ms 55792 KB Output is correct
26 Correct 29 ms 36224 KB Output is correct
27 Correct 30 ms 36224 KB Output is correct
28 Correct 29 ms 36088 KB Output is correct
29 Correct 29 ms 36096 KB Output is correct
30 Correct 29 ms 36224 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 35584 KB Output is correct
2 Correct 25 ms 35584 KB Output is correct
3 Correct 26 ms 35584 KB Output is correct
4 Correct 25 ms 35584 KB Output is correct
5 Correct 24 ms 35584 KB Output is correct
6 Correct 25 ms 35712 KB Output is correct
7 Correct 27 ms 35584 KB Output is correct
8 Correct 25 ms 35584 KB Output is correct
9 Correct 24 ms 35584 KB Output is correct
10 Correct 28 ms 35584 KB Output is correct
11 Correct 25 ms 35584 KB Output is correct
12 Correct 25 ms 35584 KB Output is correct
13 Correct 24 ms 35584 KB Output is correct
14 Correct 24 ms 35584 KB Output is correct
15 Correct 27 ms 35712 KB Output is correct
16 Correct 25 ms 35584 KB Output is correct
17 Correct 26 ms 35584 KB Output is correct
18 Correct 25 ms 35584 KB Output is correct
19 Correct 25 ms 35584 KB Output is correct
20 Correct 25 ms 35584 KB Output is correct
21 Correct 26 ms 35584 KB Output is correct
22 Correct 26 ms 35584 KB Output is correct
23 Correct 25 ms 35584 KB Output is correct
24 Correct 25 ms 35584 KB Output is correct
25 Correct 27 ms 35712 KB Output is correct
26 Correct 29 ms 36096 KB Output is correct
27 Correct 30 ms 36224 KB Output is correct
28 Correct 30 ms 36224 KB Output is correct
29 Correct 29 ms 36224 KB Output is correct
30 Correct 29 ms 36096 KB Output is correct
31 Correct 25 ms 35584 KB Output is correct
32 Correct 29 ms 36352 KB Output is correct
33 Correct 25 ms 35712 KB Output is correct
34 Correct 30 ms 36096 KB Output is correct
35 Correct 29 ms 36224 KB Output is correct
36 Correct 29 ms 36096 KB Output is correct
37 Correct 30 ms 36096 KB Output is correct
38 Correct 25 ms 35584 KB Output is correct
39 Correct 30 ms 36096 KB Output is correct
40 Correct 29 ms 36096 KB Output is correct
41 Correct 29 ms 36096 KB Output is correct
42 Correct 31 ms 36096 KB Output is correct
43 Correct 30 ms 36224 KB Output is correct
44 Correct 25 ms 35584 KB Output is correct
45 Correct 29 ms 36224 KB Output is correct
46 Correct 31 ms 36096 KB Output is correct
47 Correct 25 ms 35584 KB Output is correct
48 Correct 29 ms 36096 KB Output is correct
49 Correct 29 ms 36224 KB Output is correct
50 Correct 29 ms 36224 KB Output is correct
51 Correct 29 ms 36096 KB Output is correct
52 Correct 29 ms 36096 KB Output is correct
53 Correct 29 ms 36096 KB Output is correct
54 Correct 29 ms 36144 KB Output is correct
55 Correct 26 ms 35584 KB Output is correct
56 Correct 182 ms 50948 KB Output is correct
57 Correct 223 ms 50416 KB Output is correct
58 Correct 29 ms 36096 KB Output is correct
59 Correct 25 ms 35584 KB Output is correct
60 Correct 25 ms 35584 KB Output is correct
61 Correct 224 ms 50504 KB Output is correct
62 Correct 28 ms 36088 KB Output is correct
63 Correct 240 ms 54644 KB Output is correct
64 Correct 222 ms 50708 KB Output is correct
65 Correct 29 ms 36096 KB Output is correct
66 Correct 235 ms 50684 KB Output is correct
67 Correct 29 ms 36084 KB Output is correct
68 Correct 29 ms 36096 KB Output is correct
69 Correct 185 ms 50684 KB Output is correct
70 Correct 29 ms 36224 KB Output is correct
71 Correct 219 ms 50284 KB Output is correct
72 Correct 233 ms 51804 KB Output is correct
73 Correct 25 ms 35584 KB Output is correct
74 Correct 240 ms 54772 KB Output is correct
75 Correct 252 ms 52740 KB Output is correct
76 Correct 29 ms 36096 KB Output is correct
77 Correct 31 ms 36096 KB Output is correct
78 Correct 177 ms 50816 KB Output is correct
79 Correct 223 ms 54488 KB Output is correct
80 Correct 29 ms 36224 KB Output is correct
81 Correct 29 ms 36096 KB Output is correct
82 Correct 27 ms 35584 KB Output is correct
83 Correct 25 ms 35584 KB Output is correct
84 Correct 29 ms 36096 KB Output is correct
85 Correct 265 ms 52080 KB Output is correct
86 Correct 31 ms 36224 KB Output is correct
87 Correct 239 ms 50552 KB Output is correct
88 Correct 25 ms 35584 KB Output is correct
89 Correct 235 ms 50416 KB Output is correct
90 Correct 255 ms 52084 KB Output is correct
91 Correct 243 ms 54256 KB Output is correct
92 Correct 179 ms 50688 KB Output is correct
93 Correct 29 ms 36096 KB Output is correct
94 Correct 27 ms 35712 KB Output is correct
95 Correct 211 ms 54252 KB Output is correct
96 Correct 274 ms 56688 KB Output is correct
97 Correct 29 ms 36224 KB Output is correct
98 Correct 27 ms 35584 KB Output is correct
99 Correct 229 ms 52452 KB Output is correct
100 Correct 29 ms 36096 KB Output is correct
101 Correct 235 ms 50928 KB Output is correct
102 Correct 258 ms 55792 KB Output is correct
103 Correct 29 ms 36224 KB Output is correct
104 Correct 30 ms 36224 KB Output is correct
105 Correct 29 ms 36088 KB Output is correct
106 Correct 29 ms 36096 KB Output is correct
107 Correct 29 ms 36224 KB Output is correct
108 Correct 1223 ms 117212 KB Output is correct
109 Correct 1419 ms 119272 KB Output is correct
110 Correct 1361 ms 126048 KB Output is correct
111 Correct 1635 ms 141864 KB Output is correct
112 Correct 1701 ms 136136 KB Output is correct
113 Correct 1264 ms 127568 KB Output is correct
114 Correct 1186 ms 108768 KB Output is correct
115 Correct 1198 ms 108900 KB Output is correct
116 Correct 1454 ms 112816 KB Output is correct
117 Correct 1474 ms 128496 KB Output is correct
118 Correct 1338 ms 108620 KB Output is correct
119 Correct 1451 ms 128232 KB Output is correct
120 Correct 1628 ms 136672 KB Output is correct
121 Correct 1456 ms 128328 KB Output is correct
122 Correct 1518 ms 119756 KB Output is correct
123 Correct 1270 ms 129728 KB Output is correct
124 Correct 1581 ms 126060 KB Output is correct