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.
 
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

HDOJ Important Sisters的更多相关文章

  1. 【23.91%】【hdu 4694】Important Sisters("支NMLGB配树"后记)(支配树代码详解)

    Time Limit: 7000/7000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total Submission( ...

  2. [HDU]4694 Important Sisters(支配树)

    支配树模板 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; ...

  3. [hdu4694]Important Sisters

    来自FallDream的博客,未经允许,请勿转载,谢谢. 给定一张图,求每个点到第n个点必须经过的点的编号之和.n<=50000 一道支配树裸题 然后统计答案的时候可以正着推,ans[i]=an ...

  4. HDU.4694.Important Sisters(支配树)

    HDU \(Description\) 给定一张简单有向图,起点为\(n\).对每个点求其支配点的编号和. \(n\leq 50000\). \(Solution\) 支配树. 还是有点小懵逼. 不管 ...

  5. hdu 4694 Important Sisters【支配树】

    求出支配树输出到father的和即可 支配树见:https://blog.csdn.net/a710128/article/details/49913553 #include<iostream& ...

  6. Dominator Tree & Lengauer-Tarjan Algorithm

    问题描述 给出一张有向图,可能存在环,对于所有的i,求出从1号点到i点的所有路径上的必经点集合. 什么是支配树 两个简单的小性质—— 1.如果i是j的必经点,而j又是k的必经点,则i也是k的必经点. ...

  7. HDOJ并查集题目 HDOJ 1213 HDOJ 1242

    Problem Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. ...

  8. 算法——A*——HDOJ:1813

    Escape from Tetris Time Limit: 12000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  9. hdoj 1116 Play on Words 【并查集】+【欧拉路】

    Play on Words Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

随机推荐

  1. Oracle数据库学习(四)

    11.创建表 crate table tab1(f_id number not null,f_a varchar2(7) not null,f_b number(6,2) not null): 主键: ...

  2. React报错 :browserHistory doesn't exist in react-router

    由于版本问题,React中history不可用 import { hashHistory } from 'react-router' 首先应该导入react-router-dom包: import { ...

  3. 20180909 解析JS Cookie的设置,获取和检索

    引用: JavaScript Cookie - by runoob.com Cookie是储存在电脑文本文件中的数据,用于保存访问者的信息,并可以在下次打开页面时引用. 页面在设置/引用访问者信息时, ...

  4. Java 替换word文档文字,指定位置插入图片

    先说下 需要的依赖包 <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ex ...

  5. GoF23种设计模式之结构型模式之桥接模式

    一.概述         将类的抽象部分与实现分部分离开来,使它们都可以独立地变化. 二.适用性 1.你不希望在抽象和实现之间有一个固定的绑定关系的时候.例如:在程序运行时实现部分应可以被选择或切换. ...

  6. Python入门基础--变量与基本数据类型

    变量 什么是变量 变量就是变化的量,变就是变化,量用于衡量描述对象的状态 为什么要有变量 程序执行的本质就是一系列状态的变化,变是程序执行的直接体现,所以我们需要有一种机制能够反映或者说是保存下来程序 ...

  7. Nearest Common Ancestors POJ - 1330 (LCA)

    Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 34657   Accept ...

  8. [BZOJ2947]促销(Splay)

    Description Great Bytelandish的超级市场网络请你编写一个程序模拟促销商品的成本费用(simulating costs of the promotionbeing prepa ...

  9. HTML插入文件链接(如音乐,照片)

    html中插入音频.H5的标签 src为本地 <audio controls="> <source src="韩庚 - I Don't Give A 屑.mp3& ...

  10. RDLC Reporting in Visual Studio 2017

    原文:RDLC Reporting in Visual Studio 2017 Visual Studio 2017 中可以使用 RDLC Reporting 插件来设计报表,SAP Crystal ...