QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#388810#8134. LCA CountingcjsWA 2ms13808kbC++142.2kb2024-04-13 20:21:472024-04-13 20:21:49

Judging History

你现在查看的是最新测评结果

  • [2024-04-13 20:21:49]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:13808kb
  • [2024-04-13 20:21:47]
  • 提交

answer

#include<bits/stdc++.h>
#define pb push_back
using namespace std;
int read(){
	int x=0,f=1;char c;
	while(!isdigit(c=getchar()))if(c=='-')f=-1;
	while(isdigit(c))x=(x<<1)+(x<<3)+(c&15),c=getchar();
	return x*f;
}
const int N=2e5+10;
int n,fa[N],dfn[N],siz[N],num,ans[N],l[N],r[N];
int f[N];
bool c[N],vis[N];
vector<int>e[N],vec,buc0,buc1;
void dfs(int x){
	dfn[x]=++num,siz[x]=1;
	if(!(int)e[x].size())vec.pb(x);
	for(int y:e[x]){
		dfs(y);
		siz[x]+=siz[y];
	}
	l[x]=dfn[x],r[x]=dfn[x]+siz[x]-1;
}
void add(int x){
	for(;x<=n;x+=x&-x)c[x]|=1;
}
bool ask(int x){
	bool res=0;
	for(;x&&!res;x-=x&-x)res|=c[x];
	return res;
}
int calc(int x,int son){
	if(vis[x])return 0;
	bool flag=0;
	for(int y:e[x])if(y!=son){
		flag|=ask(r[y])-ask(l[y]-1);
		if(flag)break;
	}
	if(flag)return x;
	return calc(fa[x],x);
}
void dfs2(int x,int v){
	if(!(int)e[x].size()){
		f[x]=v;
		buc1.pb(x);
	}
	for(int y:e[x])dfs2(y,v);
}
int find(int x){
	if(vis[x])return x;
	return find(fa[x]);
}
void dfs3(int x,int p){
	if(!(int)e[x].size()){
		int y=calc(x,0);
		if(y)f[x]=y;
		else {
			f[x]=0;
			buc0.pb(x);
		}
	}
	for(int y:e[x])dfs3(y,p);
}
int main()
{
	n=read();
	for(int i=2;i<=n;i++){
		fa[i]=read();
		e[fa[i]].pb(i);
	}
	dfs(1);
	int leaf=(int)vec.size();
	ans[1]=0,ans[2]=1;
	vis[1]=1;
	add(vec[0]);
	add(vec.back());
	for(int j=1;j<leaf-1;j++){
		int i=vec[j];
		int x=calc(i,0);
		if(x){
			f[i]=x;
			buc1.pb(i);
		}
		else {
			buc0.pb(i);
		}
	}
	for(int i=3;i<=leaf;i++){
		while(buc1.size()&&!f[buc1.back()])buc1.pop_back();
		if((int)buc1.size()){
			int x=buc1.back();
			buc1.pop_back();
			int p=f[x];
			vis[p]=1;
			ans[i]=ans[i-1]+1;
			while(fa[x]!=p){
				int son=x;
				x=fa[x];
				for(int y:e[x])if(y!=son){
					dfs2(y,x);
				}
			}
			add(x);
			dfs3(p,p);
		}
		else {
			while(buc0.size()&&f[buc0.back()])buc0.pop_back();
			ans[i]=ans[i-1];
			int x=buc0.back();
			buc0.pop_back();
			int p=find(x);
			while(fa[x]!=p){
				int son=x;
				x=fa[x];
				for(int y:e[x])if(y!=son){
					dfs2(y,x);
				}
			}
			add(x);
		}
	}
	for(int i=1;i<=leaf;i++)printf("%d ",ans[i]+i);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 13808kb

input:

7
1 1 2 4 2 2

output:

1 3 5 6 

result:

ok 4 number(s): "1 3 5 6"

Test #2:

score: -100
Wrong Answer
time: 2ms
memory: 13732kb

input:

10
1 1 2 2 1 1 1 2 4

output:

1 3 4 5 6 7 9 

result:

wrong answer 3rd numbers differ - expected: '5', found: '4'