QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#851279 | #5017. 相等树链 | Zi_Gao | 10 | 5613ms | 214268kb | C++23 | 4.8kb | 2025-01-10 17:12:49 | 2025-01-10 17:12:49 |
Judging History
answer
#include<bits/stdc++.h>
using u32=unsigned int;
using i64=long long;
using u64=unsigned long long;
// #define ONLINE_JUDGE
#define INPUT_DATA_TYPE int
#define OUTPUT_DATA_TYPE i64
inline __attribute((always_inline)) INPUT_DATA_TYPE read(){register INPUT_DATA_TYPE x=0;register char f=0,c=getchar();while(c<'0'||'9'<c)f=(c=='-'),c=getchar();while('0'<=c&&c<='9')x=x*10+(c&15),c=getchar();return f?-x:x;}void print(OUTPUT_DATA_TYPE x){if(x<0)x=-x,putchar('-');if(x>9)print(x/10);putchar(x%10^48);return;}
#define il inline __attribute((always_inline))
std::vector<int> e[2][200010];
void addEdge(int u,int v,int op){
e[op][u].push_back(v);
e[op][v].push_back(u);
// printf("%d %d\n",u,v);
return;
}
namespace LCA{
#define ln(x) 31^__builtin_clz(x)
int cntDfn,dfn[200010],parsour[200010],depsour[200010];
#define ST_DATA_TYPE int
#define ST_TYPE >
const int ST_MAXI=200010;
const int ST_MAXJ=30;
ST_DATA_TYPE ST_CALC(ST_DATA_TYPE a,ST_DATA_TYPE b){return depsour[a]<depsour[b]?a:b;}
struct ST{
ST_DATA_TYPE f[ST_MAXJ][ST_MAXI];
void build(int size){
register int i,j;
int k=ln(size);
for(j=1;j<=k;++j)
for(i=1;i<=size-(1<<j)+1;++i)
f[j][i]=ST_CALC(f[j-1][i],f[j-1][i+(1<<(j-1))]);
return;
}
ST_DATA_TYPE query(int l,int r){
if(l==r) return l;
l=dfn[l];
r=dfn[r];
if(l>r) std::swap(l,r);
++l;
int k=ln(r-l+1);
int u=f[k][l];
int v=f[k][r-(1<<k)+1];
if(depsour[u]<depsour[v]) return parsour[u];
else return parsour[v];
}
}st;
void dfsLca(int u,int p){
depsour[u]=depsour[p]+1;
st.f[0][dfn[u]=++cntDfn]=u;
parsour[u]=p;
for(auto v:e[0][u])
if(v!=p)
dfsLca(v,u);
}
int getDis(int u,int v){return depsour[u]+depsour[v]-(depsour[st.query(u,v)]<<1);}
};
u64 val[200010];
i64 res;
char vis[200010],in2[200010];
int siz[200010],par[200010],incur[200010],sumsiz,root,n,id,siznow;
std::map<u64,int> cnt[3][3];
int getcent(int u,int p){
++sumsiz;
++siznow;
incur[u]=id;
register int siz=1,sizv,max=0;
for(auto v:e[0][u]) if(v!=p&&!vis[v]){
siz+=(sizv=getcent(v,u));
max=std::max(max,sizv);
}
max=std::max(max,n-siz);
if(max<=n/2) root=u;
return siz;
}
void dfspar(int u,int p){
++sumsiz;
siz[u]=1;
if(p==root) par[u]=0;
else par[u]=p;
for(auto v:e[0][u]) if(v!=p&&incur[v]==id) dfspar(v,u),siz[u]+=siz[v];
return;
}
// void dfs()
void solve(int t){
++id;
sumsiz+=n,root=-1;
register int i,j,u,root,nx,ny,t1,t2;
getcent(t,0);
root=::root;
dfspar(root,0);
for(i=0;i<3;++i) for(j=0;j<3;++j) cnt[i][j].clear();
cnt[1][0][0]=cnt[0][0][0]=1;
for(auto v:e[1][root]) if(incur[v]==id){
auto dfs=[&](int u,int p,int z,u64 now,int x,int y,int op,auto &&dfs) ->void {
++sumsiz;
now^=val[u]^val[par[u]],z+=(par[u]==0);
nx=x,ny=y;
if((t1=LCA::getDis(x,u))>(t2=LCA::getDis(nx,ny))) nx=x,ny=u,t2=t1;
if((t1=LCA::getDis(u,y))>t2) nx=u,ny=y,t2=t1;
std::vector<int> cur({x,y,u});
for(auto v:cur) if(LCA::getDis(v,nx)+LCA::getDis(v,ny)!=t2) return;
x=nx,y=ny;
if((~x)&&z<=2){
std::vector<std::pair<int,u64>> vec({{0,now},{1,now^val[x]}});
if(x^y) vec.push_back({2,now^val[x]^val[y]}),vec.push_back({1,now^val[y]});
if(op) for(auto [c,now]:vec) ++cnt[c][z][now];
else{
auto get=[&](int x,int y,u64 now){
for(i=0;i<=2;++i){
int z=x+i-y;
if(x+i>2||z<0||z>2) continue;
res+=cnt[i][z][now];
}
};
for(auto [c,now]:vec) get(c,z,now);
}
for(auto v:e[1][u]) if(v!=p&&incur[v]==id) dfs(v,u,z,now,x,y,op,dfs);
}
return;
};
dfs(v,root,0,0,v,v,0,dfs);
dfs(v,root,0,0,v,v,1,dfs);
}
vis[root]=1;
for(auto v:e[0][root]) if(!vis[v])
n=siz[v],solve(v);
return;
}
int main(){
// #ifndef ONLINE_JUDGE
// freopen("line/line2.in", "r", stdin);
// freopen("name.out", "w", stdout);
// #endif
std::mt19937_64 mt(time(0));
// int T=read();
int T=1;
while(T--){
register int i;
int n=read();
for(i=1;i<=n;++i) val[i]=mt();
for(i=2;i<=n;++i) addEdge(read(),i,0);
for(i=2;i<=n;++i) addEdge(read(),i,1);
LCA::cntDfn=0;
LCA::dfsLca(1,0);
LCA::st.build(n);
fprintf(stderr,"%lf\n",1.0*clock()/CLOCKS_PER_SEC);
::n=n,res=0,solve(1);
print(res+n),putchar('\n');
for(i=1;i<=n;++i) e[0][i].clear(),e[1][i].clear(),vis[i]=0;
}
fprintf(stderr,"siz: %d\n%lf\n",sumsiz,1.0*clock()/CLOCKS_PER_SEC);
#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
#endif
return 0;
}
/*
1
10
10 9 7 10 2 1 5 5 4
8 7 7 1 8 5 7 7 4
0 0 0 1
0 0 0 1
0 0 0 1
0 0 0 1
0 0 0 1
*/
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 10
Accepted
Test #1:
score: 10
Accepted
time: 16ms
memory: 20484kb
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:
76002
result:
ok 1 number(s): "76002"
Test #2:
score: 10
Accepted
time: 24ms
memory: 18432kb
input:
5000 1820 281 610 3735 3580 3994 2060 2424 3338 2859 281 532 1286 1771 825 3738 3793 2260 556 4068 3793 4169 4411 4941 122 4270 4711 524 4037 2508 50 3343 2030 1151 4002 533 2994 1440 1762 3851 3050 4470 555 1979 3178 3933 3793 281 4810 520 3793 3535 2526 4422 2859 1561 1544 649 4544 2882 1236 2749 ...
output:
604316
result:
ok 1 number(s): "604316"
Test #3:
score: 10
Accepted
time: 30ms
memory: 20396kb
input:
5000 4333 51 707 3055 3433 1451 1305 1431 3081 302 1633 88 2024 441 120 4650 3927 4970 2578 3170 4245 4204 2102 4954 3140 1039 360 3173 4203 4927 4437 4337 4502 1712 3598 2968 2 4884 1260 1768 585 1815 4346 2938 4638 4886 4482 1095 1452 298 2702 2257 1375 2819 4482 711 220 396 3907 4792 2798 4445 42...
output:
912032
result:
ok 1 number(s): "912032"
Test #4:
score: 10
Accepted
time: 20ms
memory: 20048kb
input:
5000 362 4710 4997 4405 4728 3964 4258 3568 4997 2924 2931 4997 1094 2174 2220 127 4739 260 2591 4130 4916 1614 1408 324 2924 3272 4997 4020 2924 4216 2924 2931 2924 4783 271 1101 2924 246 4953 2553 4588 2924 1770 3738 4617 2508 2486 2137 1348 4847 2632 596 1011 1442 1287 4665 2924 2203 4411 726 109...
output:
1176721
result:
ok 1 number(s): "1176721"
Test #5:
score: 10
Accepted
time: 36ms
memory: 20160kb
input:
5000 3579 3530 3328 388 4864 4954 4597 3600 2428 1610 4533 1797 427 1296 3595 3861 4703 2914 4194 3195 451 585 3600 1134 1649 470 2049 2843 2845 3473 26 845 484 3301 1929 1342 1937 2003 1543 832 2301 2543 1889 1211 1619 1937 4471 585 4440 3600 1398 4687 2931 3982 2334 589 388 4012 873 66 2406 3861 1...
output:
1226857
result:
ok 1 number(s): "1226857"
Subtask #2:
score: 0
Time Limit Exceeded
Test #6:
score: 20
Accepted
time: 3831ms
memory: 201620kb
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:
5859368
result:
ok 1 number(s): "5859368"
Test #7:
score: 20
Accepted
time: 5613ms
memory: 214268kb
input:
200000 161252 109349 161307 131176 54282 35989 53345 116328 52886 20845 166068 198634 185607 110703 32172 153437 50060 194586 193712 73284 32556 105087 55275 157714 22357 182552 31342 100889 145473 91759 18280 144489 108133 130988 11561 20028 121278 138065 83647 33848 33634 31990 198971 110781 12801...
output:
110388948
result:
ok 1 number(s): "110388948"
Test #8:
score: 0
Time Limit Exceeded
input:
200000 36915 117643 88659 78272 142053 101722 71138 149291 152470 118051 45210 31645 187500 22733 178345 55170 28768 44890 26946 76823 76271 9886 197447 130337 74747 175940 118067 191159 19884 113644 73935 160371 97288 153196 50668 47567 113533 73075 90904 115114 191694 127924 127338 41621 134964 59...
output:
result:
Subtask #3:
score: 0
Skipped
Dependency #2:
0%
Subtask #4:
score: 0
Skipped
Dependency #1:
100%
Accepted
Dependency #2:
0%