传送门
解析:
支配树裸题。
按照题目要求就是求支配树上点到根的路径和就行了。
直接 L e n g a u e r − T a r j a n Lengauer-Tarjan Lengauer−Tarjan走一发再 D F S DFS DFS一遍就完了。
注意输出格式,每行最后一个数字后面一定不能有空格,不然会PE(HDU终于知道用PE而不是WA来提示了好感动)
代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define re register
#define gc get_char
#define pc put_char
#define cs const
namespace IO{
namespace IOONLY{
cs int Rlen=1<<20|1;
char buf[Rlen],*p1,*p2;
char obuf[Rlen],*p3=obuf;
char ch[23];
}
inline char get_char(){
using namespace IOONLY;
return (p1==p2)&&(p2=(p1=buf)+fread(buf,1,Rlen,stdin),p1==p2)?EOF:*p1++;
}
inline void put_char(char c){
using namespace IOONLY;
*p3++=c;
if(p3==obuf+Rlen)fwrite(obuf,1,Rlen,stdout),p3=obuf;
}
inline void FLUSH(){
using namespace IOONLY;
fwrite(obuf,1,p3-obuf,stdout),p3=obuf;
}
inline int getint(){
re int num;
re char c;
while(!isdigit(c=gc()))if(c==EOF)return EOF;num=c^48;
while(isdigit(c=gc()))num=(num+(num<<2)<<1)+(c^48);
return num;
}
inline void outint(ll a){
using namespace IOONLY;
if(a==0)pc('0');
while(a)ch[++ch[0]]=a-a/10*10,a/=10;
while(ch[0])pc(ch[ch[0]--]^48);
}
}
using namespace IO;
cs int N=50004;
int n,m;
struct Graph{
vector<int> edge[N];
inline void clear(){for(int re i=1;i<=n;++i)edge[i].clear();}
inline void addedge(int u,int v){edge[u].push_back(v);}
}g,revg,ng,dt;
int dfn[N],id[N],fa[N],dfs_clock;
void dfs(int u){
id[dfn[u]=++dfs_clock]=u;
for(int re e=0,v;e<g.edge[u].size();++e){
v=g.edge[u][e];
if(dfn[v])continue;
fa[v]=u;dfs(v);
}
}
int bel[N],val[N],semi[N],idom[N];
inline int getfa(int x){
if(bel[x]==x)return x;
int tmp=getfa(bel[x]);
if(dfn[semi[val[bel[x]]]]<dfn[semi[val[x]]])val[x]=val[bel[x]];
return bel[x]=tmp;
}
inline void tarjan(){
for(int re i=dfs_clock;i>1;--i){
int u=id[i];
for(int re e=0,v;e<revg.edge[u].size();++e){
v=revg.edge[u][e];
if(!dfn[v])continue;
getfa(v);
if(dfn[semi[val[v]]]<dfn[semi[u]])semi[u]=semi[val[v]];
}
ng.addedge(semi[u],u);
u=bel[u]=fa[u];
for(int re e=0,v;e<ng.edge[u].size();++e){
v=ng.edge[u][e];
getfa(v);
if(semi[val[v]]==u)idom[v]=u;
else idom[v]=val[v];
}
}
for(int re i=2;i<=dfs_clock;++i){
int u=id[i];
if(idom[u]!=semi[u])idom[u]=idom[idom[u]];
dt.addedge(idom[u],u);
}
}
ll ans[N];
inline void init(){
for(int re i=1;i<=n;++i)bel[i]=semi[i]=val[i]=i,dfn[i]=ans[i]=0;
dfs_clock=0;
g.clear();
revg.clear();
ng.clear();
dt.clear();
}
void dfs_ans(int u){
for(int re e=0,v;e<dt.edge[u].size();++e){
v=dt.edge[u][e];
ans[v]=v+ans[u];
dfs_ans(v);
}
}
signed main(){
while(~(n=getint())){
m=getint();
init();
for(int re i=1;i<=m;++i){
int u=getint(),v=getint();
g.addedge(u,v);
revg.addedge(v,u);
}
dfs(n);
tarjan();
ans[n]=n;
dfs_ans(n);
for(int re i=1;i<=n;++i)outint(ans[i]),pc(i==n?'\n':' ');
}
FLUSH();
return 0;
}