/**
problem: http://poj.org/problem?id=2553
将所有出度为0环中的点排序输出即可。
**/ #include<stdio.h>
#include<stack>
#include<vector>
#include<algorithm>
using namespace std; class Graphics{
const static int MAXN = ;
const static int MAXM = MAXN * MAXN;
private:
struct Edge{
int to, next;
}edge[MAXM];
struct Point{
int dfn, low, color;
Point(){dfn = low = color = ;}
}point[MAXN], emptyPoint;
int first[MAXN], sign, colorNum, dfnNum, sumOfPoint;
bool vis[MAXN];
vector<int> ring[MAXN];
stack<int> stk;
void tarjan(int u){
point[u].dfn = ++ dfnNum;
point[u].low = dfnNum;
vis[u] = true;
stk.push(u);
for(int i = first[u]; i != -; i = edge[i].next){
int to = edge[i].to;
if(!point[to].dfn){
tarjan(to);
point[u].low = min(point[to].low, point[u].low);
}else if(vis[to]){
point[u].low = min(point[to].dfn, point[u].low);
}
}
if(point[u].low == point[u].dfn){
vis[u] = false;
point[u].color = ++colorNum;
ring[colorNum].push_back(u);
while(stk.top() != u){
vis[stk.top()] = false;
point[stk.top()].color = colorNum;
ring[colorNum].push_back(stk.top());
stk.pop();
}
stk.pop();
}
}
public:
void clear(int n){
sign = colorNum = dfnNum = ;
sumOfPoint = n;
for(int i = ; i <= n; i ++){
first[i] = -;
vis[i] = false;
ring[i].clear();
point[i] = emptyPoint;
}
while(!stk.empty()) stk.pop();
}
void addEdgeOneWay(int u, int v){
edge[sign].to = v;
edge[sign].next = first[u];
first[u] = sign ++;
}
void tarjanAllPoint(){
for(int i = ; i <= sumOfPoint; i ++){
if(!point[i].dfn){
tarjan(i);
}
}
}
vector<int> getAns(){
vector<int> ans;
int *outdegree = new int[sumOfPoint+];
for(int i = ; i <= sumOfPoint; i ++){
outdegree[i] = ;
}
tarjanAllPoint();
for(int i = ; i <= sumOfPoint; i ++){
for(int j = first[i]; j != -; j = edge[j].next){
int to = edge[j].to;
if(point[to].color != point[i].color){
outdegree[point[i].color] ++;
}
}
}
for(int i = ; i <= colorNum; i ++){
if(!outdegree[i]){
for(int j = ; j < ring[i].size(); j ++){
ans.push_back(ring[i][j]);
}
}
}
sort(ans.begin(), ans.end());
delete []outdegree;
return ans;
}
}graph; int main(){
int n, m;
while(scanf("%d%d", &n, &m) != EOF && n){
graph.clear(n);
while(m --){
int a, b;
scanf("%d%d", &a, &b);
graph.addEdgeOneWay(a, b);
}
vector<int> ans = graph.getAns();
bool first = ;
for(int i = ; i < ans.size(); i ++){
if(first) first = ;
else putchar(' ');
printf("%d", ans[i]);
}
putchar('\n');
}
return ;
}

poj 2553 The Bottom of a Graph : tarjan O(n) 存环中的点的更多相关文章

  1. POJ 2553 The Bottom of a Graph (Tarjan)

    The Bottom of a Graph Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 11981   Accepted: ...

  2. POJ 2553 The Bottom of a Graph Tarjan找环缩点(题解解释输入)

    Description We will use the following (standard) definitions from graph theory. Let V be a nonempty ...

  3. POJ 2553 The Bottom of a Graph TarJan算法题解

    本题分两步: 1 使用Tarjan算法求全部最大子强连通图.而且标志出来 2 然后遍历这些节点看是否有出射的边,没有的顶点所在的子强连通图的全部点,都是解集. Tarjan算法就是模板算法了. 这里使 ...

  4. [poj 2553]The Bottom of a Graph[Tarjan强连通分量]

    题意: 求出度为0的强连通分量. 思路: 缩点 具体有两种实现: 1.遍历所有边, 边的两端点不在同一强连通分量的话, 将出发点所在强连通分量出度+1. #include <cstdio> ...

  5. POJ 2553 The Bottom of a Graph(强连通分量)

    POJ 2553 The Bottom of a Graph 题目链接 题意:给定一个有向图,求出度为0的强连通分量 思路:缩点搞就可以 代码: #include <cstdio> #in ...

  6. poj 2553 The Bottom of a Graph(强连通分量+缩点)

    题目地址:http://poj.org/problem?id=2553 The Bottom of a Graph Time Limit: 3000MS   Memory Limit: 65536K ...

  7. poj 2553 The Bottom of a Graph【强连通分量求汇点个数】

    The Bottom of a Graph Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 9641   Accepted:  ...

  8. POJ 2553 The Bottom of a Graph (强连通分量)

    题目地址:POJ 2553 题目意思不好理解.题意是:G图中从v可达的全部点w,也都能够达到v,这种v称为sink.然后升序输出全部的sink. 对于一个强连通分量来说,全部的点都符合这一条件,可是假 ...

  9. POJ 2553 The Bottom of a Graph 【scc tarjan】

    图论之强连通复习开始- - 题目大意:给你一个有向图,要你求出这样的点集:从这个点出发能到达的点,一定能回到这个点 思路:强连通分量里的显然都可以互相到达 那就一起考虑,缩点后如果一个点有出边,一定不 ...

随机推荐

  1. mysql二:库操作

    一.系统数据库 information_schema: 虚拟库,不占用磁盘空间,存储的是数据库启动后的一些参数,如用户表信息.列信息.权限信息.字符信息等 performance_schema: My ...

  2. JVM crash at ForUtil.readBlock

    今天同学让帮忙看下JVM错误日志,才发现已经开始接触java3个月,还没看到相关错误日志.平时看的都只是程序运行时写入的日志,关于JVM的错误日志还真没看过.网上收集资料,整理如下. 一.日志文件: ...

  3. 软件项目技术点(8)—— canvas调用drawImage绘制图片

    AxeSlide软件项目梳理   canvas绘图系列知识点整理 html5中标签canvas,函数drawImage(): 使用drawImage()方法绘制图像.绘图环境提供了该方法的三个不同版本 ...

  4. SharePoint 2013 - Add-ins

    1. App Web & Host Web The special website to which the app is deployed is called an App Web. The ...

  5. HTML 5入门知识(四)

    表单的作用 表单不是表格,既不用来显示数据,也不用来布局网页.表单提供一个界面,一个入口,便于用户把数据提交给后台程序进行处理. 表单的数据传递方式method属性 表单的method属性用于指定在数 ...

  6. 【转】python安装库

    1.打开网址https://pypi.python.org/pypi/numpy,找到安装的python版本对应的numpy版本. 我的python版本是 下载的对应numpy版本是 2.将numpy ...

  7. BIEE入门(二)物理层的定义

    使用BIEE的第一步是使用admintool去建立一个多维数据模型,而建立多维数据模型的第一步则是建立物理层,请注意因为BIEE本身并不存 储数据,所以所谓BIEE物理层的意义是需要在BIEE里建立各 ...

  8. Azure镜像市场再下一城,中标软件入驻开启Azure国产操作系统时代

    近日,中标软件成功入驻 Azure 镜像市场,提供中标麒麟 Linux 的产品镜像服务,这样一来,中标麒麟也成为国内唯一能够在 Azure 公有云上运行的国产操作系统产品. 作为国内操作系统的领头羊, ...

  9. how to create folders in batches

    you need be good at thinking when you see problem. Work experience:when you need to copy web chinese ...

  10. 用AutoHotkey调整Windows音量

    我用了[右Alt]+方向键来调整音量:Alt+上下键,音量调整幅度为5,如果再增加个右Ctrl,音量调整幅度为1. Alt+左键为静音,Alt+右键为最大音量. >!Up:: ;音量+ < ...