Submission #360018

# Submission time Handle Problem Language Result Execution time Memory
360018 2021-01-27T12:05:50 Z soroush Mergers (JOI19_mergers) C++14
100 / 100
2342 ms 141640 KB
/*
#pragma GCC optimize("O2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,sse,sse2,fma")
*/
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int , int> pii;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int maxn = 5e5 + 100;
const ll mod = 1e9+7;
const ld PI = acos((ld)-1);

#define pb push_back
#define endl '\n'
#define dokme(x) cout << x , exit(0)
#define migmig ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define ms(x , y) memset(x , y , sizeof x)
ll pw(ll a, ll b, ll md = mod){ll res = 1;while(b){if(b&1){res=(a*res)%md;}a=(a*a)%md;b>>=1;}return(res);}

int n , k , c[maxn];
vector < int > adj[maxn] , col[maxn];
int root[maxn];

const int lg = 20;

struct LCA{
	int n;
	int par[lg+3][maxn] , h[maxn];
	void dfs(int v  , int p , vector < int > *adj){
		for(int u : adj[v]) if(u ^ p)
			h[u] = h[v] + 1 , par[0][u] = v , dfs(u , v , adj);
	}
	void init(int N , vector < int > *adj , int root = 1){
		n = N;
		dfs(root , 0 , adj);
		h[0] = -1;
		for(int j = 1 ; j <= lg ; j ++)
			for(int i = 1 ; i <= n ; i ++)
				par[j][i] = par[j - 1][par[j - 1][i]];
	}
	int lca(int u , int v){
		if(h[u] < h[v])
			swap(u , v);
		for(int i = lg ; i >= 0 ; i --)
			if(h[par[i][u]] >= h[v])
				u = par[i][u];
		if(u == v)
			return(u);
		for(int i = lg ; i >= 0 ; i --)
			if(par[i][u] ^ par[i][v])
				u = par[i][u] , v = par[i][v];
		return(par[0][v]);
	}
	int dist(int u , int v){
		return(h[u] + h[v] - 2*h[lca(u , v)]);
	}
};

LCA lca;

int par[maxn];

int getpar(int v){
	return((!par[v]) ? v : par[v] = getpar(par[v]));
}

void merge(int v , int u){
	if((u = getpar(u)) == (v = getpar(v)))
		return;
	par[u] = v;
}

int a[maxn];

void dfs(int v , int par = 0){
	for(int u : adj[v])if(u ^ par)
		dfs(u , v) , a[v] += a[u];
	if(a[v]) merge(v , par);
}

int d[maxn];

int32_t main(){
	migmig;
	cin >> n >> k;
	for(int i = 1 ; i < n ; i ++){
		int u , v;
		cin >> u >> v;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	for(int i = 1 ; i <= n ; i ++)
		cin >> c[i] , col[c[i]].pb(i) , root[c[i]] = i;
	lca.init(n , adj);
	int ans = 0;
	for(int i = 1 ; i <= k ; i ++)
		for(int v : col[i])
			root[i] = lca.lca(root[i] , v);
	for(int i = 1 ; i <= k ; i ++)
		for(int v : col[i])
			a[v] ++ , a[root[i]] --;
	dfs(1);
	for(int i = 1 ; i <= n ; i ++){
		for(auto u : adj[i])
			if(getpar(i) ^ getpar(u))
				d[getpar(i)] ++ , d[getpar(u)] ++;
	}
	for(int i = 1 ; i <= n ; i ++)
		ans += (d[i] == 2);
	cout << (ans + 1) / 2;
	return(0);
}
# Verdict Execution time Memory Grader output
1 Correct 17 ms 24044 KB Output is correct
2 Correct 16 ms 24044 KB Output is correct
3 Correct 19 ms 24044 KB Output is correct
4 Correct 17 ms 24044 KB Output is correct
5 Correct 17 ms 24060 KB Output is correct
6 Correct 17 ms 24044 KB Output is correct
7 Correct 17 ms 24044 KB Output is correct
8 Correct 16 ms 24044 KB Output is correct
9 Correct 17 ms 24044 KB Output is correct
10 Correct 17 ms 24044 KB Output is correct
11 Correct 20 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 24172 KB Output is correct
15 Correct 18 ms 24044 KB Output is correct
16 Correct 17 ms 24044 KB Output is correct
17 Correct 17 ms 24044 KB Output is correct
18 Correct 17 ms 24044 KB Output is correct
19 Correct 16 ms 24044 KB Output is correct
20 Correct 15 ms 24044 KB Output is correct
21 Correct 18 ms 24044 KB Output is correct
22 Correct 17 ms 24044 KB Output is correct
23 Correct 17 ms 24044 KB Output is correct
24 Correct 17 ms 24044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 24044 KB Output is correct
2 Correct 16 ms 24044 KB Output is correct
3 Correct 19 ms 24044 KB Output is correct
4 Correct 17 ms 24044 KB Output is correct
5 Correct 17 ms 24060 KB Output is correct
6 Correct 17 ms 24044 KB Output is correct
7 Correct 17 ms 24044 KB Output is correct
8 Correct 16 ms 24044 KB Output is correct
9 Correct 17 ms 24044 KB Output is correct
10 Correct 17 ms 24044 KB Output is correct
11 Correct 20 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 24172 KB Output is correct
15 Correct 18 ms 24044 KB Output is correct
16 Correct 17 ms 24044 KB Output is correct
17 Correct 17 ms 24044 KB Output is correct
18 Correct 17 ms 24044 KB Output is correct
19 Correct 16 ms 24044 KB Output is correct
20 Correct 15 ms 24044 KB Output is correct
21 Correct 18 ms 24044 KB Output is correct
22 Correct 17 ms 24044 KB Output is correct
23 Correct 17 ms 24044 KB Output is correct
24 Correct 17 ms 24044 KB Output is correct
25 Correct 18 ms 24044 KB Output is correct
26 Correct 21 ms 24556 KB Output is correct
27 Correct 18 ms 24556 KB Output is correct
28 Correct 19 ms 24684 KB Output is correct
29 Correct 20 ms 24556 KB Output is correct
30 Correct 19 ms 24512 KB Output is correct
31 Correct 16 ms 24044 KB Output is correct
32 Correct 19 ms 24812 KB Output is correct
33 Correct 17 ms 24044 KB Output is correct
34 Correct 19 ms 24556 KB Output is correct
35 Correct 19 ms 24556 KB Output is correct
36 Correct 19 ms 24556 KB Output is correct
37 Correct 26 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 19 ms 24556 KB Output is correct
43 Correct 22 ms 24812 KB Output is correct
44 Correct 22 ms 24044 KB Output is correct
45 Correct 23 ms 24684 KB Output is correct
46 Correct 21 ms 24556 KB Output is correct
47 Correct 19 ms 24044 KB Output is correct
48 Correct 21 ms 24556 KB Output is correct
49 Correct 19 ms 24556 KB Output is correct
50 Correct 19 ms 24684 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 24684 KB Output is correct
54 Correct 19 ms 24556 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 24044 KB Output is correct
2 Correct 16 ms 24044 KB Output is correct
3 Correct 19 ms 24044 KB Output is correct
4 Correct 17 ms 24044 KB Output is correct
5 Correct 17 ms 24060 KB Output is correct
6 Correct 17 ms 24044 KB Output is correct
7 Correct 17 ms 24044 KB Output is correct
8 Correct 16 ms 24044 KB Output is correct
9 Correct 17 ms 24044 KB Output is correct
10 Correct 17 ms 24044 KB Output is correct
11 Correct 20 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 24172 KB Output is correct
15 Correct 18 ms 24044 KB Output is correct
16 Correct 17 ms 24044 KB Output is correct
17 Correct 17 ms 24044 KB Output is correct
18 Correct 17 ms 24044 KB Output is correct
19 Correct 16 ms 24044 KB Output is correct
20 Correct 15 ms 24044 KB Output is correct
21 Correct 18 ms 24044 KB Output is correct
22 Correct 17 ms 24044 KB Output is correct
23 Correct 17 ms 24044 KB Output is correct
24 Correct 17 ms 24044 KB Output is correct
25 Correct 19 ms 24044 KB Output is correct
26 Correct 133 ms 40164 KB Output is correct
27 Correct 167 ms 39020 KB Output is correct
28 Correct 19 ms 24556 KB Output is correct
29 Correct 17 ms 24044 KB Output is correct
30 Correct 17 ms 24044 KB Output is correct
31 Correct 163 ms 39148 KB Output is correct
32 Correct 19 ms 24556 KB Output is correct
33 Correct 214 ms 46188 KB Output is correct
34 Correct 194 ms 39040 KB Output is correct
35 Correct 20 ms 24448 KB Output is correct
36 Correct 188 ms 39916 KB Output is correct
37 Correct 19 ms 24556 KB Output is correct
38 Correct 19 ms 24556 KB Output is correct
39 Correct 155 ms 40548 KB Output is correct
40 Correct 19 ms 24684 KB Output is correct
41 Correct 125 ms 39012 KB Output is correct
42 Correct 174 ms 41424 KB Output is correct
43 Correct 18 ms 24044 KB Output is correct
44 Correct 185 ms 46328 KB Output is correct
45 Correct 200 ms 42860 KB Output is correct
46 Correct 18 ms 24556 KB Output is correct
47 Correct 19 ms 24556 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 164 ms 38756 KB Output is correct
2 Correct 239 ms 40792 KB Output is correct
3 Correct 19 ms 24556 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 24172 KB Output is correct
7 Correct 18 ms 24556 KB Output is correct
8 Correct 259 ms 41236 KB Output is correct
9 Correct 19 ms 24556 KB Output is correct
10 Correct 225 ms 39532 KB Output is correct
11 Correct 17 ms 24044 KB Output is correct
12 Correct 219 ms 39532 KB Output is correct
13 Correct 254 ms 40812 KB Output is correct
14 Correct 230 ms 42336 KB Output is correct
15 Correct 144 ms 40548 KB Output is correct
16 Correct 19 ms 24556 KB Output is correct
17 Correct 18 ms 24044 KB Output is correct
18 Correct 245 ms 42832 KB Output is correct
19 Correct 316 ms 46444 KB Output is correct
20 Correct 20 ms 24576 KB Output is correct
21 Correct 17 ms 24044 KB Output is correct
22 Correct 237 ms 43492 KB Output is correct
23 Correct 19 ms 24556 KB Output is correct
24 Correct 264 ms 40044 KB Output is correct
25 Correct 315 ms 44908 KB Output is correct
26 Correct 19 ms 24556 KB Output is correct
27 Correct 21 ms 24684 KB Output is correct
28 Correct 22 ms 24556 KB Output is correct
29 Correct 18 ms 24556 KB Output is correct
30 Correct 22 ms 24556 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 24044 KB Output is correct
2 Correct 16 ms 24044 KB Output is correct
3 Correct 19 ms 24044 KB Output is correct
4 Correct 17 ms 24044 KB Output is correct
5 Correct 17 ms 24060 KB Output is correct
6 Correct 17 ms 24044 KB Output is correct
7 Correct 17 ms 24044 KB Output is correct
8 Correct 16 ms 24044 KB Output is correct
9 Correct 17 ms 24044 KB Output is correct
10 Correct 17 ms 24044 KB Output is correct
11 Correct 20 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 24172 KB Output is correct
15 Correct 18 ms 24044 KB Output is correct
16 Correct 17 ms 24044 KB Output is correct
17 Correct 17 ms 24044 KB Output is correct
18 Correct 17 ms 24044 KB Output is correct
19 Correct 16 ms 24044 KB Output is correct
20 Correct 15 ms 24044 KB Output is correct
21 Correct 18 ms 24044 KB Output is correct
22 Correct 17 ms 24044 KB Output is correct
23 Correct 17 ms 24044 KB Output is correct
24 Correct 17 ms 24044 KB Output is correct
25 Correct 18 ms 24044 KB Output is correct
26 Correct 21 ms 24556 KB Output is correct
27 Correct 18 ms 24556 KB Output is correct
28 Correct 19 ms 24684 KB Output is correct
29 Correct 20 ms 24556 KB Output is correct
30 Correct 19 ms 24512 KB Output is correct
31 Correct 16 ms 24044 KB Output is correct
32 Correct 19 ms 24812 KB Output is correct
33 Correct 17 ms 24044 KB Output is correct
34 Correct 19 ms 24556 KB Output is correct
35 Correct 19 ms 24556 KB Output is correct
36 Correct 19 ms 24556 KB Output is correct
37 Correct 26 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 19 ms 24556 KB Output is correct
43 Correct 22 ms 24812 KB Output is correct
44 Correct 22 ms 24044 KB Output is correct
45 Correct 23 ms 24684 KB Output is correct
46 Correct 21 ms 24556 KB Output is correct
47 Correct 19 ms 24044 KB Output is correct
48 Correct 21 ms 24556 KB Output is correct
49 Correct 19 ms 24556 KB Output is correct
50 Correct 19 ms 24684 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 24684 KB Output is correct
54 Correct 19 ms 24556 KB Output is correct
55 Correct 19 ms 24044 KB Output is correct
56 Correct 133 ms 40164 KB Output is correct
57 Correct 167 ms 39020 KB Output is correct
58 Correct 19 ms 24556 KB Output is correct
59 Correct 17 ms 24044 KB Output is correct
60 Correct 17 ms 24044 KB Output is correct
61 Correct 163 ms 39148 KB Output is correct
62 Correct 19 ms 24556 KB Output is correct
63 Correct 214 ms 46188 KB Output is correct
64 Correct 194 ms 39040 KB Output is correct
65 Correct 20 ms 24448 KB Output is correct
66 Correct 188 ms 39916 KB Output is correct
67 Correct 19 ms 24556 KB Output is correct
68 Correct 19 ms 24556 KB Output is correct
69 Correct 155 ms 40548 KB Output is correct
70 Correct 19 ms 24684 KB Output is correct
71 Correct 125 ms 39012 KB Output is correct
72 Correct 174 ms 41424 KB Output is correct
73 Correct 18 ms 24044 KB Output is correct
74 Correct 185 ms 46328 KB Output is correct
75 Correct 200 ms 42860 KB Output is correct
76 Correct 18 ms 24556 KB Output is correct
77 Correct 19 ms 24556 KB Output is correct
78 Correct 164 ms 38756 KB Output is correct
79 Correct 239 ms 40792 KB Output is correct
80 Correct 19 ms 24556 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 24172 KB Output is correct
84 Correct 18 ms 24556 KB Output is correct
85 Correct 259 ms 41236 KB Output is correct
86 Correct 19 ms 24556 KB Output is correct
87 Correct 225 ms 39532 KB Output is correct
88 Correct 17 ms 24044 KB Output is correct
89 Correct 219 ms 39532 KB Output is correct
90 Correct 254 ms 40812 KB Output is correct
91 Correct 230 ms 42336 KB Output is correct
92 Correct 144 ms 40548 KB Output is correct
93 Correct 19 ms 24556 KB Output is correct
94 Correct 18 ms 24044 KB Output is correct
95 Correct 245 ms 42832 KB Output is correct
96 Correct 316 ms 46444 KB Output is correct
97 Correct 20 ms 24576 KB Output is correct
98 Correct 17 ms 24044 KB Output is correct
99 Correct 237 ms 43492 KB Output is correct
100 Correct 19 ms 24556 KB Output is correct
101 Correct 264 ms 40044 KB Output is correct
102 Correct 315 ms 44908 KB Output is correct
103 Correct 19 ms 24556 KB Output is correct
104 Correct 21 ms 24684 KB Output is correct
105 Correct 22 ms 24556 KB Output is correct
106 Correct 18 ms 24556 KB Output is correct
107 Correct 22 ms 24556 KB Output is correct
108 Correct 1612 ms 115648 KB Output is correct
109 Correct 1443 ms 116936 KB Output is correct
110 Correct 1418 ms 127884 KB Output is correct
111 Correct 2277 ms 141640 KB Output is correct
112 Correct 2342 ms 131880 KB Output is correct
113 Correct 1563 ms 119760 KB Output is correct
114 Correct 774 ms 98696 KB Output is correct
115 Correct 778 ms 98780 KB Output is correct
116 Correct 1866 ms 105452 KB Output is correct
117 Correct 1948 ms 116496 KB Output is correct
118 Correct 1447 ms 100788 KB Output is correct
119 Correct 1924 ms 116716 KB Output is correct
120 Correct 2302 ms 130668 KB Output is correct
121 Correct 1998 ms 118380 KB Output is correct
122 Correct 1884 ms 111428 KB Output is correct
123 Correct 1533 ms 118044 KB Output is correct
124 Correct 2102 ms 116716 KB Output is correct