POJ2533&&SP1799 The Bottom of a Graph(tarjan+缩点)
POJ2553
SP1799
我们知道单独一个强连通分量中的所有点是满足题目要求的
但如果它连出去到了其他点那里,要么成为新的强连通分量,要么失去原有的符合题目要求的性质
所以只需tarjan缩点求出所有强连通分量,再O(E)枚举所有边,是否会成为连接一个分量与另一个分量的边——即一条出度——即可
如果一个分量没有出度,那么他中间的所有点都是符合题目要求的点
(因为快读快输加了太长所以就不贴了)
const int N=5005,M=N*N>>1;
int h[N],en,n,m,dfn[N],out[N],bel[N],low[N],num,cnt;
stack<int> st;
struct edge{int n,u,v;}e[M]; //前向星存边
inline void add(const int &x,const int &y){e[++en]=(edge){h[x],x,y},h[x]=en;}
inline void tarjan(int x){ //一个tarjan缩点STL栈模板
st.push(x);
dfn[x]=low[x]=++num;
for(int i=h[x];i;i=e[i].n){
int y=e[i].v;
if(!dfn[y]){
tarjan(y);
low[x]=min(low[x],low[y]);
}
else if(!bel[y])
low[x]=min(low[x],dfn[y]);
}
if(low[x]==dfn[x]){
cnt++;
int TOP;
do{
TOP=st.top();
st.pop();
bel[TOP]=cnt;
}while(TOP!=x);
}
}
signed main(){
read(n);
while(n){
en=num=cnt=0;
memset(h,0,sizeof h);
memset(dfn,0,sizeof dfn);
memset(out,0,sizeof out);
memset(bel,0,sizeof bel);
memset(low,0,sizeof low);
read(m);
while(m--){
int x,y;
read(x),read(y);
add(x,y);
}
for(int i=1;i<=n;i++) if(!dfn[i]) //跑缩点
tarjan(i);
for(int i=1,u,v;i<=en;i++){
u=e[i].u,v=e[i].v;
if(bel[u]!=bel[v]) out[bel[u]]++; //判断每条边的起点终点是否在同一强连通分量中,如果不是,则起点所在强连通分量出度加1
}
for(int i=1;i<=n;i++) if(!out[bel[i]]) //如果点i所在强连通分量没有出度则满足要求,输出
Write(i,' ');
printf("\n"); //统一换行
read(n);
}
}
POJ2533&&SP1799 The Bottom of a Graph(tarjan+缩点)的更多相关文章
- POJ 2553 The Bottom of a Graph (Tarjan)
The Bottom of a Graph Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 11981 Accepted: ...
- POJ 2553 The Bottom of a Graph Tarjan找环缩点(题解解释输入)
Description We will use the following (standard) definitions from graph theory. Let V be a nonempty ...
- poj--2553--The Bottom of a Graph (scc+缩点)
The Bottom of a Graph Time Limit : 6000/3000ms (Java/Other) Memory Limit : 131072/65536K (Java/Oth ...
- POJ 2553 The Bottom of a Graph TarJan算法题解
本题分两步: 1 使用Tarjan算法求全部最大子强连通图.而且标志出来 2 然后遍历这些节点看是否有出射的边,没有的顶点所在的子强连通图的全部点,都是解集. Tarjan算法就是模板算法了. 这里使 ...
- [poj 2553]The Bottom of a Graph[Tarjan强连通分量]
题意: 求出度为0的强连通分量. 思路: 缩点 具体有两种实现: 1.遍历所有边, 边的两端点不在同一强连通分量的话, 将出发点所在强连通分量出度+1. #include <cstdio> ...
- The Bottom of a Graph(tarjan + 缩点)
The Bottom of a Graph Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 9139 Accepted: ...
- poj 2553 The Bottom of a Graph(强连通分量+缩点)
题目地址:http://poj.org/problem?id=2553 The Bottom of a Graph Time Limit: 3000MS Memory Limit: 65536K ...
- poj 2553 The Bottom of a Graph【强连通分量求汇点个数】
The Bottom of a Graph Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 9641 Accepted: ...
- 【图论】The Bottom of a Graph
[POJ2553]The Bottom of a Graph Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 11182 ...
随机推荐
- C#根据对象的指定字段去除重复值
PersonInfo类: public class PersonInfo { public int Index; public string Name; public override string ...
- BAT-把当前用户以管理员权限运行(用户帐户控制:用于内置管理员帐户的管理员批准模式)
相关资料: http://jingyan.baidu.com/article/72ee561a5dc24fe16138df95.html 网友求助:联想Y400,Win8系统 怎样获得管理员身份 要求 ...
- html5创建的sqlite存放为止以及在手机中的位置
C:\Users\xiaoai\AppData\Local\Google\Chrome\User Data\Default\databases\http_127.0.0.1_8020 如图:这是用bh ...
- CopyMemory、FillMemory、MoveMemory、ZeroMemory
CopyMemory 复制内存,第一个参数为目的地址,第二个参数为源地址,第三个参数为复制数据的大小,单位字节,源内存区域不能重叠,如果重叠,可以使用MoveMemory()函数.函数原型如下: vo ...
- 02、MySQL—数据库基本操作
数据库是数据存储的最外层(最大单元) 1.创建数据库 基本语法:create database 数据库名字 [库选项]; 范例:使用create database 创建数据库 库选项:数据库的相关属性 ...
- Fabric1.4源码解析: 链码容器启动过程
想写点东西记录一下最近看的一些Fabric源码,本文使用的是fabric1.4的版本,所以对于其他版本的fabric,内容可能会有所不同. 本文想针对Fabric中链码容器的启动过程进行源码的解析.这 ...
- 【maven 】jar包冲突-记一次冲突解决
方法一:根据mvn提示一个一个排除 1.请到pom.xml文件所在的目录(包含父子目录)下分别执行下面的命令排查是什么原因导致fastjson版本不正确: mvn dependency:tree -D ...
- 【hadoop+spark】搭建spark过程
部分转载,已标红源地址,本博客为本菜搭建与爬坑记录,整理版请看: https://blog.csdn.net/the_fool_/article/details/78211166 记录: ====== ...
- 用 IQ分布模拟图来测试浏览器的性能
今天天气太凉快,跟这个日历上属于夏天的那一页显得格格不入!就连我我床下那台废弃的ThinkPad,居然也十分透凉气,那外壳连我的体温高都没有,于是,我就开始想一个方法,让我那个废弃的电脑发热,顺便用它 ...
- HDU 5113:Black And White(DFS)
题目链接 题意 给出一个n*m的图,现在有k种颜色让你对这个图每个格子染色,每种颜色最多可以使用col[i]次,问是否存在一种染色方案使得相邻格子的颜色不同. 思路 以为是构造题,结果是爆搜.对于每一 ...