Submission #706864

# Submission time Handle Problem Language Result Execution time Memory
706864 2023-03-07T23:20:57 Z Antekb Mergers (JOI19_mergers) C++14
100 / 100
1163 ms 157044 KB
#include<bits/stdc++.h>
 
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("trapv")
 
#define st first
#define nd second
#define pb push_back
#define eb emplace_back
#define pp(x) pop_back(x)
#define mp(a, b) make_pair(a, b)
#define all(x) (x).begin(), (x).end()
#define rev(x) reverse(all(x))
#define sor(x) sort(all(x))
#define sz(x) (int)(x).size()
#define rsz(x) resize(x)
 
using namespace std;
 
///~~~~~~~~~~~~~~~~~~~~~~~~~~
 
template <typename H, typename T> 
ostream& operator<<(ostream& os, pair<H, T> m){
	return os <<"("<< m.st<<", "<<m.nd<<")";
}
template <typename H> 
ostream& operator<<(ostream& os, vector<H> V){
	os<<"{";
	for(int i=0; i<V.size(); i++){
		if(i)os<<" ";
		os<<V[i];
	}
	os<<"}";
	return os;
}
 
void debug(){cerr<<"\n";}
template <typename H, typename... T>
void debug(H h, T... t) {cerr<<h; if (sizeof...(t)) cerr << ", "; debug(t...);}
#define deb(x...) cerr<<#x<<" = ";debug(x);
//#define deb(x...) ;
 
///~~~~~~~~~~~~~~~~~~~~~~~~~
 
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<pii > vii;
typedef vector<ll> vl;
typedef vector<pll> vll;
typedef string str;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

 
#define BOOST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
 
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
 
const int K=19, N=1<<K, INF=1e9+5, mod2=1e9+7, mod=998244353;

struct modint{
	int n=0;
	modint(){}
	modint(ll x){
		n=x%mod;
		if(n<0)n+=mod;
	}
	operator int(){
		return n;
	}
	modint operator+(modint a){a.n = n+a.n; if(a.n>=mod)a.n-=mod;return a;}
	modint operator+=(modint a){return (*this)=(*this)+a;}
	modint operator-(modint a){a.n = n-a.n; if(a.n<0)a.n+=mod;return a;}
	modint operator-=(modint a){return (*this)=(*this)-a;}
	modint operator*(modint a){a.n = (n*1ll*a.n)%mod; return a;}
	modint operator*=(modint a){return (*this)=(*this)*a;}
	modint operator^(const ll &m)const{
		modint a(1);
		if(m==0)return a;
		if(m==1)return (*this);
		a=(*this)^(m/2);
		a*=a;
		return a*((*this)^(m&1));
	}
	modint odw(){
		return (*this)^((ll)mod-2);
	}
	modint operator/(modint a){return (*this)*a.odw();}
	modint operator/=(modint a){return (*this)=(*this)/a;}
	bool operator==(modint a){return a.n==n;}
	friend ostream& operator<<(ostream& os, modint m) {
		return os << m.n; 
	}
};
modint fact[N], fact2[N];
typedef vector<modint> vm;
void factinit(){
    fact[0]=1;
    for(int i=1; i<N; i++){
        fact[i]=(fact[i-1]*modint(i))%mod;
    }
    fact2[N-1]=fact[N-1].odw();
    for(int i=N-2; i>=0; i--){
    	fact2[i]=(fact2[i+1]*modint(i+1))%mod;
    }
}
modint npok(int _n, int _k){
    return fact[_n]*fact2[_k]*fact2[_n-_k];
}
vi E[N], E2[N], kto[N];
int r[N], col[N], par[K][N], d[N];
int find(int v){
	if(r[v]==v)return v;
	return r[v]=find(r[v]);
}
void Union(int a, int b){
	a=find(a);
	b=find(b);
	r[a]=b;
}
void dfs(int v){
	for(int i:E[v]){
		if(i!=par[0][v]){
			d[i]=d[v]+1;
			par[0][i]=v;
			dfs(i);
		}
	}
}
int lca(int u, int v){
	if(u==v)return u;
	if(d[u]>d[v])swap(u, v);
	for(int k=K-1; k>=0; k--){
		if(d[u]+(1<<k)<=d[v])v=par[k][v];
	}
	if(u==v)return v;
	for(int k=K-1; k>=0; k--){
		if(par[k][u]!=par[k][v]){
			u=par[k][u];
			v=par[k][v];
		}
	}
	return par[0][v];
}
int main(){
	//factinit();
	//BOOST;
	int n, k;
	cin>>n>>k;
	for(int i=1; i<n; i++){
		int u, v;
		cin>>u>>v;
		E[u].pb(v);
		E[v].pb(u);
	}
	for(int i=1; i<=n; i++){
		int c;
		cin>>c;
		kto[c].pb(i);
		r[i]=i;
	}
	d[1]=1;
	dfs(1);
	for(int k=1; k<K; k++){
		for(int i=1; i<=n; i++){
			par[k][i]=par[k-1][par[k-1][i]];
		}
	}
	for(int i=1; i<=k; i++){
		int v=kto[i][0];
		for(int j:kto[i]){
			v=lca(v, j);
		}
		for(int j:kto[i]){
			while(find(j)!=find(v)){
				Union(j, par[0][j]);
				j=find(j);
			}
		}
	}
	for(int i=2; i<=n; i++){
		if(find(i)!=find(par[0][i])){
			E2[find(i)].pb(find(par[0][i]));
			E2[find(par[0][i])].pb(find(i));
		}
	}
	int l=0;
	for(int i=1; i<=n; i++){
		if(E2[i].size()){
			E2[i].resize(unique(all(E2[i]))-E2[i].begin());
			if(E2[i].size()==1)l++;
		}
	}
	cout<<(l+1)/2;
}
# Verdict Execution time Memory Grader output
1 Correct 20 ms 41428 KB Output is correct
2 Correct 20 ms 41520 KB Output is correct
3 Correct 22 ms 41484 KB Output is correct
4 Correct 21 ms 41480 KB Output is correct
5 Correct 21 ms 41484 KB Output is correct
6 Correct 20 ms 41476 KB Output is correct
7 Correct 21 ms 41432 KB Output is correct
8 Correct 20 ms 41428 KB Output is correct
9 Correct 20 ms 41428 KB Output is correct
10 Correct 22 ms 41428 KB Output is correct
11 Correct 21 ms 41428 KB Output is correct
12 Correct 21 ms 41440 KB Output is correct
13 Correct 25 ms 41472 KB Output is correct
14 Correct 21 ms 41396 KB Output is correct
15 Correct 21 ms 41464 KB Output is correct
16 Correct 20 ms 41440 KB Output is correct
17 Correct 20 ms 41412 KB Output is correct
18 Correct 19 ms 41512 KB Output is correct
19 Correct 21 ms 41496 KB Output is correct
20 Correct 20 ms 41396 KB Output is correct
21 Correct 21 ms 41508 KB Output is correct
22 Correct 20 ms 41484 KB Output is correct
23 Correct 21 ms 41492 KB Output is correct
24 Correct 20 ms 41428 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 41428 KB Output is correct
2 Correct 20 ms 41520 KB Output is correct
3 Correct 22 ms 41484 KB Output is correct
4 Correct 21 ms 41480 KB Output is correct
5 Correct 21 ms 41484 KB Output is correct
6 Correct 20 ms 41476 KB Output is correct
7 Correct 21 ms 41432 KB Output is correct
8 Correct 20 ms 41428 KB Output is correct
9 Correct 20 ms 41428 KB Output is correct
10 Correct 22 ms 41428 KB Output is correct
11 Correct 21 ms 41428 KB Output is correct
12 Correct 21 ms 41440 KB Output is correct
13 Correct 25 ms 41472 KB Output is correct
14 Correct 21 ms 41396 KB Output is correct
15 Correct 21 ms 41464 KB Output is correct
16 Correct 20 ms 41440 KB Output is correct
17 Correct 20 ms 41412 KB Output is correct
18 Correct 19 ms 41512 KB Output is correct
19 Correct 21 ms 41496 KB Output is correct
20 Correct 20 ms 41396 KB Output is correct
21 Correct 21 ms 41508 KB Output is correct
22 Correct 20 ms 41484 KB Output is correct
23 Correct 21 ms 41492 KB Output is correct
24 Correct 20 ms 41428 KB Output is correct
25 Correct 21 ms 41484 KB Output is correct
26 Correct 22 ms 41880 KB Output is correct
27 Correct 25 ms 41812 KB Output is correct
28 Correct 23 ms 42068 KB Output is correct
29 Correct 24 ms 42080 KB Output is correct
30 Correct 22 ms 41836 KB Output is correct
31 Correct 20 ms 41424 KB Output is correct
32 Correct 26 ms 41984 KB Output is correct
33 Correct 22 ms 41480 KB Output is correct
34 Correct 22 ms 41916 KB Output is correct
35 Correct 22 ms 42072 KB Output is correct
36 Correct 21 ms 41812 KB Output is correct
37 Correct 23 ms 41928 KB Output is correct
38 Correct 20 ms 41468 KB Output is correct
39 Correct 23 ms 41852 KB Output is correct
40 Correct 22 ms 41864 KB Output is correct
41 Correct 23 ms 41868 KB Output is correct
42 Correct 23 ms 42016 KB Output is correct
43 Correct 22 ms 42044 KB Output is correct
44 Correct 19 ms 41440 KB Output is correct
45 Correct 24 ms 41908 KB Output is correct
46 Correct 23 ms 41932 KB Output is correct
47 Correct 20 ms 41480 KB Output is correct
48 Correct 22 ms 41812 KB Output is correct
49 Correct 22 ms 42100 KB Output is correct
50 Correct 23 ms 42124 KB Output is correct
51 Correct 26 ms 41852 KB Output is correct
52 Correct 27 ms 41832 KB Output is correct
53 Correct 27 ms 41940 KB Output is correct
54 Correct 26 ms 41808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 41428 KB Output is correct
2 Correct 20 ms 41520 KB Output is correct
3 Correct 22 ms 41484 KB Output is correct
4 Correct 21 ms 41480 KB Output is correct
5 Correct 21 ms 41484 KB Output is correct
6 Correct 20 ms 41476 KB Output is correct
7 Correct 21 ms 41432 KB Output is correct
8 Correct 20 ms 41428 KB Output is correct
9 Correct 20 ms 41428 KB Output is correct
10 Correct 22 ms 41428 KB Output is correct
11 Correct 21 ms 41428 KB Output is correct
12 Correct 21 ms 41440 KB Output is correct
13 Correct 25 ms 41472 KB Output is correct
14 Correct 21 ms 41396 KB Output is correct
15 Correct 21 ms 41464 KB Output is correct
16 Correct 20 ms 41440 KB Output is correct
17 Correct 20 ms 41412 KB Output is correct
18 Correct 19 ms 41512 KB Output is correct
19 Correct 21 ms 41496 KB Output is correct
20 Correct 20 ms 41396 KB Output is correct
21 Correct 21 ms 41508 KB Output is correct
22 Correct 20 ms 41484 KB Output is correct
23 Correct 21 ms 41492 KB Output is correct
24 Correct 20 ms 41428 KB Output is correct
25 Correct 19 ms 41476 KB Output is correct
26 Correct 107 ms 53820 KB Output is correct
27 Correct 130 ms 53532 KB Output is correct
28 Correct 23 ms 41864 KB Output is correct
29 Correct 23 ms 41528 KB Output is correct
30 Correct 25 ms 41408 KB Output is correct
31 Correct 128 ms 53532 KB Output is correct
32 Correct 23 ms 41812 KB Output is correct
33 Correct 148 ms 59276 KB Output is correct
34 Correct 133 ms 53616 KB Output is correct
35 Correct 23 ms 41892 KB Output is correct
36 Correct 137 ms 54168 KB Output is correct
37 Correct 28 ms 41804 KB Output is correct
38 Correct 24 ms 41836 KB Output is correct
39 Correct 116 ms 53864 KB Output is correct
40 Correct 27 ms 42056 KB Output is correct
41 Correct 116 ms 53452 KB Output is correct
42 Correct 142 ms 55460 KB Output is correct
43 Correct 21 ms 41480 KB Output is correct
44 Correct 154 ms 59572 KB Output is correct
45 Correct 150 ms 56496 KB Output is correct
46 Correct 23 ms 41812 KB Output is correct
47 Correct 23 ms 41904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 108 ms 53768 KB Output is correct
2 Correct 125 ms 59892 KB Output is correct
3 Correct 21 ms 41940 KB Output is correct
4 Correct 24 ms 41892 KB Output is correct
5 Correct 20 ms 41428 KB Output is correct
6 Correct 21 ms 41408 KB Output is correct
7 Correct 22 ms 41912 KB Output is correct
8 Correct 158 ms 54988 KB Output is correct
9 Correct 22 ms 41876 KB Output is correct
10 Correct 134 ms 55052 KB Output is correct
11 Correct 20 ms 41420 KB Output is correct
12 Correct 171 ms 54956 KB Output is correct
13 Correct 154 ms 56764 KB Output is correct
14 Correct 181 ms 60992 KB Output is correct
15 Correct 130 ms 55184 KB Output is correct
16 Correct 28 ms 41968 KB Output is correct
17 Correct 23 ms 41484 KB Output is correct
18 Correct 134 ms 60664 KB Output is correct
19 Correct 172 ms 64036 KB Output is correct
20 Correct 25 ms 41816 KB Output is correct
21 Correct 25 ms 41380 KB Output is correct
22 Correct 137 ms 57120 KB Output is correct
23 Correct 23 ms 41912 KB Output is correct
24 Correct 143 ms 55592 KB Output is correct
25 Correct 165 ms 63040 KB Output is correct
26 Correct 29 ms 42004 KB Output is correct
27 Correct 23 ms 42120 KB Output is correct
28 Correct 23 ms 41936 KB Output is correct
29 Correct 22 ms 41848 KB Output is correct
30 Correct 24 ms 41916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 41428 KB Output is correct
2 Correct 20 ms 41520 KB Output is correct
3 Correct 22 ms 41484 KB Output is correct
4 Correct 21 ms 41480 KB Output is correct
5 Correct 21 ms 41484 KB Output is correct
6 Correct 20 ms 41476 KB Output is correct
7 Correct 21 ms 41432 KB Output is correct
8 Correct 20 ms 41428 KB Output is correct
9 Correct 20 ms 41428 KB Output is correct
10 Correct 22 ms 41428 KB Output is correct
11 Correct 21 ms 41428 KB Output is correct
12 Correct 21 ms 41440 KB Output is correct
13 Correct 25 ms 41472 KB Output is correct
14 Correct 21 ms 41396 KB Output is correct
15 Correct 21 ms 41464 KB Output is correct
16 Correct 20 ms 41440 KB Output is correct
17 Correct 20 ms 41412 KB Output is correct
18 Correct 19 ms 41512 KB Output is correct
19 Correct 21 ms 41496 KB Output is correct
20 Correct 20 ms 41396 KB Output is correct
21 Correct 21 ms 41508 KB Output is correct
22 Correct 20 ms 41484 KB Output is correct
23 Correct 21 ms 41492 KB Output is correct
24 Correct 20 ms 41428 KB Output is correct
25 Correct 21 ms 41484 KB Output is correct
26 Correct 22 ms 41880 KB Output is correct
27 Correct 25 ms 41812 KB Output is correct
28 Correct 23 ms 42068 KB Output is correct
29 Correct 24 ms 42080 KB Output is correct
30 Correct 22 ms 41836 KB Output is correct
31 Correct 20 ms 41424 KB Output is correct
32 Correct 26 ms 41984 KB Output is correct
33 Correct 22 ms 41480 KB Output is correct
34 Correct 22 ms 41916 KB Output is correct
35 Correct 22 ms 42072 KB Output is correct
36 Correct 21 ms 41812 KB Output is correct
37 Correct 23 ms 41928 KB Output is correct
38 Correct 20 ms 41468 KB Output is correct
39 Correct 23 ms 41852 KB Output is correct
40 Correct 22 ms 41864 KB Output is correct
41 Correct 23 ms 41868 KB Output is correct
42 Correct 23 ms 42016 KB Output is correct
43 Correct 22 ms 42044 KB Output is correct
44 Correct 19 ms 41440 KB Output is correct
45 Correct 24 ms 41908 KB Output is correct
46 Correct 23 ms 41932 KB Output is correct
47 Correct 20 ms 41480 KB Output is correct
48 Correct 22 ms 41812 KB Output is correct
49 Correct 22 ms 42100 KB Output is correct
50 Correct 23 ms 42124 KB Output is correct
51 Correct 26 ms 41852 KB Output is correct
52 Correct 27 ms 41832 KB Output is correct
53 Correct 27 ms 41940 KB Output is correct
54 Correct 26 ms 41808 KB Output is correct
55 Correct 19 ms 41476 KB Output is correct
56 Correct 107 ms 53820 KB Output is correct
57 Correct 130 ms 53532 KB Output is correct
58 Correct 23 ms 41864 KB Output is correct
59 Correct 23 ms 41528 KB Output is correct
60 Correct 25 ms 41408 KB Output is correct
61 Correct 128 ms 53532 KB Output is correct
62 Correct 23 ms 41812 KB Output is correct
63 Correct 148 ms 59276 KB Output is correct
64 Correct 133 ms 53616 KB Output is correct
65 Correct 23 ms 41892 KB Output is correct
66 Correct 137 ms 54168 KB Output is correct
67 Correct 28 ms 41804 KB Output is correct
68 Correct 24 ms 41836 KB Output is correct
69 Correct 116 ms 53864 KB Output is correct
70 Correct 27 ms 42056 KB Output is correct
71 Correct 116 ms 53452 KB Output is correct
72 Correct 142 ms 55460 KB Output is correct
73 Correct 21 ms 41480 KB Output is correct
74 Correct 154 ms 59572 KB Output is correct
75 Correct 150 ms 56496 KB Output is correct
76 Correct 23 ms 41812 KB Output is correct
77 Correct 23 ms 41904 KB Output is correct
78 Correct 108 ms 53768 KB Output is correct
79 Correct 125 ms 59892 KB Output is correct
80 Correct 21 ms 41940 KB Output is correct
81 Correct 24 ms 41892 KB Output is correct
82 Correct 20 ms 41428 KB Output is correct
83 Correct 21 ms 41408 KB Output is correct
84 Correct 22 ms 41912 KB Output is correct
85 Correct 158 ms 54988 KB Output is correct
86 Correct 22 ms 41876 KB Output is correct
87 Correct 134 ms 55052 KB Output is correct
88 Correct 20 ms 41420 KB Output is correct
89 Correct 171 ms 54956 KB Output is correct
90 Correct 154 ms 56764 KB Output is correct
91 Correct 181 ms 60992 KB Output is correct
92 Correct 130 ms 55184 KB Output is correct
93 Correct 28 ms 41968 KB Output is correct
94 Correct 23 ms 41484 KB Output is correct
95 Correct 134 ms 60664 KB Output is correct
96 Correct 172 ms 64036 KB Output is correct
97 Correct 25 ms 41816 KB Output is correct
98 Correct 25 ms 41380 KB Output is correct
99 Correct 137 ms 57120 KB Output is correct
100 Correct 23 ms 41912 KB Output is correct
101 Correct 143 ms 55592 KB Output is correct
102 Correct 165 ms 63040 KB Output is correct
103 Correct 29 ms 42004 KB Output is correct
104 Correct 23 ms 42120 KB Output is correct
105 Correct 23 ms 41936 KB Output is correct
106 Correct 22 ms 41848 KB Output is correct
107 Correct 24 ms 41916 KB Output is correct
108 Correct 795 ms 117996 KB Output is correct
109 Correct 938 ms 122688 KB Output is correct
110 Correct 941 ms 131612 KB Output is correct
111 Correct 1163 ms 157044 KB Output is correct
112 Correct 960 ms 144068 KB Output is correct
113 Correct 759 ms 138320 KB Output is correct
114 Correct 706 ms 108472 KB Output is correct
115 Correct 698 ms 108456 KB Output is correct
116 Correct 1049 ms 113072 KB Output is correct
117 Correct 1024 ms 140556 KB Output is correct
118 Correct 832 ms 109000 KB Output is correct
119 Correct 1083 ms 140424 KB Output is correct
120 Correct 1078 ms 151096 KB Output is correct
121 Correct 1025 ms 140436 KB Output is correct
122 Correct 1004 ms 120904 KB Output is correct
123 Correct 729 ms 143372 KB Output is correct
124 Correct 1000 ms 132364 KB Output is correct