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 ...
随机推荐
- PowerDesigner 在通过jdbc连接数据库时 Could not Initialize JavaVM!
最近用到PowerDesigner的逆向工程,从数据库中逆向生成模型,本想使用odbc连接的,但是需要安装驱动,mysql的还好弄,oracle对我来讲实在是有些麻烦,看到能用jdbc连接,就想试试, ...
- 数据库连接池之_Druid简单使用
数据库连接池: 连接池是创建和管理一个连接的缓冲池的技术,这些连接真备好被任何需要他们的线程使用,可以对传统的JDBCjava数据库连接()进行优化 在实际开发中,我们需要频繁的操作数据库,这就意味着 ...
- Easy Compression Library(代替TFileStream, TMemoryStream and TStream)
Easy Compression Library is a very easy-to-use replacement of TFileStream, TMemoryStream and other T ...
- visual studio添加docker支持简记
很久以前学过一段时间的docker,那时装了电脑卡得受不了,学了一会就卸载了,最近电脑又装上了docker,感觉好像没有以前这么卡了,还是同一台电脑surface pro4, 试了一下visual s ...
- 获取函数的地址(三种方法,分别是@,Addr,MethodAddress)
问题来源: http://www.cnblogs.com/del/archive/2008/07/30/1039045.html#1272783 在编译器看来, 重载函数根本就是完全不同的几个函数, ...
- selenium工作原理详解
selenium简介 Selenium是一个用于Web应用程序自动化测试工具.Selenium测试直接运行在浏览器中,就像真正的用户在操作一样.支持的浏览器包括IE(7, 8, 9, 10, 11), ...
- Linux五种IO模型 ——Java学习笔记
本文摘自网络: 1.阻塞IO(blocking IO) 在linux中,默认情况下所有的socket都是blocking,一个典型的读操作流程大概是这样: 图1 阻塞IO 当用户进程调用了re ...
- PATA 1027 Colors In Mars
#include <cstdio> char radix[13] = {'0','1','2','3','4','5','6','7','8','9','A','B','C'}; int ...
- CentOS 7编译安装Redis
安装编译相关依赖 CentOS/Fedora yum install gcc gcc-c++ cmake tcl 下载Redis源码包 curl -o redis-4.0.10.tar.gz http ...
- 深度探索c++对象模型 第二章
1,c++转换函数:显示转换和隐式转换. 隐式转换为程序员提供了很大的变量.比如整形提升,普通类型转换为类类型(operator int())都为程序带来无尽的方便.试想,如果没有整形提升,一个sho ...