题解
2023-08-25 14:40:30
发布于:广东
3阅读
0回复
0点赞
#include<bits/stdc++.h>
#define ll long long
#define inf 0x7fffffff
#define N 500008
#define IL inline
#define M 508611
#define D double
#define ull unsigned long long
#define R register
using namespace std;
template<typename T>IL void read(T &_)
{
T __=0,___=1;char ____=getchar();
while(!isdigit(____)) {if(____=='-') ___=0;____=getchar();}
while(isdigit(____)) {__=(__<<1)+(__<<3)+____-'0';____=getchar();}
_=___ ? __:-__;
}
int n,m,tot,cnt,top,all;
int to[M],nex[M],head[M],ans[M];
int dep[M],fath[M][20],siz[M];
int dfn[M],wt[M],tre[M],in[M];
int dfnn[M],low[M],sta[M];
bool vis[M];
vector<int> G[M];
bool flag;
IL void add_edge(int x,int y)
{;to[++tot]=y;nex[tot]=head[x];head[x]=tot;}
IL void add(int x,int y)
{for(;x<=n;x+=x&-x) tre[x]+=y;}
IL int qury(int x)
{int res=0;for(;x;x-=x&-x) res+=tre[x];return res;}
IL void dfs(int now,int fat,int deep)
{
dep[now]=deep;fath[now][0]=fat;dfn[now]=++cnt;wt[cnt]=now;siz[now]=1;
for(R int i=1;i<=19;++i) fath[now][i]=fath[fath[now][i-1]][i-1];
for(R int i=head[now];i;i=nex[i])
{
int v=to[i];
if(v==fat) continue;
dfs(v,now,deep+1);
siz[now]+=siz[v];
}
}
IL int LCA(int nowx,int nowy)
{
if(dep[nowx]<dep[nowy]) swap(nowx,nowy);
for(R int i=19;i>=0;--i)
if(dep[fath[nowx][i]]>=dep[nowy]) nowx=fath[nowx][i];
if(nowx==nowy) return nowx;
for(R int i=19;i>=0;--i)
if(fath[nowx][i]!=fath[nowy][i])
nowx=fath[nowx][i],nowy=fath[nowy][i];
return fath[nowx][0];
}
IL void update(int x,int y)
{
if(x>y) return;
add(x,1);add(y+1,-1);
}
IL void Tarjan(int now)
{
dfnn[now]=low[now]=++cnt;
vis[now]=1;sta[++top]=now;
for(R int i=0;i<(int)G[now].size();++i)
{
int v=G[now][i];
if(!dfnn[v]) Tarjan(v),low[now]=min(low[now],low[v]);
else if(vis[v]) low[now]=min(low[now],dfnn[v]);
}
if(low[now]==dfnn[now])
{
all=0;
while(sta[top+1]!=now)
{
++all;
vis[sta[top--]]=0;
}
if(all>1) flag=1;
}
}
int main()
{
read(n);read(m);
for(R int i=1,x,y;i<n;++i)
{
read(x);read(y);
add_edge(x,y);add_edge(y,x);
}
dfs(1,0,1);
for(R int i=1,x,y;i<=m;++i)
{
read(x);read(y);
G[x].push_back(y);
int lca=LCA(x,y);
if(lca==x)
{
int now=y;
for(R int i=19;i>=0;--i)
if(fath[now][i]&&dep[fath[now][i]]>dep[x]) now=fath[now][i];
update(dfn[now],dfn[now]+siz[now]-1);
}
else
{
update(1,dfn[x]-1);update(dfn[x]+siz[x],n);
}
}
for(R int i=n;i;--i) if(!dfnn[i]) Tarjan(i);
if(flag)
for(R int i=1;i<=n;++i) puts("0");
else
for(R int i=1;i<=n;++i) printf("%d\n",qury(dfn[i])==m);
return 0;
}
这里空空如也
有帮助,赞一个