/**
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. Scala 知识点掌握1

    Scala知识点巩固 1.Scala简介 Scala是一门面向对象和面向函数的编程语言,是一门静态编程语言,如 Java Scala(变量类型在编译阶段确定):源码文件需要基于 JVM 运行的. 动态 ...

  2. flask-session总结

    一.session       session和cookie的原理和区别: cookie是保存在浏览器上的键值对             session是存在服务端的键值对(服务端的session就是 ...

  3. es6新语法的使用

    1.声明变量: let 声明变量 作用域代码块作用域{} 尽在模块 先使用后声明 会报错 { let a= 12; alert(a) } let 不允许重复声明同一个变量 const 声明是一个常量, ...

  4. 节点nodeName与nodeValue表

  5. Html5 突破微信限制实现大文件分割上传

    先来前端代码 <!DOCTYPE html> <html> <head> <meta name="viewport" content=&q ...

  6. 论文投稿Cover letter

    转自:http://blog.sciencenet.cn/blog-479412-686426.html,感谢分享! 1.第一次投稿Cover letter:主要任务是介绍文章主要创新以及声明没有一稿 ...

  7. Django初步(一):安装和简单演示

    参考:Windows下安装Django:http://www.th7.cn/Program/Python/201305/136301.shtml

  8. Select selectedIndex 属性

    定义和用法 selectedIndex 属性可设置或返回下拉列表中被选选项的索引号. 注意: 若允许多重选择,则仅会返回第一个被选选项的索引号. 语法 设置 selectedIndex 属性: sel ...

  9. 获取apk package name(包名)以及activity name

    通过adb 查看最上层成activity名字: linux: adb shell dumpsys activity | grep "mFocusedActivity" window ...

  10. 利用SQL Server Management Studio(SSMS)复制数据库

    利用SQL Server Management Studio(SSMS)复制数据库 标签(空格分隔): SQLServer 前言 今天由于客户购买的软件版本确认了,而之前进行开发的本地数据库版本较低, ...