QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#117671 | #4901. Speike & Tom | youngsystem | 0 | 26ms | 40440kb | C++20 | 8.0kb | 2023-07-01 22:27:47 | 2023-07-01 22:27:48 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
inline int read()
{
int n=0,f=1,ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-')f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9')
{
n=n*10+ch-'0';
ch=getchar();
}
return n*f;
}
vector<int>v[200005],ew[200005],zj[200005];
int dep[200005];
int bz[200005][19];
void dfspre(int x,int f)
{
bz[x][0]=f;
dep[x]=dep[f]+1;
for(int i=1;i<=18;i++)bz[x][i]=bz[bz[x][i-1]][i-1];
for(int i=0;i<v[x].size();i++)
{
if(v[x][i]==f)continue;
dfspre(v[x][i],x);
}
}
int lca(int x,int y)
{
if(dep[x]<dep[y])swap(x,y);
for(int i=18;i>=0;i--)
{
if(dep[bz[x][i]]>=dep[y])x=bz[x][i];
}
if(x==y)return x;
for(int i=18;i>=0;i--)
{
if(bz[x][i]!=bz[y][i])
{
x=bz[x][i];
y=bz[y][i];
}
}
return x;
}
int finddis(int x,int y)
{
return dep[x]+dep[y]-2*dep[lca(x,y)];
}
bool aq[200005];
int zsl;
int sl[200005];
void dfsaq(int x,int f)
{
if(aq[x])sl[x]++;
int esl=0;
for(int i=0;i<v[x].size();i++)
{
if(v[x][i]==f)continue;
dfsaq(v[x][i],x);
sl[x]+=sl[v[x][i]];
if(sl[v[x][i]]!=0)esl++;
}
if(sl[x]!=zsl)esl++;
if(esl>=2)aq[x]=true;
}
int sum,rt;
int siz[200005],sy[200005],fa[200005];
bool vis[200005];
void findrt(int x,int f)
{
fa[x]=f;
sy[x]=0;
siz[x]=1;
for(int i=0;i<v[x].size();i++)
{
if(vis[v[x][i]]||v[x][i]==f)continue;
findrt(v[x][i],x);
siz[x]+=siz[v[x][i]];
sy[x]=max(sy[x],siz[v[x][i]]);
}
sy[x]=max(sy[x],sum-siz[x]);
if(sy[x]<sy[rt])rt=x;
}
int f[200005],f2[200005],sx;
void insert(int k,int x)
{
k++;
while(k<=sx)
{
f[k]+=x;
k+=((k)&(-k));
}
}
void qingkong(int k)
{
k++;
while(k<=sx)
{
f[k]=0;
k+=((k)&(-k));
}
}
int query(int k)
{
k++;
int ans=0;
while(k>=1)
{
ans+=f[k];
k-=((k)&(-k));
}
return ans;
}
void insert2(int k,int x)
{
k++;
while(k<=sx)
{
f2[k]+=x;
k+=((k)&(-k));
}
}
int query2(int k)
{
k++;
int ans=0;
while(k>=1)
{
ans+=f2[k];
k-=((k)&(-k));
}
return ans;
}
void qingkong2(int k)
{
k++;
while(k<=sx)
{
f2[k]=0;
k+=(k&(-k));
}
}
bool check(int x,int y)
{
if(y==bz[x][0])return (sl[x]>0);
return (zsl-sl[y]>0);
}
int dis1[200005],n;
long long qans;
void dfs1(int x,int f,int zf)
{
dis1[x]=dis1[f]+1;
insert(dis1[x],zf);
for(int i=0;i<v[x].size();i++)
{
if(v[x][i]==f||vis[v[x][i]])continue;
dfs1(v[x][i],x,zf);
}
}
int nf[200005];
int bh[200005],las[200005],dis2[200005];
int cz[200005],csl[200005];
vector<int>js[200005];
void dfs2(int x,int f)
{
nf[x]=f;
if(check(x,f))
{
bh[x]=x;
las[x]=-1;
dis2[x]=0;
}
else
{
bh[x]=bh[f];
if(bh[f]!=f)las[x]=las[f];
else las[x]=x;
dis2[x]=dis2[f]+1;
for(int i=0;i<ew[x].size();i++)
{
if(zj[x][i]!=f)continue;
if(ew[x][i]==nf[f])dis2[x]=min(dis2[x],dis2[nf[f]]+1);
}
}
//printf("!!!%d %d %d %d\n",x,f,bh[x],las[x]);
csl[x]=0;
cz[x]=0;
for(int i=0;i<ew[x].size();i++)
{
if(zj[x][i]!=f)continue;
if(ew[x][i]==nf[f])continue;
if(check(ew[x][i],f))
{
csl[x]++;
cz[x]=ew[x][i];
}
}
if(bh[x]==x)qans+=query(n);
else
{
int nsx=dis2[x]-dis1[bh[x]]+1;
//printf("???%d %d\n",bh[x],dis1[bh[x]]);
if(csl[las[x]]>=1)nsx--;
if(bh[x]==rt&&csl[las[x]]==1)
{
if(nsx>=0)js[cz[las[x]]].push_back(nsx);
}
//printf("%d %d\n",x,nsx);
qans+=query(n)-query(nsx-1);
}
for(int i=0;i<v[x].size();i++)
{
if(vis[v[x][i]]||v[x][i]==f)continue;
dfs2(v[x][i],x);
}
}
int dis3[200005];
bool jg3[300005];
void dfs3(int x,int f,bool cz)
{
nf[x]=f;
if(f!=rt)
{
dis3[x]=dis3[f]+1;
jg3[x]=jg3[f];
for(int i=0;i<ew[x].size();i++)
{
if(ew[x][i]==nf[nf[x]])
{
dis3[x]=min(dis3[x],dis3[nf[nf[x]]]+1);
jg3[x]=jg3[nf[nf[x]]];
}
}
}
if(jg3[x]&&cz)insert2(dis3[x]+1,1);
else insert2(dis3[x],1);
for(int i=0;i<v[x].size();i++)
{
if(v[x][i]==f||vis[v[x][i]])continue;
dfs3(v[x][i],x,cz);
}
}
int dis4[200005],dis5[200005];
int tbh[200005],tlas[200005];
int nsd[200005];
void dfs4(int x,int f)
{
nf[x]=f;
nsd[x]=nsd[f]+1;
if(f!=rt)
{
dis4[x]=dis4[f]+1;
dis5[x]=dis5[f]+1;
for(int i=0;i<ew[x].size();i++)
{
if(ew[x][i]==nf[f])
{
dis4[x]=min(dis4[x],dis4[nf[f]]+1);
dis5[x]=min(dis5[x],dis5[nf[f]]+1);
}
}
}
tbh[x]=tbh[f];
if(tbh[f]!=f)tlas[x]=tlas[f];
else tlas[x]=x;
if(tbh[x]==-1&&check(f,x))
{
tbh[x]=f;
tlas[x]=x;
csl[x]=0;
}
//printf("???%d %d %d %d %d\n",x,tbh[x],tlas[x],dis4[x],dis5[x]);
csl[x]=0;
for(int i=0;i<ew[f].size();i++)
{
if(zj[f][i]!=x)continue;
if(check(ew[f][i],x))csl[x]++,cz[x]=ew[f][i];
}
if(tbh[x]!=-1)
{
int nsx=nsd[x]-nsd[tbh[x]]-1;
if(csl[tbh[x]]>=2||(csl[tbh[x]]==1&&tlas[x]!=cz[tbh[x]]))nsx++;
//printf("%d %d %d\n",nsx,dis4[x],dis5[x]);
qans+=query(nsx-dis4[tbh[x]])+query2(nsx-dis5[tbh[x]]);
}
for(int i=0;i<v[x].size();i++)
{
if(v[x][i]==f||vis[v[x][i]])continue;
dfs4(v[x][i],x);
}
}
void dfs5(int x,int f)
{
qingkong2(dis3[x]+1);
qingkong(dis3[x]);
for(int i=0;i<v[x].size();i++)
{
if(v[x][i]==f||vis[v[x][i]])continue;
dfs5(v[x][i],x);
}
}
void solve(int x)
{
//printf("solve:%d\n",x);
for(int i=1;i<=sx;i++)assert(f[i]==0);
vis[x]=true;
if(fa[x]!=0&&vis[fa[x]]==false)siz[fa[x]]=sum-siz[x];
int wy=0;
if(!aq[x])
{
for(int i=0;i<v[x].size();i++)
{
if(vis[v[x][i]])continue;
if(v[x][i]!=bz[x][0]&&sl[v[x][i]]>0)wy=v[x][i];
}
if(wy==0&&vis[bz[x][0]]==false&&sl[x]!=zsl)wy=bz[x][0];
}
//printf("orz%d %d\n",x,wy);
dis1[x]=0;
insert(0,1);
for(int i=0;i<v[x].size();i++)
{
if(vis[v[x][i]])continue;
js[v[x][i]].clear();
if(v[x][i]!=wy)dfs1(v[x][i],x,1);
}
nf[x]=-1;
bh[x]=x;
dis2[x]=0;
for(int i=0;i<v[x].size();i++)
{
if(vis[v[x][i]])continue;
if(v[x][i]!=wy)dfs1(v[x][i],x,-1);
if(v[x][i]!=wy)qans+=siz[v[x][i]];
dfs2(v[x][i],x);
if(v[x][i]!=wy)dfs1(v[x][i],x,1);
}
insert(0,-1);
for(int i=0;i<v[x].size();i++)
{
if(vis[v[x][i]])continue;
if(v[x][i]!=wy)dfs1(v[x][i],x,-1);
}
for(int i=0;i<v[x].size();i++)
{
if(vis[v[x][i]])continue;
if(js[v[x][i]].empty())continue;
dfs1(v[x][i],x,1);
for(int j=0;j<js[v[x][i]].size();j++)
{
int sth=js[v[x][i]][j];
qans-=query(sth)-query(sth-1);
}
dfs1(v[x][i],x,-1);
js[v[x][i]].clear();
}
if(wy!=0)
{
nf[x]=-1;
jg3[x]=false;
for(int i=0;i<v[x].size();i++)
{
if(vis[v[x][i]]||v[x][i]==wy)continue;
bool flag=false;
for(int j=0;j<ew[v[x][i]].size();j++)
{
if(ew[v[x][i]][j]==wy)
{
flag=true;
break;
}
}
jg3[v[x][i]]=true;
if(flag==true)dis3[v[x][i]]=0;
else dis3[v[x][i]]=1;
dfs3(v[x][i],x,flag);
}
insert(0,1);
dis4[x]=0;
dis4[wy]=1;
dis5[x]=1000000000;
dis5[wy]=0;
tbh[x]=-1;
nsd[x]=0;
dfs4(wy,x);
for(int i=0;i<v[x].size();i++)
{
if(vis[v[x][i]]||v[x][i]==wy)continue;
dfs5(v[x][i],x);
}
qingkong(0);
}
//printf("jieshu:%d %d\n",x,qans);
for(int i=0;i<v[x].size();i++)
{
if(vis[v[x][i]])continue;
sum=siz[v[x][i]];
if(sum==1)continue;
rt=0;
findrt(v[x][i],x);
solve(rt);
}
}
int main()
{
int m,x,y;
n=read();
m=read();
for(int i=1;i<=n-1;i++)
{
x=read();
y=read();
v[x].push_back(y);
v[y].push_back(x);
}
dfspre(1,0);
for(int i=1;i<=m;i++)
{
x=read();
y=read();
int sth=finddis(x,y);
if(sth==1)continue;
if(sth>=3)aq[x]=aq[y]=true;
else
{
int zz=0;
if(x==bz[y][1])zz=bz[y][0];
else if(y==bz[x][1])zz=bz[x][0];
else zz=bz[x][0];
ew[x].push_back(y);
ew[y].push_back(x);
zj[x].push_back(zz);
zj[y].push_back(zz);
}
}
zsl=0;
for(int i=1;i<=n;i++)if(aq[i])zsl++;
if(zsl==0)
{
printf("0\n");
return 0;
}
sx=n+1;
dfsaq(1,0);
sy[0]=1000000000;
rt=0;
sum=n;
findrt(1,0);
solve(rt);
// printf("%lld\n",qans);
for(int i=1;i<=sx;i++)assert(f[i]==0&&f2[i]==0);
printf("%lld\n",qans);
return 0;
}
詳細信息
Subtask #1:
score: 0
Dangerous Syscalls
Test #1:
score: 0
Dangerous Syscalls
input:
20 3 1 2 1 3 3 4 4 5 1 6 6 7 1 8 5 9 8 10 5 11 7 12 11 13 12 14 11 15 4 16 7 17 2 18 1 19 3 20 8 20 12 4 10 1
output:
result:
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Skipped
Dependency #2:
0%
Subtask #4:
score: 0
Skipped
Dependency #3:
0%
Subtask #5:
score: 0
Dangerous Syscalls
Test #29:
score: 10
Accepted
time: 26ms
memory: 40440kb
input:
98765 1 2 1 3 1 4 2 5 2 6 5 7 4 8 6 9 7 10 7 11 6 12 1 13 11 14 13 15 7 16 6 17 14 18 4 19 13 20 14 21 11 22 21 23 1 24 13 25 7 26 16 27 8 28 21 29 20 30 10 31 12 32 10 33 7 34 31 35 29 36 29 37 30 38 34 39 38 40 14 41 40 42 26 43 33 44 1 45 44 46 25 47 14 48 2 49 30 50 26 51 46 52 34 53 32 54 31 55...
output:
0
result:
ok 1 number(s): "0"
Test #30:
score: 0
Accepted
time: 14ms
memory: 39976kb
input:
99824 1 2 1 3 1 4 1 5 1 6 2 7 2 8 1 9 3 10 9 11 4 12 6 13 6 14 2 15 3 16 9 17 13 18 15 19 4 20 13 21 12 22 15 23 5 24 16 25 17 26 9 27 26 28 18 29 8 30 23 31 5 32 31 33 28 34 5 35 11 36 20 37 6 38 36 39 35 40 4 41 11 42 10 43 12 44 28 45 15 46 38 47 9 48 36 49 16 50 45 51 49 52 44 53 6 54 12 55 5 56...
output:
0
result:
ok 1 number(s): "0"
Test #31:
score: -10
Dangerous Syscalls
input:
67765 1 2 1 3 2 4 1 5 2 6 2 7 3 8 4 9 3 10 5 11 6 12 4 13 7 14 13 15 11 16 11 17 3 18 8 19 2 20 13 21 19 22 7 23 14 24 7 25 6 26 21 27 1 28 1 29 2 30 16 31 9 32 31 33 26 34 1 35 21 36 10 37 32 38 16 39 38 40 10 41 20 42 23 43 8 44 10 45 14 46 42 47 12 48 17 49 45 50 28 51 42 52 49 53 44 54 9 55 8 56...
output:
result:
Subtask #6:
score: 0
Skipped
Dependency #1:
0%