Submission #337089

# Submission time Handle Problem Language Result Execution time Memory
337089 2020-12-18T09:15:38 Z alishahali1382 Mergers (JOI19_mergers) C++14
0 / 100
99 ms 35064 KB
#include <bits/stdc++.h>
#pragma GCC optimize ("O2,unroll-loops")
//#pragma GCC optimize("no-stack-protector,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef pair<ll, ll> pll;
#define debug(x) cerr<<#x<<'='<<(x)<<endl;
#define debugp(x) cerr<<#x<<"= {"<<(x.first)<<", "<<(x.second)<<"}"<<endl;
#define debug2(x, y) cerr<<"{"<<#x<<", "<<#y<<"} = {"<<(x)<<", "<<(y)<<"}"<<endl;
#define debugv(v) {cerr<<#v<<" : ";for (auto x:v) cerr<<x<<' ';cerr<<endl;}
#define all(x) x.begin(), x.end()
#define pb push_back
#define kill(x) return cout<<x<<'\n', 0;

const int inf=1000000010;
const ll INF=10000000000000010LL;
const int mod=1000000007;
const int MAXN=500010, LOG=20;

int n, m, k, u, v, x, y, t, a, b, ans;
int par[LOG][MAXN], mark[MAXN];
int stt[MAXN], fnt[MAXN], timer=1;
int S[MAXN], dsu[MAXN], deg[MAXN];
vector<int> G[MAXN];
vector<pii> vec[MAXN];

void dfs(int node){
	stt[node]=timer++;
	for (int v:G[node]) if (v!=par[0][node]){
		par[0][v]=node;
		dfs(v);
	}
	fnt[node]=timer;
}
int Lca(int x, int y){
	if (fnt[y]<=fnt[x]) return x;
	for (int i=LOG-1; ~i; i--) if (fnt[y]>fnt[par[i][x]]) x=par[i][x];
	return par[0][x];
}
int dfs2(int node){
	for (int v:G[node]) if (v!=par[0][node]) mark[node]+=dfs2(v);
	return ++mark[node];
}
int get(int x){ return (dsu[x]==x?x:dsu[x]=get(dsu[x]));}

int main(){
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	cin>>n>>k;
	for (int i=1; i<n; i++){
		cin>>u>>v;
		G[u].pb(v);
		G[v].pb(u);
	}
	par[0][1]=1;
	dfs(1);
	for (int i=1; i<=n; i++) cin>>S[i], vec[S[i]].pb({stt[i], i});
	for (int i=1; i<=k; i++){
		sort(all(vec[i]));
		mark[Lca(vec[i][0].second, vec[i].back().second)]-=vec[i].size();
	}
	dfs2(1);
	iota(dsu+1, dsu+n+1, 1);
	for (int i=2; i<=n; i++) if (mark[i]) dsu[get(par[0][i])]=get(i);
	for (int i=2; i<=n; i++) if (!mark[i]){
		deg[get(par[0][i])]++;
		deg[get(i)]++;
	}
	for (int i=1; i<=n; i++) ans+=(deg[i]==1);
	debug(ans)
	cout<<(ans+1)/2<<"\n";
	
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 17 ms 23916 KB Output is correct
2 Incorrect 17 ms 23936 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 17 ms 23916 KB Output is correct
2 Incorrect 17 ms 23936 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 17 ms 23916 KB Output is correct
2 Incorrect 17 ms 23936 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 74 ms 32368 KB Output is correct
2 Correct 99 ms 35064 KB Output is correct
3 Incorrect 19 ms 24172 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 17 ms 23916 KB Output is correct
2 Incorrect 17 ms 23936 KB Output isn't correct
3 Halted 0 ms 0 KB -