Submission #289340

# Submission time Handle Problem Language Result Execution time Memory
289340 2020-09-02T14:59:22 Z TadijaSebez Capital City (JOI20_capital_city) C++11
100 / 100
1140 ms 66296 KB
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define pii pair<int,int>
const int N=200050;
const int L=18;
int up[N],lca[N],c[N];
int Find(int x){return up[x]==x?x:up[x]=Find(up[x]);}
vector<int> nodes[N],E[N];
int par[N][L],dep[N],head[N],lid[N],rid[N],sz[N],tme;
void Tree(int u,int p){
	par[u][0]=p;
	dep[u]=dep[p]+1;
	sz[u]=1;
	for(int i=1;i<L;i++)par[u][i]=par[par[u][i-1]][i-1];
	for(int v:E[u])if(v!=p)Tree(v,u),sz[u]+=sz[v];
}
void HLD(int u,int p){
	if(!head[u])head[u]=u;
	lid[u]=++tme;
	int hc=0;
	for(int v:E[u])if(v!=p&&(!hc||sz[hc]<sz[v]))hc=v;
	if(hc)head[hc]=head[u],HLD(hc,u);
	for(int v:E[u])if(v!=p&&v!=hc)HLD(v,u);
	rid[u]=tme;
}
int LCA(int u,int v){
	if(dep[u]<dep[v])swap(u,v);
	for(int i=L-1;~i;i--)if(dep[par[u][i]]>=dep[v])u=par[u][i];
	for(int i=L-1;~i;i--)if(par[u][i]!=par[v][i])u=par[u][i],v=par[v][i];
	return u==v?v:par[v][0];
}
const int M=2*N;
pii st[M];
pii operator + (pii a,pii b){return {min(a.first,b.first),a.second+b.second};}
int ls[M],rs[M],tsz,root;
void Set(int&c,int ss,int se,int qi,pii x){
	if(!c)c=++tsz;
	if(ss==se){st[c]=x;return;}
	int mid=ss+se>>1;
	if(qi<=mid)Set(ls[c],ss,mid,qi,x);
	else Set(rs[c],mid+1,se,qi,x);
	st[c]=st[ls[c]]+st[rs[c]];
}
pii Get(int c,int ss,int se,int qs,int qe){
	if(qs>qe||ss>qe||qs>se)return {N,0};
	if(qs<=ss&&qe>=se)return st[c];
	int mid=ss+se>>1;
	return Get(ls[c],ss,mid,qs,qe)+Get(rs[c],mid+1,se,qs,qe);
}
void Rem(int i){for(int j:nodes[i])up[j]=par[j][0];}
int ptr[N];
int Get(int col){
	while(ptr[col]<nodes[col].size()&&dep[Find(nodes[col][ptr[col]])]<dep[lca[col]])ptr[col]++;
	if(ptr[col]==nodes[col].size())return 0;
	else return c[Find(nodes[col][ptr[col]])];
}
int disc[N],low[N],tid,ans;
stack<int> stk;
bool in[N],has_out[N];
int val[N];
void AddHLD(int col){
	for(int i:nodes[col])Set(root,1,tme,lid[i],{disc[col],0});
}
void DelHLD(int col){
	for(int i:nodes[col])Set(root,1,tme,lid[i],{N,1});
}
pii GetHLD(int col){
	pii ans={N,0};
	for(int i:nodes[col]){
		for(int j=i;dep[j]>=dep[lca[col]];j=par[head[j]][0]){
			ans=ans+Get(root,1,tme,max(lid[head[j]],lid[lca[col]]),lid[j]);
		}
	}
	return ans;
}
void DFS(int u){
	Rem(u);
	disc[u]=low[u]=++tid;
	stk.push(u);in[u]=1;
	AddHLD(u);
	int v;
	while(v=Get(u)){
		DFS(v);
		low[u]=min(low[u],low[v]);
	}
	pii tmp=GetHLD(u);
	low[u]=min(low[u],tmp.first);
	has_out[u]=tmp.second>0;
	if(low[u]==disc[u]){
		bool ok=1;
		int sz=0;
		do{
			v=stk.top();
			stk.pop();
			in[v]=0;
			DelHLD(v);
			if(has_out[v])ok=0;
			sz++;
		}while(v!=u);
		if(ok)ans=min(ans,sz-1);
	}
}
int main(){
	int n,k;
	scanf("%i %i",&n,&k);
	for(int i=1;i<=n;i++)up[i]=i;
	for(int i=1,u,v;i<n;i++)scanf("%i %i",&u,&v),E[u].pb(v),E[v].pb(u);
	Tree(1,0);
	HLD(1,0);
	st[0]={N,0};
	for(int i=1;i<=n;i++)Set(root,1,n,i,{N,0});
	for(int i=1;i<=n;i++)scanf("%i",&c[i]),nodes[c[i]].pb(i);
	for(int i=1;i<=k;i++){
		lca[i]=nodes[i][0];
		for(int j:nodes[i])lca[i]=LCA(lca[i],j);
	}
	ans=n;
	for(int i=1;i<=k;i++)if(!disc[i])DFS(i);
	printf("%i\n",ans);
	return 0;
}

Compilation message

capital_city.cpp: In function 'void Set(int&, int, int, int, std::pair<int, int>)':
capital_city.cpp:40:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   40 |  int mid=ss+se>>1;
      |          ~~^~~
capital_city.cpp: In function 'std::pair<int, int> Get(int, int, int, int, int)':
capital_city.cpp:48:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   48 |  int mid=ss+se>>1;
      |          ~~^~~
capital_city.cpp: In function 'int Get(int)':
capital_city.cpp:54:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   54 |  while(ptr[col]<nodes[col].size()&&dep[Find(nodes[col][ptr[col]])]<dep[lca[col]])ptr[col]++;
      |        ~~~~~~~~^~~~~~~~~~~~~~~~~~
capital_city.cpp:55:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   55 |  if(ptr[col]==nodes[col].size())return 0;
      |     ~~~~~~~~^~~~~~~~~~~~~~~~~~~
capital_city.cpp: In function 'void DFS(int)':
capital_city.cpp:83:9: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
   83 |  while(v=Get(u)){
      |        ~^~~~~~~
capital_city.cpp: In function 'int main()':
capital_city.cpp:106:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  106 |  scanf("%i %i",&n,&k);
      |  ~~~~~^~~~~~~~~~~~~~~
capital_city.cpp:108:31: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  108 |  for(int i=1,u,v;i<n;i++)scanf("%i %i",&u,&v),E[u].pb(v),E[v].pb(u);
      |                          ~~~~~^~~~~~~~~~~~~~~
capital_city.cpp:113:28: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  113 |  for(int i=1;i<=n;i++)scanf("%i",&c[i]),nodes[c[i]].pb(i);
      |                       ~~~~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 8 ms 9856 KB Output is correct
2 Correct 7 ms 9856 KB Output is correct
3 Correct 7 ms 9856 KB Output is correct
4 Correct 7 ms 9856 KB Output is correct
5 Correct 7 ms 9856 KB Output is correct
6 Correct 9 ms 9856 KB Output is correct
7 Correct 7 ms 9856 KB Output is correct
8 Correct 7 ms 9856 KB Output is correct
9 Correct 7 ms 9856 KB Output is correct
10 Correct 8 ms 9856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 9856 KB Output is correct
2 Correct 7 ms 9856 KB Output is correct
3 Correct 7 ms 9856 KB Output is correct
4 Correct 7 ms 9856 KB Output is correct
5 Correct 7 ms 9856 KB Output is correct
6 Correct 9 ms 9856 KB Output is correct
7 Correct 7 ms 9856 KB Output is correct
8 Correct 7 ms 9856 KB Output is correct
9 Correct 7 ms 9856 KB Output is correct
10 Correct 8 ms 9856 KB Output is correct
11 Correct 12 ms 10240 KB Output is correct
12 Correct 11 ms 10240 KB Output is correct
13 Correct 11 ms 10240 KB Output is correct
14 Correct 11 ms 10368 KB Output is correct
15 Correct 10 ms 10240 KB Output is correct
16 Correct 11 ms 10240 KB Output is correct
17 Correct 10 ms 10240 KB Output is correct
18 Correct 11 ms 10368 KB Output is correct
19 Correct 10 ms 10368 KB Output is correct
20 Correct 10 ms 10240 KB Output is correct
21 Correct 10 ms 10240 KB Output is correct
22 Correct 10 ms 10368 KB Output is correct
23 Correct 12 ms 10368 KB Output is correct
24 Correct 10 ms 10240 KB Output is correct
25 Correct 10 ms 10240 KB Output is correct
26 Correct 10 ms 10368 KB Output is correct
27 Correct 10 ms 10368 KB Output is correct
28 Correct 10 ms 10240 KB Output is correct
29 Correct 10 ms 10240 KB Output is correct
30 Correct 10 ms 10240 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 891 ms 62332 KB Output is correct
2 Correct 410 ms 66040 KB Output is correct
3 Correct 879 ms 65400 KB Output is correct
4 Correct 403 ms 66296 KB Output is correct
5 Correct 869 ms 62332 KB Output is correct
6 Correct 411 ms 65988 KB Output is correct
7 Correct 830 ms 62584 KB Output is correct
8 Correct 405 ms 65396 KB Output is correct
9 Correct 770 ms 60152 KB Output is correct
10 Correct 730 ms 57680 KB Output is correct
11 Correct 746 ms 60536 KB Output is correct
12 Correct 768 ms 63224 KB Output is correct
13 Correct 763 ms 57080 KB Output is correct
14 Correct 780 ms 63608 KB Output is correct
15 Correct 768 ms 63352 KB Output is correct
16 Correct 745 ms 58232 KB Output is correct
17 Correct 736 ms 58744 KB Output is correct
18 Correct 745 ms 58976 KB Output is correct
19 Correct 738 ms 61944 KB Output is correct
20 Correct 729 ms 64376 KB Output is correct
21 Correct 7 ms 9856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 9856 KB Output is correct
2 Correct 7 ms 9856 KB Output is correct
3 Correct 7 ms 9856 KB Output is correct
4 Correct 7 ms 9856 KB Output is correct
5 Correct 7 ms 9856 KB Output is correct
6 Correct 9 ms 9856 KB Output is correct
7 Correct 7 ms 9856 KB Output is correct
8 Correct 7 ms 9856 KB Output is correct
9 Correct 7 ms 9856 KB Output is correct
10 Correct 8 ms 9856 KB Output is correct
11 Correct 12 ms 10240 KB Output is correct
12 Correct 11 ms 10240 KB Output is correct
13 Correct 11 ms 10240 KB Output is correct
14 Correct 11 ms 10368 KB Output is correct
15 Correct 10 ms 10240 KB Output is correct
16 Correct 11 ms 10240 KB Output is correct
17 Correct 10 ms 10240 KB Output is correct
18 Correct 11 ms 10368 KB Output is correct
19 Correct 10 ms 10368 KB Output is correct
20 Correct 10 ms 10240 KB Output is correct
21 Correct 10 ms 10240 KB Output is correct
22 Correct 10 ms 10368 KB Output is correct
23 Correct 12 ms 10368 KB Output is correct
24 Correct 10 ms 10240 KB Output is correct
25 Correct 10 ms 10240 KB Output is correct
26 Correct 10 ms 10368 KB Output is correct
27 Correct 10 ms 10368 KB Output is correct
28 Correct 10 ms 10240 KB Output is correct
29 Correct 10 ms 10240 KB Output is correct
30 Correct 10 ms 10240 KB Output is correct
31 Correct 891 ms 62332 KB Output is correct
32 Correct 410 ms 66040 KB Output is correct
33 Correct 879 ms 65400 KB Output is correct
34 Correct 403 ms 66296 KB Output is correct
35 Correct 869 ms 62332 KB Output is correct
36 Correct 411 ms 65988 KB Output is correct
37 Correct 830 ms 62584 KB Output is correct
38 Correct 405 ms 65396 KB Output is correct
39 Correct 770 ms 60152 KB Output is correct
40 Correct 730 ms 57680 KB Output is correct
41 Correct 746 ms 60536 KB Output is correct
42 Correct 768 ms 63224 KB Output is correct
43 Correct 763 ms 57080 KB Output is correct
44 Correct 780 ms 63608 KB Output is correct
45 Correct 768 ms 63352 KB Output is correct
46 Correct 745 ms 58232 KB Output is correct
47 Correct 736 ms 58744 KB Output is correct
48 Correct 745 ms 58976 KB Output is correct
49 Correct 738 ms 61944 KB Output is correct
50 Correct 729 ms 64376 KB Output is correct
51 Correct 7 ms 9856 KB Output is correct
52 Correct 1140 ms 48588 KB Output is correct
53 Correct 1135 ms 48760 KB Output is correct
54 Correct 1112 ms 48504 KB Output is correct
55 Correct 1124 ms 48632 KB Output is correct
56 Correct 1128 ms 48632 KB Output is correct
57 Correct 1113 ms 48760 KB Output is correct
58 Correct 757 ms 51960 KB Output is correct
59 Correct 770 ms 52600 KB Output is correct
60 Correct 930 ms 52088 KB Output is correct
61 Correct 935 ms 51704 KB Output is correct
62 Correct 397 ms 66040 KB Output is correct
63 Correct 399 ms 66040 KB Output is correct
64 Correct 400 ms 65912 KB Output is correct
65 Correct 403 ms 66168 KB Output is correct
66 Correct 652 ms 56964 KB Output is correct
67 Correct 660 ms 56704 KB Output is correct
68 Correct 641 ms 56832 KB Output is correct
69 Correct 637 ms 56732 KB Output is correct
70 Correct 631 ms 56816 KB Output is correct
71 Correct 622 ms 56560 KB Output is correct
72 Correct 622 ms 56688 KB Output is correct
73 Correct 641 ms 55920 KB Output is correct
74 Correct 643 ms 56688 KB Output is correct
75 Correct 670 ms 56716 KB Output is correct
76 Correct 763 ms 56184 KB Output is correct
77 Correct 735 ms 54488 KB Output is correct
78 Correct 767 ms 58616 KB Output is correct
79 Correct 740 ms 56660 KB Output is correct
80 Correct 743 ms 63992 KB Output is correct
81 Correct 744 ms 60152 KB Output is correct
82 Correct 763 ms 60660 KB Output is correct
83 Correct 752 ms 57088 KB Output is correct
84 Correct 761 ms 63096 KB Output is correct
85 Correct 743 ms 61304 KB Output is correct
86 Correct 717 ms 56696 KB Output is correct
87 Correct 744 ms 58488 KB Output is correct
88 Correct 770 ms 59708 KB Output is correct
89 Correct 782 ms 55544 KB Output is correct
90 Correct 747 ms 55416 KB Output is correct
91 Correct 761 ms 57852 KB Output is correct
92 Correct 796 ms 56440 KB Output is correct
93 Correct 781 ms 56056 KB Output is correct
94 Correct 778 ms 55416 KB Output is correct
95 Correct 758 ms 56824 KB Output is correct
96 Correct 781 ms 55672 KB Output is correct
97 Correct 767 ms 57720 KB Output is correct