HDOJ Important Sisters
Important Sisters
Time Limit: 7000/7000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 766 Accepted Submission(s): 192
Problem Description
There are N clones of Misaka Mikoto (sisters) forming the Misaka network. Some pairs of sisters are connected so that one of them can pass message to the other one. The sister with serial number N is the source of all messages. All the other sisters get message directly or indirectly from her. There might be more than one path from sister #N to sister #I, but some sisters do appear in all of these paths. These sisters are called important sister of sister #K. What are the important sisters of each sister?
Input
There are multiple test cases. Process to the End of File.
The first line of each test case contains two integers: the number of sisters 1 ≤ N ≤ 50,000 and the number of connections 0 ≤ M ≤ 100,000. The following M lines are M connections 1 ≤ Ai, Bi ≤ N, indicating that Ai can pass message to Bi.
The first line of each test case contains two integers: the number of sisters 1 ≤ N ≤ 50,000 and the number of connections 0 ≤ M ≤ 100,000. The following M lines are M connections 1 ≤ Ai, Bi ≤ N, indicating that Ai can pass message to Bi.
Output
For each test case, output the sum of the serial numbers of important sisters of each sister, separated with single space.
Sample Input
3 2
3 2
2 1
5 7
3 2
1 2
2 1
3 1
3 2
5 3
5 4
Sample Output
6 5 3
9 10 8 9 5
Author
Zejun Wu (watashi)
Source
分析:
支配树板子题...
代码:
#include<algorithm> #include<iostream> #include<cstring> #include<cstdio> #include<stack> //by NeighThorn using namespace std; const int maxn=50000+5,maxm=100000+5; int n,m,tot,f[maxn],fa[maxn],id[maxn],dfn[maxn],idom[maxn],semi[maxn],node[maxn]; long long ans[maxn]; stack<int> dom[maxn]; struct M{ int cnt,hd[maxn],to[maxm],nxt[maxm]; inline void init(void){ cnt=0; memset(hd,-1,sizeof(hd)); } inline void add(int x,int y){ to[cnt]=y;nxt[cnt]=hd[x];hd[x]=cnt++; } }G,tr; inline bool cmp(int x,int y){ return dfn[semi[x]]<dfn[semi[y]]; } inline int find(int x){ if(f[x]==x) return x; int fx=find(f[x]); node[x]=min(node[f[x]],node[x],cmp); return f[x]=fx; } inline void dfs(int x){ dfn[x]=++tot;id[tot]=x; for(int i=tr.hd[x];i!=-1;i=tr.nxt[i]) if(!dfn[tr.to[i]]) dfs(tr.to[i]),fa[tr.to[i]]=x; } inline void LT(void){ dfs(n);dfn[0]=tot<<1; for(int i=tot,x;i>=1;i--){ x=id[i]; if(i!=1){ for(int j=G.hd[x],v;j!=-1;j=G.nxt[j]) if(dfn[G.to[j]]){ v=G.to[j]; if(dfn[v]<dfn[x]){ if(dfn[v]<dfn[semi[x]]) semi[x]=v; } else{ find(v); if(dfn[semi[node[v]]]<dfn[semi[x]]) semi[x]=semi[node[v]]; } } dom[semi[x]].push(x); } while(dom[x].size()){ int y=dom[x].top();dom[x].pop();find(y); if(semi[node[y]]!=x) idom[y]=node[y]; else idom[y]=x; } for(int j=tr.hd[x];j!=-1;j=tr.nxt[j]) if(fa[tr.to[j]]==x) f[tr.to[j]]=x; } for(int i=2,x;i<=tot;i++){ x=id[i]; if(semi[x]!=idom[x]) idom[x]=idom[idom[x]]; } idom[id[1]]=0; } inline long long calc(int x){ if(ans[x]) return ans[x]; if(x==n) return ans[x]=n; return ans[x]=calc(idom[x])+x; } signed main(void){ while(scanf("%d%d",&n,&m)!=EOF){ G.init();tr.init();tot=0; memset(id,0,sizeof(id)); memset(ans,0,sizeof(ans)); memset(dfn,0,sizeof(dfn)); memset(semi,0,sizeof(semi)); memset(idom,0,sizeof(idom)); for(int i=1;i<=n;i++) f[i]=node[i]=i; for(int i=1,x,y;i<=m;i++) scanf("%d%d",&x,&y),tr.add(x,y),G.add(y,x); LT(); for(int i=1;i<=n;i++){ if(!dfn[i]) printf("%d",0); else printf("%lld",calc(i)); if(i<n) printf(" "); } puts(""); } return 0; }
By NeighThorn