poj 2553 The Bottom of a Graph : tarjan O(n) 存环中的点
/**
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) 存环中的点的更多相关文章
- 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 TarJan算法题解
本题分两步: 1 使用Tarjan算法求全部最大子强连通图.而且标志出来 2 然后遍历这些节点看是否有出射的边,没有的顶点所在的子强连通图的全部点,都是解集. Tarjan算法就是模板算法了. 这里使 ...
- [poj 2553]The Bottom of a Graph[Tarjan强连通分量]
题意: 求出度为0的强连通分量. 思路: 缩点 具体有两种实现: 1.遍历所有边, 边的两端点不在同一强连通分量的话, 将出发点所在强连通分量出度+1. #include <cstdio> ...
- POJ 2553 The Bottom of a Graph(强连通分量)
POJ 2553 The Bottom of a Graph 题目链接 题意:给定一个有向图,求出度为0的强连通分量 思路:缩点搞就可以 代码: #include <cstdio> #in ...
- 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: ...
- POJ 2553 The Bottom of a Graph (强连通分量)
题目地址:POJ 2553 题目意思不好理解.题意是:G图中从v可达的全部点w,也都能够达到v,这种v称为sink.然后升序输出全部的sink. 对于一个强连通分量来说,全部的点都符合这一条件,可是假 ...
- POJ 2553 The Bottom of a Graph 【scc tarjan】
图论之强连通复习开始- - 题目大意:给你一个有向图,要你求出这样的点集:从这个点出发能到达的点,一定能回到这个点 思路:强连通分量里的显然都可以互相到达 那就一起考虑,缩点后如果一个点有出边,一定不 ...
随机推荐
- java编程中'为了性能'一些尽量做到的地方
原文地址:http://blog.csdn.NET/m13666368773/article/details/7796924 最近的机器内存又爆满了,出了新增机器内存外,还应该好好review一下我们 ...
- VUE-地区选择器(V-Distpicker)
V - Distpicker 地区选择器环境问题不多说,自己看文档,主要讲一下在实际使用过程中如何将下拉框的值赋值到对象属性上.文档: https://distpicker.pigjian.com/g ...
- Drupal Module Hooks
Drupal is a Content Management System. Drupal is also deeply, deeply weird. While systems like Magen ...
- SQL 出现18456
SQL Server 2008R2 18456错误解决方案 SQL Server 2008R2 18456错误解决方案 微软解释说,因密码或用户名错误而使身份验证失败并导致连接尝试被拒时,类似下面 ...
- liunx增强命令
查找命令 grep 格式:grep [option] pattern [file] 实例: ps -ef | grep sshd 查找指定 ssh 服务进程 ps -ef | grep sshd | ...
- 模拟精灵 z
反复做历史测试,重大改进了卖出的判断模式.此项改进能使系统收益每年增加5%-左右 重新整合了几种买法,使之在熊市更加谨慎.对大盘的反转反应更为灵敏 适当加大了仓位 单独处理有重大机会的股票 加入多种短 ...
- FIFO认识(一)
1.什么是FIFO? FIFO是英文First In First Out 的缩写,是一种先进先出的数据缓存器,他与普通存储器的区别是没有外部读写地址线,这样使用起来非常简单,但缺点就是只能顺序写入数据 ...
- CryptoSwift:密码学
Hash (Digest) MD5 | SHA1 | SHA224 | SHA256 | SHA384 | SHA512 | SHA3 Cyclic Redundancy Check (CRC) CR ...
- HDU 3625 第一类斯特林数
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3625 题意: n个房间,房间里面放着钥匙,允许破门而入k个,拿到房间里面的钥匙后可以打开对应的门,但是 ...
- prior_box层
https://www.jianshu.com/p/5195165bbd06 1.step_w.step_h其实就相当于faster中的feat_stride,也就是把这些点从feature map映 ...