Submission #328543

# Submission time Handle Problem Language Result Execution time Memory
328543 2020-11-17T04:09:09 Z YJU Capital City (JOI20_capital_city) C++14
1 / 100
817 ms 64492 KB
#include<bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector")
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pll;
const ll MOD=1e9+7;
const ll MOD2=998244353;
const ll N=4e5+5;
const ll K=350;
const ld pi=3.14159265359;
const ll INF=(1LL<<40);
#define SQ(i) ((i)*(i))
#define REP(i,n) for(ll i=0;i<n;i++)
#define REP1(i,n) for(ll i=1;i<=n;i++)
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define setp setprecision
#define lwb lower_bound
#define SZ(_a) (ll)_a.size()

ll n,m,x,y,col[N],maxn[N],vis[N],sub[N],vis_col[N],ans=INF;
vector<ll> v[N],C[N];
ll pa[N],tag[N];

ll Find_root(ll nd){
	function<void(ll,ll)> DFS = [&](ll id,ll p){
		sub[id]=1;maxn[id]=-1;
		for(auto i:v[id]){
			if(i!=p&&!vis[i]){
				DFS(i,id);
				sub[id]+=sub[i];
				maxn[id]=((maxn[id]==-1||sub[maxn[id]]<sub[i])?i:maxn[id]);
			}
		}
	};
	DFS(nd,0);
	ll tot=sub[nd];
	while(maxn[nd]!=-1&&sub[maxn[nd]]*2>=tot)nd=maxn[nd];
	return nd;
}

void centroid(ll nd){
	ll rt=Find_root(nd);
	function<void(ll,ll)> DFS = [&](ll id,ll p){
		pa[id]=p;tag[id]=rt;
		for(auto i:v[id]){
			if(!vis[i]&&i!=p)DFS(i,id);
		}
	};
	DFS(rt,rt);
	vector<ll> res;
	queue<ll> q;
	q.push(rt);
	ll cnt=-1,ok=1,u;
	while(SZ(q)&&ok){
		u=q.front();q.pop();
		++cnt;
		while(ok){
			if(tag[u]==0)break;
			if(!vis_col[col[u]]){
				cnt-=SZ(C[col[u]]);
				vis_col[col[u]]=1;
				res.pb(col[u]);
				for(auto i:C[col[u]]){
					q.push(i);
					if(tag[i]!=rt){
						ok=0;break;
					}
				}
			}
			tag[u]=0;
			u=pa[u];
			if(u==rt)break;
		}
	}
	for(auto i:res)vis_col[i]=0;
	if(cnt==0)ans=min(ans,SZ(res)-1);
	vis[u]=1;
	for(auto i:v[u])if(!vis[i])centroid(i);
}

int main(){
	ios_base::sync_with_stdio(0);cin.tie(0);
	cin>>n>>m;
	REP1(i,n-1)cin>>x>>y,v[x].pb(y),v[y].pb(x);
	REP1(i,n)cin>>col[i],C[col[i]].pb(i);
	centroid(1);
	cout<<ans<<"\n"; 
	return 0;
}

Compilation message

capital_city.cpp: In function 'void centroid(ll)':
capital_city.cpp:57:17: warning: 'u' may be used uninitialized in this function [-Wmaybe-uninitialized]
   57 |  ll cnt=-1,ok=1,u;
      |                 ^
# Verdict Execution time Memory Grader output
1 Correct 12 ms 19308 KB Output is correct
2 Correct 12 ms 19180 KB Output is correct
3 Correct 12 ms 19180 KB Output is correct
4 Correct 12 ms 19180 KB Output is correct
5 Correct 12 ms 19180 KB Output is correct
6 Correct 12 ms 19180 KB Output is correct
7 Correct 12 ms 19180 KB Output is correct
8 Correct 13 ms 19308 KB Output is correct
9 Correct 13 ms 19180 KB Output is correct
10 Correct 12 ms 19180 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 19308 KB Output is correct
2 Correct 12 ms 19180 KB Output is correct
3 Correct 12 ms 19180 KB Output is correct
4 Correct 12 ms 19180 KB Output is correct
5 Correct 12 ms 19180 KB Output is correct
6 Correct 12 ms 19180 KB Output is correct
7 Correct 12 ms 19180 KB Output is correct
8 Correct 13 ms 19308 KB Output is correct
9 Correct 13 ms 19180 KB Output is correct
10 Correct 12 ms 19180 KB Output is correct
11 Correct 14 ms 19436 KB Output is correct
12 Correct 14 ms 19436 KB Output is correct
13 Correct 15 ms 19456 KB Output is correct
14 Correct 14 ms 19436 KB Output is correct
15 Correct 15 ms 19564 KB Output is correct
16 Correct 15 ms 19436 KB Output is correct
17 Incorrect 14 ms 19436 KB Output isn't correct
18 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 817 ms 63712 KB Output is correct
2 Correct 251 ms 64364 KB Output is correct
3 Correct 800 ms 63340 KB Output is correct
4 Correct 256 ms 64492 KB Output is correct
5 Incorrect 804 ms 59376 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 12 ms 19308 KB Output is correct
2 Correct 12 ms 19180 KB Output is correct
3 Correct 12 ms 19180 KB Output is correct
4 Correct 12 ms 19180 KB Output is correct
5 Correct 12 ms 19180 KB Output is correct
6 Correct 12 ms 19180 KB Output is correct
7 Correct 12 ms 19180 KB Output is correct
8 Correct 13 ms 19308 KB Output is correct
9 Correct 13 ms 19180 KB Output is correct
10 Correct 12 ms 19180 KB Output is correct
11 Correct 14 ms 19436 KB Output is correct
12 Correct 14 ms 19436 KB Output is correct
13 Correct 15 ms 19456 KB Output is correct
14 Correct 14 ms 19436 KB Output is correct
15 Correct 15 ms 19564 KB Output is correct
16 Correct 15 ms 19436 KB Output is correct
17 Incorrect 14 ms 19436 KB Output isn't correct
18 Halted 0 ms 0 KB -