Submission #258053

# Submission time Handle Problem Language Result Execution time Memory
258053 2020-08-05T09:19:30 Z AMO5 Capital City (JOI20_capital_city) C++17
11 / 100
3000 ms 50012 KB
#include <bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define eb emplace_back
#define mt make_tuple
#define all(x) (x).begin(), (x).end()  
#define sz(x) int(x.size()) 
#define MOD 1000000007

typedef long long ll;
typedef pair <int, int> ii;
typedef pair <ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef long double ld;

const ll INF=LLONG_MAX;
const int mxn=2e5+5;
bool DEBUG=0;

int n,k,city[mxn],siz[mxn],h[mxn],par[mxn],ans;
vi adj[mxn],adj2[mxn];
set<int>nodes,mcity,usedcity;
//required to vis, merged city, visited city
int citycnt[mxn];
bool vis[mxn];

struct DSU{
	vi par;
	DSU(int n){
		for(int i=0; i<n; i++){
			par.eb(i);
		}
	}
	void reset(int n){
		par.resize(n);
		for(int i=0; i<n; i++){
			par[i]=i;
		}
	}
	void resetnode(int u){
		par[u]=u;
	}
	int rt(int u){
		if(u!=par[u])par[u]=rt(par[u]);
		return par[u];
	}
	void merge(int u, int v){
		u=rt(u); v=rt(v);
		if(u==v)return;
		if(h[u]>h[v])swap(u,v);
		par[v]=u;
	}
	int getrt(int u){
		return rt(u);
	}
};
DSU dsu(1);

void dfs_sz(int u, int p=-1){
	siz[u]=1;
	citycnt[city[u]]++;
	usedcity.insert(city[u]);
	for(int v:adj[u]){
		if(v==p||vis[v])continue;
		dfs_sz(v,u);
		siz[u]+=siz[v];
	}
	return;
}

int centroid(int u, int p, int sizz){
	for(int v:adj[u]){
		if(v==p||vis[v])continue;
		if(siz[v]*2>sizz)return centroid(v,u,sizz);
	}
	return u;
}

void dfs(int u, int p){
	nodes.insert(u);
	for(int v:adj[u]){
		if(v==p||vis[v])continue;
		par[v]=u;
		h[v]=h[u]+1;
		dfs(v,u);
	}
	return;
}

void ctsolve(int ct){
	//assuming capital city is centroid
	par[ct]=-1;
	h[ct]=0;
	dfs(ct,-1);
	int City = city[ct];
	set<int>ncity;
	//cities need to visit 
	ncity.emplace(City);
	bool ok=1;
	if(sz(adj2[City])!=citycnt[City]){
		ok=0; ncity.erase(City);
	}
	while(ncity.size()&&ok){
		int u = *ncity.begin();
		ncity.erase(u);
		mcity.insert(u);
		for(int v:adj2[u]){
			while(v!=ct){
				v=dsu.rt(v);
				if(v==ct)break;
				if(ncity.find(city[par[v]])==ncity.end()&&mcity.find(city[par[v]])==mcity.end()){
					if(sz(adj2[city[par[v]]])==citycnt[city[par[v]]])ncity.insert(city[par[v]]);
					else{
						ok=0;
						break;
					}
				}
				dsu.merge(par[v],v);
				v=par[v];
			}
			if(!ok)break;
		}
	}
	for(int x:nodes){
		dsu.resetnode(x);
	}
	for(int x:usedcity)citycnt[x]=0;
	if(ok)ans = min(ans,sz(mcity));
	nodes=mcity=usedcity=set<int>();
	return;
}

void solve(int u=0){
	dfs_sz(u);
	int ct = centroid(u,-1,siz[u]);
	ctsolve(ct);
	vis[ct]=1;
	for(int v:adj[ct]){
		if(!vis[v]){
			solve(v);
		}
	}
}

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0);
    //freopen("input.txt","r",stdin); freopen("output.txt","w",stdout);
	cin >> n >> k;
	ans=k;
	dsu.reset(n);
	for(int i=0; i<n-1; i++){
		int u,v;
		cin >> u >> v;
		u--; v--;
		adj[u].eb(v);
		adj[v].eb(u);
	}
	for(int i=0; i<n; i++){
		cin>>city[i];
		city[i]--;
		adj2[city[i]].eb(i);
	}
	for(int i=0; i<k; i++){
		if(sz(adj2[i])==1){
			cout << 0 << '\n';
			return 0;
		}
	}
	solve(0);
	cout << ans-1 << '\n';
	return 0;
}
	
// READ & UNDERSTAND
// ll, int overflow, array bounds, memset(0)
// special cases (n=1?), n+1 (1-index)
// do smth instead of nothing & stay organized
// WRITE STUFF DOWN
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9728 KB Output is correct
2 Correct 6 ms 9728 KB Output is correct
3 Correct 6 ms 9728 KB Output is correct
4 Correct 6 ms 9728 KB Output is correct
5 Correct 6 ms 9728 KB Output is correct
6 Correct 6 ms 9728 KB Output is correct
7 Correct 6 ms 9728 KB Output is correct
8 Correct 7 ms 9816 KB Output is correct
9 Correct 6 ms 9788 KB Output is correct
10 Correct 6 ms 9728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9728 KB Output is correct
2 Correct 6 ms 9728 KB Output is correct
3 Correct 6 ms 9728 KB Output is correct
4 Correct 6 ms 9728 KB Output is correct
5 Correct 6 ms 9728 KB Output is correct
6 Correct 6 ms 9728 KB Output is correct
7 Correct 6 ms 9728 KB Output is correct
8 Correct 7 ms 9816 KB Output is correct
9 Correct 6 ms 9788 KB Output is correct
10 Correct 6 ms 9728 KB Output is correct
11 Correct 10 ms 9984 KB Output is correct
12 Correct 11 ms 9984 KB Output is correct
13 Correct 15 ms 9984 KB Output is correct
14 Correct 11 ms 9984 KB Output is correct
15 Correct 12 ms 10112 KB Output is correct
16 Correct 12 ms 9984 KB Output is correct
17 Correct 11 ms 10112 KB Output is correct
18 Correct 9 ms 10112 KB Output is correct
19 Correct 9 ms 10112 KB Output is correct
20 Correct 10 ms 10112 KB Output is correct
21 Correct 10 ms 10112 KB Output is correct
22 Correct 14 ms 10112 KB Output is correct
23 Correct 13 ms 10112 KB Output is correct
24 Correct 14 ms 10112 KB Output is correct
25 Correct 14 ms 10112 KB Output is correct
26 Correct 14 ms 10112 KB Output is correct
27 Correct 13 ms 10112 KB Output is correct
28 Correct 14 ms 10112 KB Output is correct
29 Correct 12 ms 10112 KB Output is correct
30 Correct 12 ms 10112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 3050 ms 50012 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9728 KB Output is correct
2 Correct 6 ms 9728 KB Output is correct
3 Correct 6 ms 9728 KB Output is correct
4 Correct 6 ms 9728 KB Output is correct
5 Correct 6 ms 9728 KB Output is correct
6 Correct 6 ms 9728 KB Output is correct
7 Correct 6 ms 9728 KB Output is correct
8 Correct 7 ms 9816 KB Output is correct
9 Correct 6 ms 9788 KB Output is correct
10 Correct 6 ms 9728 KB Output is correct
11 Correct 10 ms 9984 KB Output is correct
12 Correct 11 ms 9984 KB Output is correct
13 Correct 15 ms 9984 KB Output is correct
14 Correct 11 ms 9984 KB Output is correct
15 Correct 12 ms 10112 KB Output is correct
16 Correct 12 ms 9984 KB Output is correct
17 Correct 11 ms 10112 KB Output is correct
18 Correct 9 ms 10112 KB Output is correct
19 Correct 9 ms 10112 KB Output is correct
20 Correct 10 ms 10112 KB Output is correct
21 Correct 10 ms 10112 KB Output is correct
22 Correct 14 ms 10112 KB Output is correct
23 Correct 13 ms 10112 KB Output is correct
24 Correct 14 ms 10112 KB Output is correct
25 Correct 14 ms 10112 KB Output is correct
26 Correct 14 ms 10112 KB Output is correct
27 Correct 13 ms 10112 KB Output is correct
28 Correct 14 ms 10112 KB Output is correct
29 Correct 12 ms 10112 KB Output is correct
30 Correct 12 ms 10112 KB Output is correct
31 Execution timed out 3050 ms 50012 KB Time limit exceeded
32 Halted 0 ms 0 KB -