QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#308334#5017. 相等树链________a____0 190ms120644kbC++144.5kb2024-01-19 22:18:482024-01-19 22:18:48

Judging History

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

  • [2024-01-19 22:18:48]
  • 评测
  • 测评结果:0
  • 用时:190ms
  • 内存:120644kb
  • [2024-01-19 22:18:48]
  • 提交

answer

#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#include <ext/pb_ds/priority_queue.hpp>
using namespace std;
using namespace __gnu_pbds;
#define MAXN 200010
#define ull unsigned long long
mt19937_64 rd(1);
vector<int> ver1[MAXN],ver2[MAXN];
int fa2[MAXN];
int dep[MAXN];
int dfn[MAXN];
int ST[MAXN][19];
ull XOR[MAXN],rw[MAXN];
int R[MAXN];
void dfs2(int u){
	static int tm_=0;
	dfn[u]=++tm_;
	ST[dfn[u]][0]=u,dep[u]=dep[fa2[u]]+1;
	rw[u]=rd();
	//rw[u]=pow(2,u-1);
	XOR[u]=rw[u]^XOR[fa2[u]];
	for(int v:ver2[u]) dfs2(v);
	R[u]=tm_;
}
inline int lca(int u,int v){
	if(u==v) return v;
	if(dfn[u]>dfn[v]) swap(u,v);
	u=dfn[u]+1,v=dfn[v];
	int len=v-u+1;
	return fa2[(dep[ST[u][__lg(len)]]<dep[ST[v-(1<<__lg(len))+1][__lg(len)]])?ST[u][__lg(len)]:ST[v-(1<<__lg(len))+1][__lg(len)]];
}
int n;
int siz[MAXN],vis[MAXN],mx[MAXN];
int ALL,rt,nowmx;
void find(int u,int fa){
	siz[u]=1;
	mx[u]=0;
	for(int v:ver1[u]){
		if(vis[v]||v==fa) continue ;
		find(v,u);
		siz[u]+=siz[v];
		mx[u]=max(mx[u],siz[v]);
	}
	mx[u]=max(mx[u],ALL-siz[u]);
	if(nowmx<mx[u]) rt=u,nowmx=mx[u];
}
inline int getdis(int u,int v){
	return dep[u]+dep[v]-(dep[lca(u,v)]<<1);
}
vector<int> id;
void get(int u,int fa){id.push_back(u); for(int v:ver1[u]) if(!vis[v]&&v!=fa) get(v,u);}
gp_hash_table<ull,int> S1,S2,W[MAXN],WW;
int bl[MAXN];
int st[MAXN],top;
int w[MAXN],z[MAXN],len[MAXN];
ull W1[MAXN],W2[MAXN],W3[MAXN],W4[MAXN];
int ans;
bitset<7> OUT;
inline void out(ull ww){
	OUT=ww;
	cout<<OUT<<endl;
}
inline ull getx(int u){
	return XOR[u]^XOR[rt]^(XOR[lca(u,rt)]^XOR[fa2[lca(u,rt)]]);
}
void get2(int u,int fa,ull val){
	st[++top]=u;
	if(fa==0) w[u]=u,len[u]=getdis(w[u],z[u]);
	else {
		int len2=getdis(w[fa],u),len3=getdis(z[fa],u);
		//if(u==1) cout<<w[fa]<<' '<<z[fa]<<' '<<len[fa]<<' '<<len2<<' '<<len3<<endl;
		//if(u==6) cout<<lca(1,6)<<endl;
		if(len2>len[fa]) 
			if(len2>len3) len[u]=len2,w[u]=w[fa],z[u]=u;
			else len[u]=len3,z[u]=z[fa],w[u]=u;
		else
			if(len[fa]>len3) len[u]=len[fa],w[u]=w[fa],z[u]=z[fa];
			else len[u]=len3,z[u]=z[fa],w[u]=u;
	}
	val^=XOR[u]^XOR[fa2[u]];
	W1[u]=val^XOR[w[u]]^XOR[z[u]]^(XOR[lca(w[u],z[u])]^XOR[fa2[lca(w[u],z[u])]]);
	W4[u]=val^(XOR[rt]^XOR[fa2[rt]]);
	//cout<<u<<' '<<S1[val]<<' '<<S2[W1[u]]<<endl;
	//cout<<u<<' '<<w[u]<<' '<<z[u]<<' '<<endl;
	//cout<<'+'; out(W1[u]);
	//cout<<'-'; out(W4[u]);
	//cout<<u<<' '<<ans<<endl;
	ans+=S1[W4[u]];
	ans+=S2[W1[u]];
	//cout<<u<<' '<<ans<<endl;
	if(w[u]!=rt) {
		//if(u==4) out(getx(w[u])),out(val);
		W2[u]=val^getx(w[u]),ans+=WW[W2[u]]-W[bl[w[u]]][W2[u]];
		//cout<<WW[W2[u]]<<endl;
	}
	if(w[u]!=z[u]&&z[u]!=rt) W3[u]=val^getx(z[u]),ans+=WW[W3[u]]-W[bl[z[u]]][W3[u]];
	for(int v:ver1[u]) if(!vis[v]&&v!=fa) get2(v,u,val);
}
inline void init(int u){
	for(int v:id) bl[v]=0;
	id.clear(),W[0].clear();
	for(int v:ver2[u]) W[v].clear();
	WW.clear(),S1.clear(),S2.clear();
}
void solve(int u,int DEP){
	if(DEP>50) exit(1);
	//cerr<<u<<endl;
	//if(vis[u]) cerr<<1<<endl;
	vis[u]=1;
	get(u,0);
	sort(id.begin(),id.end(),[](int _a,int _b){return dfn[_a]<dfn[_b];});
	int las=0;
	auto P=id.begin();
	for(int v:ver2[u]){
		while(P!=id.end()&&dfn[*P]<dfn[v]) ++P;
		while(P!=id.end()&&dfn[*P]<=R[v]) bl[*P]=v,++P;
	}
	rt=u;
	S2[0]=1;
	//out(XOR[u]^XOR[fa2[u]]);
	for(int v:ver1[u]){
		//cout<<v<<' ';
		if(vis[v]) continue ;
		z[v]=u;
		top=0;
		get2(v,0,XOR[u]^XOR[fa2[u]]);
		for(int i=1,p;i<=top;i++) {
			p=st[i];
			S1[W1[p]]++;
			S2[W4[p]]++;
			if(w[p]!=rt) W[bl[w[p]]][W2[p]]++,WW[W2[p]]++;
			if(w[p]!=z[p]&&z[p]!=rt) W[bl[z[p]]][W3[p]]++,WW[W3[p]]++;
		}
	}
	//return ;
	init(u);
	for(int v:ver1[u]) {
		if(vis[v]) continue ;
		ALL=siz[v],rt=v,nowmx=INT_MAX;
		find(v,0);
		solve(rt,DEP+1);
	}
}
signed main(){
//	freopen("test.in","r",stdin);
//	freopen("test.out","w",stdout);
	scanf("%d",&n);
	for(int i=2,u;i<=n;i++) {
		scanf("%d",&u),ver1[u].push_back(i),ver1[i].push_back(u);
		//cout<<u<<' '<<i<<endl;
	}
	for(int i=2;i<=n;i++) {
		scanf("%d",&fa2[i]),ver2[fa2[i]].push_back(i);
		//cout<<fa2[i]<<' '<<i<<endl;
	}
	dfs2(1);
	for(int k=1;k<=18;k++)
		for(int i=1;i+(1<<k)-1<=n;i++) ST[i][k]=dep[ST[i][k-1]]<dep[ST[i+(1<<k-1)][k-1]]?ST[i][k-1]:ST[i+(1<<k-1)][k-1];
	ALL=n,nowmx=n,rt=0;
	find(1,0);
	//cout<<rt<<endl;
	solve(rt,0);
	//cout<<ans<<endl;
	printf("%d",ans+n);
	//cerr<<clock()<<endl;
	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: 19ms
memory: 85280kb

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:

5000

result:

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

Subtask #2:

score: 0
Wrong Answer

Test #6:

score: 0
Wrong Answer
time: 190ms
memory: 120644kb

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:

200000

result:

wrong answer 1st numbers differ - expected: '5859368', found: '200000'

Subtask #3:

score: 0
Skipped

Dependency #2:

0%

Subtask #4:

score: 0
Skipped

Dependency #1:

0%