POJ 2553 The Bottom of a Graph(强连通分量)
POJ 2553 The Bottom of a Graph
题意:给定一个有向图,求出度为0的强连通分量
思路:缩点搞就可以
代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <stack>
using namespace std; const int N = 5005; int n, m;
vector<int> g[N], save[N];
stack<int> S; int pre[N], dfn[N], dfs_clock, sccno[N], sccn; void dfs_scc(int u) {
pre[u] = dfn[u] = ++dfs_clock;
S.push(u);
for (int i = 0; i < g[u].size(); i++) {
int v = g[u][i];
if (!pre[v]) {
dfs_scc(v);
dfn[u] = min(dfn[u], dfn[v]);
} else if (!sccno[v]) dfn[u] = min(dfn[u], pre[v]);
}
if (dfn[u] == pre[u]) {
sccn++;
save[sccn].clear();
while (1) {
int x = S.top(); S.pop();
save[sccn].push_back(x);
sccno[x] = sccn;
if (x == u) break;
}
}
} void find_scc() {
dfs_clock = sccn = 0;
memset(pre, 0, sizeof(pre));
memset(sccno, 0, sizeof(sccno));
for (int i = 1; i <= n; i++)
if (!pre[i]) dfs_scc(i);
} int out[N];
int ans[N], an; int main() {
while (~scanf("%d", &n) && n) {
for (int i = 1; i <= n; i++) g[i].clear();
scanf("%d", &m);
int u, v;
while (m--) {
scanf("%d%d", &u, &v);
g[u].push_back(v);
}
find_scc();
memset(out, 0, sizeof(out));
for (int i = 1; i <= n; i++) {
for (int j = 0; j < g[i].size(); j++) {
int v = g[i][j];
if (sccno[i] != sccno[v]) {
out[sccno[i]]++;
}
}
}
an = 0;
for (int i = 1; i <= sccn; i++) {
if (!out[i]) {
for (int j = 0; j < save[i].size(); j++)
ans[an++] = save[i][j];
}
}
sort(ans, ans + an);
for (int i = 0; i < an; i++)
printf("%d%c", ans[i], i == an - 1 ? '\n' : ' ');
}
return 0;
}
POJ 2553 The Bottom of a Graph(强连通分量)的更多相关文章
- 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 (强连通分量)
题目地址:POJ 2553 题目意思不好理解.题意是:G图中从v可达的全部点w,也都能够达到v,这种v称为sink.然后升序输出全部的sink. 对于一个强连通分量来说,全部的点都符合这一条件,可是假 ...
- poj - 2186 Popular Cows && poj - 2553 The Bottom of a Graph (强连通)
http://poj.org/problem?id=2186 给定n头牛,m个关系,每个关系a,b表示a认为b是受欢迎的,但是不代表b认为a是受欢迎的,关系之间还有传递性,假如a->b,b-&g ...
- 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[Tarjan强连通分量]
题意: 求出度为0的强连通分量. 思路: 缩点 具体有两种实现: 1.遍历所有边, 边的两端点不在同一强连通分量的话, 将出发点所在强连通分量出度+1. #include <cstdio> ...
- POJ 2553 The Bottom of a Graph(强连通分量的出度)
题意: 求出图中所有汇点 定义:点v是汇点须满足 --- 对图中任意点u,若v可以到达u则必有u到v的路径:若v不可以到达u,则u到v的路径可有可无. 模板:http://www.cnblogs.co ...
- 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
求解的是有向图中满足“自己可达的顶点都能到达自己”的顶点个数如果强连通分量中某个顶点,还能到达分量外的顶点,则该连通分量不满足要求// 因此,本题要求的是将强连通分量缩点后所构造的新图中出度为0的顶点 ...
随机推荐
- 笔记 — 动画效果(Css3)
/** * animation-name: 调用 @keyframes 所定义的动画 * animation-duration: 动画周期所花费的时间长度 * animation-timing-fun ...
- Android Fragment与Activity交互的几种方式
这里我不再详细介绍那写比较常规的方式,例如静态变量,静态方法,持久化,application全局变量,收发广播等等. 首先我们来介绍使用Handler来实现Fragment与Activity 的交互. ...
- spring - quartz - experssion 表达式
字段 允许值 允许的特殊字符 秒 0-59 , - * / 分 0-59 , - * / 小时 0-23 , - * / 日期 1-31 , - * ? / L W C 月份 1-12 或者 JAN- ...
- mysql自动添加时间的方法
时间添加方法,可以在编辑数据时方便时间选择输入: 将时间列DataType设为timestamp,设定其默认值为CURRENT_TIMESTAMP. 这样每次插入一条新纪录,数据库会自动在时间段存储当 ...
- sphinx在windows下的简单安装与使用
1.下载地址 http://sphinxsearch.com/downloads/release/,我这里下的是“Win64 binaries w/MySQL+PgSQL+libstemmer+id6 ...
- ubuntu操作系统的目录结构
/:根目录,是所有目录的绝对路径的起始点.一般根目录下只存放目录,不要存放文件,/etc./bin./dev./lib./sbin应该和根目录放置在一个分区中 /bin (类似的还有/usr/bin) ...
- sql server 查询时间 格式化输出
use test select * from vote insert into vote (contents) values(GETDATE()) insert into vote (contents ...
- 【转载】MySQL之CONCAT()的用法
mysql CONCAT()函数用于将多个字符串连接成一个字符串,是最重要的mysql函数之一,下面就将为您详细介绍mysql CONCAT()函数,供您参考 mysql CONCAT(str1,st ...
- PAT_A1127#ZigZagging on a Tree
Source: PAT A1127 ZigZagging on a Tree (30 分) Description: Suppose that all the keys in a binary tre ...
- scrapy-redis 之处理异常
今天心情不好 不想多打字 自己看注释吧 from scrapy.http import HtmlResponse from twisted.internet import defer from twi ...