QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#99300 | #5017. 相等树链 | 275307894a | 0 | 876ms | 19676kb | C++14 | 3.1kb | 2023-04-21 21:06:28 | 2023-04-21 21:06:31 |
Judging History
answer
#include<bits/stdc++.h>
#define Gc() getchar()
#define Me(x,y) memset(x,y,sizeof(x))
#define Mc(x,y) memcpy(x,y,sizeof(x))
#define d(x,y) ((m)*(x-1)+(y))
#define R(n) (rnd()%(n)+1)
#define Pc(x) putchar(x)
#define LB lower_bound
#define UB upper_bound
#define PB push_back
#define fi first
#define se second
using ll=long long;using db=double;using lb=long db;using ui=unsigned;using ull=unsigned long long;using LL=__int128;
using namespace std;const int N=2e5+5,M=N*N+5,K=600,mod=998244353,Mod=mod-1;const db eps=1e-9;const int INF=1e9+7;mt19937 rnd(263082);
int n,m,k,x,y,z,tg[N],col[N];vector<int> S1[N],S2[N];ll Ans;
int vis[N],dp[N],Si[N],Ct,Rt,Bg[N],En[N],Bh;
void GI(int x,int La){Bg[x]=++Ct;tg[x]=Si[x]=1;dp[x]=0;for(int i:S1[x]) if(i^La&&!vis[i]) GI(i,x),Si[x]+=Si[i];En[x]=Ct;}
void DP(int x,int La){dp[x]=Ct-Si[x];for(int i:S1[x]) if(i^La&&!vis[i]) DP(i,x),dp[x]=max(dp[x],Si[i]);if(dp[x]<dp[Rt]) Rt=x;}
ull rd[N],Sum[N];int d[N];
void Make(int x,int La,int t,ull w){
if(!tg[x]) return;
w^=rd[x];Sum[x]=w;d[x]=d[La]+1;col[x]=t;
for(int i:S2[x]) if(i^La) Make(i,x,t,w);
}
void Cl(int x,int La){tg[x]=0;for(int i:S1[x]) if(i^La&&!vis[i]) Cl(i,x);}
map<pair<ull,ull>,int> f1,f2,f3;
int CKI(int x,int y){return Bg[y]>=Bg[x]&&Bg[y]<=En[x];}
void GA(int x,int La,int c1,ull s1,int d1,int c2,ull s2,int d2){
if(!c1) c1=col[x],s1=rd[x],d1=x;
else if(c1==col[x]){
s1^=rd[x];if(CKI(d1,x)) d1=x;else if(!CKI(x,d1)) return;
}else if(!c2) c2=col[x],s2=rd[x],d2=x;
else if(c2==col[x]){
s2^=rd[x];if(CKI(d2,x)) d2=x;else if(!CKI(x,d2)) return;
}else return;
pair<ull,ull> p1={Sum[d1]^s1,Sum[d2]^s2},p2={Sum[d1]^s1,s2},p3={s1,Sum[d2]^s2},p4={s1,s2};
if(p1.fi>p1.se) swap(p1.fi,p1.se);if(p2.fi>p2.se) swap(p2.fi,p2.se);if(p3.fi>p3.se) swap(p3.fi,p3.se);if(p4.fi>p4.se) swap(p4.fi,p4.se);
Ans+=f3[p2]+f3[p3];if(c1&&c2) Ans+=f1[p1]+f2[p4];
for(int i:S1[x]) if(!vis[i]&&i^La) GA(i,x,c1,s1,d1,c2,s2,d2);
}
void Ins(int x,int La,int c1,ull s1,int d1,int c2,ull s2,int d2){
if(!c1) c1=col[x],s1=rd[x],d1=x;
else if(c1==col[x]){
s1^=rd[x];if(CKI(d1,x)) d1=x;else if(!CKI(x,d1)) return;
}else if(!c2) c2=col[x],s2=rd[x],d2=x;
else if(c2==col[x]){
s2^=rd[x];if(CKI(d2,x)) d2=x;else if(!CKI(x,d2)) return;
}else return;
pair<ull,ull> p1={Sum[d1]^s1,Sum[d2]^s2},p2={Sum[d1]^s1,s2},p3={s1,Sum[d2]^s2},p4={s1,s2};
if(p1.fi>p1.se) swap(p1.fi,p1.se);if(p2.fi>p2.se) swap(p2.fi,p2.se);if(p3.fi>p3.se) swap(p3.fi,p3.se);if(p4.fi>p4.se) swap(p4.fi,p4.se);
if(c1&&c2)f1[p4]++,f2[p1]++;f3[p2]++;f3[p3]++;
for(int i:S1[x]) if(!vis[i]&&i^La) Ins(i,x,c1,s1,d1,c2,s2,d2);
}
void dfs(int x){
GI(x,0);dp[Rt=0]=INF;DP(x,0);vis[x=Rt]=1;
d[x]=Sum[x]=0;for(int i:S2[x]) Make(i,x,i,0);
Ans++;f1.clear();f2.clear();f3.clear();f1[{0,0}]=1;f3[{0,0}]=1;
for(int i:S1[x]) if(!vis[i]) GA(i,x,0,0,0,0,0,0),Ins(i,x,0,0,0,0,0,0);
for(int i:S1[x]) if(!vis[i]) Cl(i,x);
for(int i:S1[x]) if(!vis[i]) dfs(i);
}
int main(){
int i,j;scanf("%d",&n);for(i=2;i<=n;i++) scanf("%d",&x),S1[x].PB(i),S1[i].PB(x);
for(i=2;i<=n;i++) scanf("%d",&x),S2[x].PB(i),S2[i].PB(x);
for(i=1;i<=n;i++) rd[i]=((ull)rnd())<<32|rnd();dfs(1);
printf("%lld\n",Ans);
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 0
Wrong Answer
time: 876ms
memory: 19676kb
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:
45540
result:
wrong answer 1st numbers differ - expected: '76002', found: '45540'
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%