QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#288373#5017. 相等树链yyyyxh0 105ms155044kbC++235.3kb2023-12-22 16:08:262023-12-22 16:08:26

Judging History

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

  • [2023-12-22 16:08:26]
  • 评测
  • 测评结果:0
  • 用时:105ms
  • 内存:155044kb
  • [2023-12-22 16:08:26]
  • 提交

answer

#include <cstdio>
#include <algorithm>
#define IL inline
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/hash_policy.hpp>
using namespace std;
int read(){
	char c=getchar();int x=0;
	while(c<48||c>57) c=getchar();
	do x=(x<<1)+(x<<3)+(c^48),c=getchar();
	while(c>=48&&c<=57);
	return x;
}
typedef unsigned long long ull;
typedef long long ll;
const int N=200003;
int n;
ll res,tmp;
ull seed=20070329ull;
ull rng(){
	seed^=seed<<7;
	seed^=seed>>13;
	seed^=seed<<19;
	return seed;
}
ull a[N];
int lis[N],len;
namespace VT{
	int hd[N],ver[N<<1],nxt[N<<1],tot;
	IL void add(int u,int v){nxt[++tot]=hd[u];hd[u]=tot;ver[tot]=v;}
}
int seq[N],rk;
namespace T2{
	int hd[N],ver[N],nxt[N],tot;
	IL void add(int u,int v){nxt[++tot]=hd[u];hd[u]=tot;ver[tot]=v;}
	int dfn[N],f[18][N],num;
	ull da[N];int de[N];
	int cmp(int x,int y){
		if(de[x]<de[y]) return x;
		return y;
	}
	int ft[N];
	void dfs(int u){
		da[u]^=a[u];
		f[0][dfn[u]=++num]=ft[u];
		for(int i=hd[u];i;i=nxt[i]){
			int v=ver[i];
			ft[v]=u;
			de[v]=de[u]+1;
			da[v]^=da[u];
			dfs(v);
		}
	}
	int lca(int u,int v){
		if(u==v) return u;
		if((u=dfn[u])>(v=dfn[v])) swap(u,v);
		int k=__lg(v-u++);
		return cmp(f[k][u],f[k][v-(1<<k)+1]);
	}
	IL int dis(int u,int v){return de[u]+de[v]-2*de[lca(u,v)];}
	IL ull calc(int u,int v){return da[u]^da[v]^a[lca(u,v)];}
	IL bool jdg(int u,int v,int x){return dis(u,x)+dis(v,x)==dis(u,v);}
	IL void init(){
		dfs(1);
		for(int t=1;t<18;++t)
			for(int i=1;i+(1<<t)-1<=n;++i)
				f[t][i]=cmp(f[t-1][i],f[t-1][i+(1<<(t-1))]);
	}
	int stk[N],tp;
	void build(){
		sort(seq+1,seq+rk+1,[](int x,int y){return dfn[x]<dfn[y];});
		lis[len=1]=stk[tp=1]=seq[1];
		for(int i=2;i<=rk;++i){
			int x=seq[i],las=0;
			int w=lca(stk[tp],x);
			while(tp&&de[w]<de[stk[tp]]){
				if(las) VT::add(stk[tp],las);
				las=stk[tp--];
			}
			if(w!=stk[tp]) lis[++len]=stk[++tp]=w;
			if(las) VT::add(w,las);
			lis[++len]=stk[++tp]=x;
		}
		while(tp>1) VT::add(stk[tp-1],stk[tp]),--tp;
	}
	struct path{
		int u,v;ull w;
		path(int U=0,int V=0,int W=0):u(U),v(V),w(W){}
		void operator+=(int x){
			if(u<0&&v<0) return;
			if(!w&&!u&&!v){u=v=x;w=a[x];return;}
			w^=a[x];
			if(jdg(u,v,x)) return;
			if(jdg(u,x,v)){v=x;return;}
			if(jdg(x,v,u)){u=x;return;}
			u=v=-1;w=0;
		}
		IL ull way(){return calc(u,v);}
		IL bool check(){
			if(!u&&!v) return 1;
			if(u<0&&v<0) return 0;
			return way()==w;
		}
	};
}
namespace VT{
	T2::path p1[N],p2[N];
	__gnu_pbds::gp_hash_table<ull,int> f1[N],f2[N];
	vector<ull> v1[N],v2[N];
	void dfs(int u){
		f1[u].clear();f2[u].clear();
		for(ull x:v1[u]) ++f1[u][x^T2::da[u]];
		for(ull x:v2[u]) ++f2[u][x^T2::da[u]];
		for(int i=hd[u];i;i=nxt[i]){
			int v=ver[i];
			dfs(v);
			if(f1[u].size()+f2[u].size()<f1[v].size()+f2[v].size()){
				f1[u].swap(f1[v]);f2[u].swap(f2[v]);
			}
			for(auto [x,t]:f1[v]) tmp+=(ll)f2[u][x^a[u]]*t;
			for(auto [x,t]:f2[v]) tmp+=(ll)f1[u][x^a[u]]*t;
			for(auto [x,t]:f1[v]) f1[u][x]+=t;
			for(auto [x,t]:f2[v]) f2[u][x]+=t;
			f1[v].clear();f2[v].clear();
		}
	}
	void solve(int cur){
		__gnu_pbds::gp_hash_table<ull,int> mp1,mp2;
		tmp=0;
		for(int i=1;i<=rk;++i){
			int x=seq[i];
			if(p1[x].u!=p1[x].v) ++mp1[p1[x].w^p1[x].way()];
			if(p2[x].u!=p2[x].v) ++mp2[p2[x].w^p2[x].way()];
		}
		for(int i=1;i<=rk;++i){
			int x=seq[i];
			if(p1[x].u>0&&p1[x].v>0){
				tmp+=mp2[p1[x].w];
				v1[p1[x].u].emplace_back(p1[x].w);
				if(p1[x].u!=p1[x].v) v1[p1[x].v].emplace_back(p1[x].w);
			}
			if(p2[x].u>0&&p2[x].v>0){
				tmp+=mp1[p2[x].w];
				v2[p2[x].u].emplace_back(p2[x].w);
				if(p2[x].u!=p2[x].v) v2[p2[x].v].emplace_back(p2[x].w);
			}
		}
		seq[++rk]=cur;
		T2::build();
		int rt=T2::stk[T2::tp];
		dfs(rt);
		for(int i=1;i<=len;++i){int x=lis[i];hd[x]=0;v1[x].clear();v2[x].clear();}
		rk=len=0;
		while(tot) ver[tot]=nxt[tot]=0,--tot;
		tmp>>=1;
	}
}
namespace T1{
	int hd[N],ver[N<<1],nxt[N<<1],tot;
	IL void add(int u,int v){nxt[++tot]=hd[u];hd[u]=tot;ver[tot]=v;}
	int sz[N],sn[N];
	bool del[N];
	void getsiz(int u,int fa){
		sz[u]=1;sn[u]=0;
		for(int i=hd[u];i;i=nxt[i]){
			int v=ver[i];
			if(v==fa||del[v]) continue;
			getsiz(v,u);
			sz[u]+=sz[v];
			if(sz[sn[u]]<sz[v]) sn[u]=v;
		}
	}
	void dfs(int u,int fa,T2::path cur,T2::path curs){
		cur+=u;
		if(fa) curs+=u,seq[++rk]=u;
		VT::p1[u]=cur;
		VT::p2[u]=curs;
		sz[u]=1;
		if(cur.check()) ++res;
		for(int i=hd[u];i;i=nxt[i]){
			int v=ver[i];
			if(v==fa||del[v]) continue;
			dfs(v,u,cur,curs);
			sz[u]+=sz[v];
		}
	}
	void trav(int u,int fa){
		seq[++rk]=u;
		for(int i=hd[u];i;i=nxt[i]){
			int v=ver[i];
			if(v==fa||del[v]) continue;
			trav(v,u);
		}
	}
	void proc(int u,int all){
		getsiz(u,0);
		while(sz[sn[u]]*2>all) u=sn[u];
		del[u]=1;dfs(u,0,T2::path(),T2::path());
		VT::solve(u);res+=tmp;
		for(int i=hd[u];i;i=nxt[i]){
			int v=ver[i];
			if(del[v]) continue;
			trav(v,u);
			VT::solve(u);res-=tmp;
		}
		for(int i=hd[u];i;i=nxt[i]){
			int v=ver[i];
			if(del[v]) continue;
			proc(v,sz[v]);
		}
	}
}
int main(){
	n=read();
	for(int i=2;i<=n;++i){int x=read();T1::add(i,x);T1::add(x,i);}
	for(int i=2;i<=n;++i) T2::add(read(),i);
	for(int i=1;i<=n;++i) a[i]=rng();
	T2::init();
	T1::proc(1,n);
	printf("%lld\n",res);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 105ms
memory: 155044kb

input:

5000
1296 1400 867 4533 1296 2007 2059 115 821 2960 3187 1597 2409 2708 4743 4778 1345 3967 911 3400 4249 3793 339 3145 3490 607 4148 3513 3264 3852 568 775 828 1348 423 3678 305 1938 1096 1373 2662 1048 4328 4208 203 779 3103 3372 4523 192 264 792 4943 2211 2494 3513 3555 4935 3277 3390 4624 128 18...

output:

76029

result:

wrong answer 1st numbers differ - expected: '76002', found: '76029'

Subtask #2:

score: 0
Time Limit Exceeded

Test #6:

score: 0
Time Limit Exceeded

input:

200000
13177 40498 104798 83659 186055 32788 86489 72123 13521 134868 158968 60124 166316 163390 120935 103000 83938 57807 97940 40447 137723 154683 191864 59080 102808 3969 21451 165592 128776 49468 4101 26441 139317 59503 18053 118809 187783 149816 21609 98521 165692 52964 60425 23437 29614 106886...

output:


result:


Subtask #3:

score: 0
Skipped

Dependency #2:

0%

Subtask #4:

score: 0
Skipped

Dependency #1:

0%