QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#876683 | #5017. 相等树链 | Mirasycle | Compile Error | / | / | C++14 | 3.3kb | 2025-01-31 11:05:02 | 2025-01-31 11:05:26 |
Judging History
This is the latest submission verdict.
- [2025-01-31 11:05:26]
- Judged
- Verdict: Compile Error
- Time: 0ms
- Memory: 0kb
- [2025-01-31 11:05:02]
- 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;
}
详细
answer.code:15:4: error: ambiguating new declaration of ‘ll random()’ 15 | ll random(){ return F(rand())+(rand()<<3)+(rand()>>1); } | ^~~~~~ In file included from /usr/include/c++/14/cstdlib:79, from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:42, from answer.code:1: /usr/include/stdlib.h:521:17: note: old declaration ‘long int random()’ 521 | extern long int random (void) __THROW; | ^~~~~~