Submission #706863

# Submission time Handle Problem Language Result Execution time Memory
706863 2023-03-07T23:16:04 Z Antekb Mergers (JOI19_mergers) C++14
24 / 100
161 ms 61688 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) && find(j)!=find(par[0][j])){
				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 19 ms 41468 KB Output is correct
2 Correct 19 ms 41400 KB Output is correct
3 Correct 20 ms 41488 KB Output is correct
4 Correct 21 ms 41392 KB Output is correct
5 Correct 19 ms 41444 KB Output is correct
6 Correct 19 ms 41428 KB Output is correct
7 Correct 19 ms 41428 KB Output is correct
8 Correct 19 ms 41500 KB Output is correct
9 Correct 19 ms 41428 KB Output is correct
10 Correct 19 ms 41408 KB Output is correct
11 Correct 19 ms 41420 KB Output is correct
12 Correct 19 ms 41456 KB Output is correct
13 Correct 19 ms 41428 KB Output is correct
14 Correct 19 ms 41428 KB Output is correct
15 Correct 19 ms 41428 KB Output is correct
16 Correct 19 ms 41516 KB Output is correct
17 Correct 18 ms 41428 KB Output is correct
18 Correct 22 ms 41396 KB Output is correct
19 Correct 19 ms 41400 KB Output is correct
20 Correct 18 ms 41416 KB Output is correct
21 Correct 18 ms 41428 KB Output is correct
22 Correct 19 ms 41428 KB Output is correct
23 Correct 20 ms 41428 KB Output is correct
24 Correct 18 ms 41432 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 41468 KB Output is correct
2 Correct 19 ms 41400 KB Output is correct
3 Correct 20 ms 41488 KB Output is correct
4 Correct 21 ms 41392 KB Output is correct
5 Correct 19 ms 41444 KB Output is correct
6 Correct 19 ms 41428 KB Output is correct
7 Correct 19 ms 41428 KB Output is correct
8 Correct 19 ms 41500 KB Output is correct
9 Correct 19 ms 41428 KB Output is correct
10 Correct 19 ms 41408 KB Output is correct
11 Correct 19 ms 41420 KB Output is correct
12 Correct 19 ms 41456 KB Output is correct
13 Correct 19 ms 41428 KB Output is correct
14 Correct 19 ms 41428 KB Output is correct
15 Correct 19 ms 41428 KB Output is correct
16 Correct 19 ms 41516 KB Output is correct
17 Correct 18 ms 41428 KB Output is correct
18 Correct 22 ms 41396 KB Output is correct
19 Correct 19 ms 41400 KB Output is correct
20 Correct 18 ms 41416 KB Output is correct
21 Correct 18 ms 41428 KB Output is correct
22 Correct 19 ms 41428 KB Output is correct
23 Correct 20 ms 41428 KB Output is correct
24 Correct 18 ms 41432 KB Output is correct
25 Correct 19 ms 41424 KB Output is correct
26 Correct 22 ms 42004 KB Output is correct
27 Correct 21 ms 41928 KB Output is correct
28 Correct 21 ms 42120 KB Output is correct
29 Correct 21 ms 42084 KB Output is correct
30 Correct 21 ms 41812 KB Output is correct
31 Correct 18 ms 41472 KB Output is correct
32 Correct 22 ms 42124 KB Output is correct
33 Correct 19 ms 41428 KB Output is correct
34 Correct 23 ms 41880 KB Output is correct
35 Correct 27 ms 42080 KB Output is correct
36 Correct 25 ms 41992 KB Output is correct
37 Correct 24 ms 41868 KB Output is correct
38 Correct 20 ms 41428 KB Output is correct
39 Incorrect 22 ms 41944 KB Output isn't correct
40 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 19 ms 41468 KB Output is correct
2 Correct 19 ms 41400 KB Output is correct
3 Correct 20 ms 41488 KB Output is correct
4 Correct 21 ms 41392 KB Output is correct
5 Correct 19 ms 41444 KB Output is correct
6 Correct 19 ms 41428 KB Output is correct
7 Correct 19 ms 41428 KB Output is correct
8 Correct 19 ms 41500 KB Output is correct
9 Correct 19 ms 41428 KB Output is correct
10 Correct 19 ms 41408 KB Output is correct
11 Correct 19 ms 41420 KB Output is correct
12 Correct 19 ms 41456 KB Output is correct
13 Correct 19 ms 41428 KB Output is correct
14 Correct 19 ms 41428 KB Output is correct
15 Correct 19 ms 41428 KB Output is correct
16 Correct 19 ms 41516 KB Output is correct
17 Correct 18 ms 41428 KB Output is correct
18 Correct 22 ms 41396 KB Output is correct
19 Correct 19 ms 41400 KB Output is correct
20 Correct 18 ms 41416 KB Output is correct
21 Correct 18 ms 41428 KB Output is correct
22 Correct 19 ms 41428 KB Output is correct
23 Correct 20 ms 41428 KB Output is correct
24 Correct 18 ms 41432 KB Output is correct
25 Correct 22 ms 41476 KB Output is correct
26 Correct 110 ms 55144 KB Output is correct
27 Correct 125 ms 54856 KB Output is correct
28 Correct 21 ms 41856 KB Output is correct
29 Correct 23 ms 41488 KB Output is correct
30 Correct 22 ms 41500 KB Output is correct
31 Correct 116 ms 54756 KB Output is correct
32 Correct 21 ms 41864 KB Output is correct
33 Correct 148 ms 60700 KB Output is correct
34 Correct 139 ms 54868 KB Output is correct
35 Correct 21 ms 41920 KB Output is correct
36 Correct 136 ms 55516 KB Output is correct
37 Correct 22 ms 41812 KB Output is correct
38 Correct 22 ms 41940 KB Output is correct
39 Correct 109 ms 55160 KB Output is correct
40 Correct 22 ms 42004 KB Output is correct
41 Correct 114 ms 54792 KB Output is correct
42 Correct 141 ms 56884 KB Output is correct
43 Correct 20 ms 41420 KB Output is correct
44 Correct 137 ms 60744 KB Output is correct
45 Correct 145 ms 57804 KB Output is correct
46 Correct 22 ms 41908 KB Output is correct
47 Correct 23 ms 41852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 116 ms 55172 KB Output is correct
2 Correct 130 ms 61688 KB Output is correct
3 Correct 23 ms 42004 KB Output is correct
4 Correct 25 ms 41920 KB Output is correct
5 Correct 20 ms 41476 KB Output is correct
6 Correct 19 ms 41428 KB Output is correct
7 Correct 22 ms 41868 KB Output is correct
8 Incorrect 161 ms 56736 KB Output isn't correct
9 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 19 ms 41468 KB Output is correct
2 Correct 19 ms 41400 KB Output is correct
3 Correct 20 ms 41488 KB Output is correct
4 Correct 21 ms 41392 KB Output is correct
5 Correct 19 ms 41444 KB Output is correct
6 Correct 19 ms 41428 KB Output is correct
7 Correct 19 ms 41428 KB Output is correct
8 Correct 19 ms 41500 KB Output is correct
9 Correct 19 ms 41428 KB Output is correct
10 Correct 19 ms 41408 KB Output is correct
11 Correct 19 ms 41420 KB Output is correct
12 Correct 19 ms 41456 KB Output is correct
13 Correct 19 ms 41428 KB Output is correct
14 Correct 19 ms 41428 KB Output is correct
15 Correct 19 ms 41428 KB Output is correct
16 Correct 19 ms 41516 KB Output is correct
17 Correct 18 ms 41428 KB Output is correct
18 Correct 22 ms 41396 KB Output is correct
19 Correct 19 ms 41400 KB Output is correct
20 Correct 18 ms 41416 KB Output is correct
21 Correct 18 ms 41428 KB Output is correct
22 Correct 19 ms 41428 KB Output is correct
23 Correct 20 ms 41428 KB Output is correct
24 Correct 18 ms 41432 KB Output is correct
25 Correct 19 ms 41424 KB Output is correct
26 Correct 22 ms 42004 KB Output is correct
27 Correct 21 ms 41928 KB Output is correct
28 Correct 21 ms 42120 KB Output is correct
29 Correct 21 ms 42084 KB Output is correct
30 Correct 21 ms 41812 KB Output is correct
31 Correct 18 ms 41472 KB Output is correct
32 Correct 22 ms 42124 KB Output is correct
33 Correct 19 ms 41428 KB Output is correct
34 Correct 23 ms 41880 KB Output is correct
35 Correct 27 ms 42080 KB Output is correct
36 Correct 25 ms 41992 KB Output is correct
37 Correct 24 ms 41868 KB Output is correct
38 Correct 20 ms 41428 KB Output is correct
39 Incorrect 22 ms 41944 KB Output isn't correct
40 Halted 0 ms 0 KB -