补模板,并没有题解
不会的算法
反正据说常数很大
虽然要倍增也很慢
感觉要记一大堆东西还是十分蛋疼
#include<bits/stdc++.h>
using namespace std;
const int RLEN=1<<20|1;
inline char gc(){
static char ibuf[RLEN],*ib,*ob;
(ob==ib)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
return (ob==ib)?EOF:*ib++;
}
#define gc getchar
inline int read(){
char ch=gc();
int res=0,f=1;
while(!isdigit(ch))f^=ch=='-',ch=gc();
while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
return f?res:-res;
}
#define ll long long
#define re register
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
#define cs const
#define bg begin
inline void chemx(int &a,int b){a<b?a=b:0;}
inline void chemn(int &a,int b){a>b?a=b:0;}
cs int N=200005;
namespace TT{
vector<int> e1[N],fr1[N],e2[N],fr2[N],tt[N],p;
int ga[N],semi[N],mn[N],idx[N],dfn[N],tim;
int fa[N][21],dep[N],ff[N],in[N];
int n,m,siz[N];
queue<int> q;
inline int find(int x){
if(ga[x]==x)return x;
int t=ga[x];ga[x]=find(ga[x]);
if(dfn[semi[mn[t]]]<dfn[semi[mn[x]]])mn[x]=mn[t];
return ga[x];
}
inline void init(){
for(int i=1;i<=n;i++)ga[i]=mn[i]=semi[i]=i;
}
inline void add1(int u,int v){
e1[u].pb(v),fr1[v].pb(u);
}
inline void add2(int u,int v){
e2[u].pb(v),fr2[v].pb(u),in[v]++;
}
void dfs_t(int u){
dfn[u]=++tim,idx[tim]=u;
for(int &v:e1[u])
if(!dfn[v])ff[v]=u,add2(u,v),dfs_t(v);
}
inline void tarjan(){
for(int i=n;i>=2;i--){//
int u=idx[i],mx=i;
if(!u)continue;//
for(int &v:fr1[u]){
if(!dfn[v])continue;
if(dfn[v]<dfn[u])chemn(mx,dfn[v]);
else find(v),chemn(mx,dfn[semi[mn[v]]]);
}
semi[u]=idx[mx],ga[u]=ff[u],add2(semi[u],u);
}
}
inline int Lca(int u,int v){
if(dep[u]<dep[v])swap(u,v);
for(int i=20;~i;i--)if(dep[fa[u][i]]>=dep[v])u=fa[u][i];
if(u==v)return u;
for(int i=20;~i;i--)if(fa[u][i]!=fa[v][i])u=fa[u][i],v=fa[v][i];
return fa[u][0];
}
inline void topsort(){
for(int i=1;i<=n;i++)if(!in[i])fr2[i].pb(0),p.pb(i),q.push(i);
while(!q.empty()){
int u=q.front();q.pop();
for(int &v:e2[u]){
in[v]--;
if(!in[v])p.pb(v),q.push(v);
}
}
}
inline void build(){
for(int &u:p){
int v=fr2[u][0];
for(int &x:fr2[u])v=Lca(v,x);
fa[u][0]=v,dep[u]=dep[v]+1;
tt[v].pb(u);
for(int i=1;i<=20;i++)fa[u][i]=fa[fa[u][i-1]][i-1];
}
}
void dfs(int u){
siz[u]=1;
for(int &v:tt[u])
dfs(v),siz[u]+=siz[v];
}
inline void buildtree(int _n,int _m){
n=_n,m=_m;
init();
for(int i=1;i<=m;i++){
int u=read(),v=read();
add1(u,v);
}
dfs_t(1);
tarjan();
topsort();
build();
dfs(1);
for(int i=1;i<=n;i++)cout<<siz[i]<<" ";
}
}
int n,m;
int main(){
n=read(),m=read();
TT::buildtree(n,m);
}