QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#876684#5017. 相等树链Mirasycle0 320ms98064kbC++143.3kb2025-01-31 11:06:102025-01-31 11:06:11

Judging History

This is the latest submission verdict.

  • [2025-01-31 11:06:11]
  • Judged
  • Verdict: 0
  • Time: 320ms
  • Memory: 98064kb
  • [2025-01-31 11:06:10]
  • Submitted

answer

#include<bits/stdc++.h>
#define pb emplace_back
#define fi first
#define se second
#define mp make_pair
using namespace std;
typedef long long ll;
const int maxn=2e5+10;
const int maxlog=20;
void cmax(int &x,int y){ x=x>y?x:y; }
void cmin(int &x,int y){ x=x<y?x:y; }
ll val[maxn],ans=0; int n,vis[maxn];
int sz[maxn],col[maxn],S,C,cur;
ll F(ll x){ return 143*x*x*x+(x>>1)+(x*x^343243); }
ll Random(){ return F(rand())+(rand()<<3)+(rand()>>1); }
struct Tree{
	int dfn[maxn],pa[maxlog][maxn];
	int lg[maxn],dep[maxn],tot=0; ll s[maxn];
	vector<int> G[maxn];
	void dfs(int u,int fa){
		pa[0][dfn[u]=++tot]=fa; dep[u]=dep[fa]+1;
		for(auto v:G[u]) 
			if(v!=fa) dfs(v,u);
	}
	int cmp(int x,int y){ return dfn[x]<dfn[y]?x:y; }
	int lca(int u,int v){
		if(u==v) return u;
		u=dfn[u]; v=dfn[v];
		if(u>v) swap(u,v);
		u++; int k=lg[v-u+1];
		return cmp(pa[k][u],pa[k][v-(1<<k)+1]);
	}
	void init(){
		dfs(1,0); lg[1]=0;
		for(int i=2;i<=n;i++) lg[i]=lg[i>>1]+1;
		for(int k=1;(1<<k)<=n;k++)
			for(int i=1;i+(1<<k)-1<=n;i++)
				pa[k][i]=cmp(pa[k-1][i],pa[k-1][i+(1<<k-1)]);
	}
	bool chk(int u,int v,int w){
		cur=v;
		if(dep[v]>dep[u]) swap(v,u);
		if(dep[v]>dep[w]) swap(v,w);
		if(dep[u]<dep[w]) swap(u,w); // u最深,v最浅
		int luw=lca(u,w),luv=lca(u,v),lvw=lca(v,w);
		//case 1: u-w-v
		if(luw==w&&lvw==v) return (w==cur);
		//case 2: u-v-w
		if(dep[v]<dep[luw]||(luv!=v&&lvw!=v)) return 0;
		return (v==cur);	
	}
	void calc(int u,int fa,int Col){
		s[u]=s[fa]^val[u]; if(!Col) Col=u; col[u]=Col;
		for(auto v:G[u])
			if(v!=fa&&!vis[v]) calc(v,u,Col);
	}
	ll get(int u,int v){ if(!u) return 0; return s[u]^s[v]; }
}t1,t2;
vector<int> G[maxn],E[maxn];
map<ll,int> sx,sy,s;
vector<ll> vx,vy,vec;
int calc(int u,int fa){
	sz[u]=1; int mx=0;
	for(auto v:G[u])
		if(v!=fa&&!vis[v]) sz[u]+=calc(v,u),cmax(mx,sz[v]);
	cmax(mx,S-sz[u]); if(mx<=S/2) C=u;
	return sz[u];
}
int find(int u){ S=calc(u,0); S=calc(u,0); return C; }
void dfs(int u,int fa,int p1,int p2){
	if(!p1) p1=u;
	else if(t2.chk(u,p1,C)) p1=u;
	else if(col[u]==col[p1]) return ;
	else if(!p2) p2=u;
	else if(t2.chk(u,p2,C)) p2=u; 
	else return ;
	ll Sx=t1.get(u,C);
	ll S=t2.get(p1,C)^t2.get(p2,C);// path(p1,p2)\{u}
	vx.pb(Sx^S); vy.pb(t1.get(u,C));
	vec.pb(Sx^t2.get(p1,C));
	if(p2) vec.pb(Sx^t2.get(p2,C)); 
	for(auto v:G[u]){
		if(v==fa||vis[v]) continue;
		dfs(v,u,p1,p2);
	}
}
void solve(int u){
//	cout<<"U"<<u<<endl;
	t1.calc(u,0,0); t2.calc(u,0,0);//计算 s 数组 
	vis[u]=1; sx.clear(); sy.clear(); s.clear();
	sy[0]=1;
	for(auto v:G[u]){
		if(vis[v]) continue;
		vec.clear(); vx.clear(); vy.clear();
		dfs(v,u,0,0);
		for(auto z:vx)
			if(sy.find(z)!=sy.end()) ans+=sy[z];
		for(auto z:vy)
			if(sx.find(z)!=sx.end()) ans+=sx[z];
		for(auto z:vec) 
			if(s.find(z)!=s.end()) ans+=s[z];
		for(auto z:vx) sx[z]++;
		for(auto z:vy) sy[z]++;
		for(auto z:vec) s[z]++;
	}
	for(auto v:G[u])
		if(!vis[v]) solve(find(v));
}
int main(){
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	srand((unsigned)time(0));
	cin>>n; int F;
	for(int i=2;i<=n;i++) cin>>F,G[F].pb(i);
	for(int i=2;i<=n;i++) cin>>F,E[F].pb(i);
	for(int i=1;i<=n;i++) val[i]=Random();
	for(int i=1;i<=n;i++) t1.G[i]=G[i],t2.G[i]=E[i];
	t1.init(); t2.init();
	solve(find(1)); cout<<ans+n; 
	return 0;
}

详细

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 4ms
memory: 55076kb

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:

6097

result:

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

Subtask #2:

score: 0
Wrong Answer

Test #6:

score: 0
Wrong Answer
time: 320ms
memory: 98064kb

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:

200002

result:

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

Subtask #3:

score: 0
Skipped

Dependency #2:

0%

Subtask #4:

score: 0
Skipped

Dependency #1:

0%