假设以1是起点。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=2e5+7;
const int maxm=8e5+7;
vector<int> dom[maxn];
//x支配的点集;
struct Edge{
int v,next;
}edge[maxm];
//正向图,反向图,支配树;
int headG[maxn],headrG[maxn],top,headd[maxn];
//父亲,时间戳,时间戳,时间戳对应的节点;
int fa[maxn], dfn[maxn], clk, rdfn[maxn];
int fre[maxn], best[maxn], semi[maxn], idom[maxn];
// 并查集,并查集路径权值,记录从节点x到首节点路径上的所有节点中半必经点dfs序最小的节点,最远半必经点,最近必经点;
void init(int n){
clk=0;
for(int i=1;i<=n;++i) dom[i].clear();
memset(fre,-1,sizeof(fre));
memset(headG,-1,sizeof(headG));
memset(headrG,-1,sizeof(headrG));
memset(headd,-1,sizeof(headd));
top=0;
for(int i=1;i<=n;++i) semi[i]=best[i]=i;
memset(dfn,0,sizeof(dfn));
}
void add(int u,int v,int head[]){
edge[top].v=v;
edge[top].next=head[u];
head[u]=top++;
}
//求dfs序;
void dfs(int u){
dfn[u]=++clk;
rdfn[clk]=u;
int v;
for(int i=headG[u];i!=-1;i=edge[i].next){
v=edge[i].v;
if(!dfn[v]) {
fa[v]=u;
dfs(v);
}
}
}
//路径压缩;
int fix(int x){
if(fre[x]==-1) return x;
int &f=fre[x],rt=fix(f);
if(dfn[semi[best[x]]]>dfn[semi[best[f]]]) best[x]=best[f];
return f=rt;
}
void go(int rt){
dfs(rt);
for(int i=clk;i>1;--i){//dfs序从大到小计算semi;
int x=rdfn[i],mn=clk+1;
for(int j=headrG[x];j!=-1;j=edge[j].next){//跑反图,遍历所有的父亲;
int u=edge[j].v;
if(!dfn[u]) continue;//可能不能到达所有点;
fix(u);//路径压缩;
mn=min(mn,dfn[semi[best[u]]]);
}
fre[x]=fa[x];
dom[ semi[x]=rdfn[mn] ].push_back(x);
x=rdfn[i-1];
for(int j=0;j<dom[x].size();++j){
int u=dom[x][j];
fix(u);
if(semi[best[u]]!=x) idom[u]=best[u];
else idom[u]=x;
}
dom[x].clear();
}
for(int i=2;i<=clk;++i){//修正idom
int u=rdfn[i];
if(idom[u]!=semi[u]) idom[u]=idom[idom[u]];
dom[idom[u]].push_back(u);
}
}
int siz[maxn];
bool vis[maxn];
void dfs2(int x){//建支配树;
siz[x]=1;
vis[x]=1;
for(int i=headd[x];i!=-1;i=edge[i].next){
int v=edge[i].v;
if(vis[v]) continue;
dfs2(v);
siz[x]+=siz[v];
}
}
int main(){
int n,m,u,v;
scanf("%d%d",&n,&m);
init(n);
while(m--){
scanf("%d%d",&u,&v);
add(u,v,headG);
add(v,u,headrG);
}
go(1);
for(int i=1;i<=n;++i)
if(idom[i]) add(idom[i],i,headd);
dfs2(1);
for(int i=1;i<=n;++i) printf("%d ",siz[i]);
return 0;
}