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(强连通分量)的更多相关文章

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

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

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

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

  3. 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 ...

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

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

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

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

  6. POJ 2553 The Bottom of a Graph(强连通分量的出度)

    题意: 求出图中所有汇点 定义:点v是汇点须满足 --- 对图中任意点u,若v可以到达u则必有u到v的路径:若v不可以到达u,则u到v的路径可有可无. 模板:http://www.cnblogs.co ...

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

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

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

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

  9. poj 2553 The Bottom of a Graph

    求解的是有向图中满足“自己可达的顶点都能到达自己”的顶点个数如果强连通分量中某个顶点,还能到达分量外的顶点,则该连通分量不满足要求// 因此,本题要求的是将强连通分量缩点后所构造的新图中出度为0的顶点 ...

随机推荐

  1. MySQL数据库中的delete语句

    在MySQL数据库中,只有在数据存在的情况下删除,才会返回受影响的行数.比如大于0的数,如果删除了不存在的数据,则会返回0:

  2. HTML学习(2018.1.18)

    1,         转义字符 转义字符:用于表示网页中的特殊字符 XHTML不直接输入符号,建议使用转义字符. &nbsp------空格: &copy------版权: & ...

  3. Java常见错误整理(一)

    1.配置完Java环境变量之后,仍然不能使用java命令. 解决方法: 如果是Windows10系统出现此问题,是因为个别Windows10系统不识别“JAVA_HOME”环境变量,将path中所有的 ...

  4. 《java数据结构与算法》系列之“快速排序"

    部门没人了,公司动作好快...算了,不想了!还是学知识吧,只有它不会让自己失望. 继续我的算法学习,快速排序是应用很广的算法,看了一早上才看懂些,感觉比冒泡之类的难理解,可能主要是递归那块自己不是很理 ...

  5. Excel常用的小技巧

    1.Excel如何实现单元格内轻松换行:按住ALT+enter就可以了. 2.Excel固定表头:在“视图”>冻结窗口>冻结首行. 3.防止电脑突然断电,导致正在编辑的Excel数据丢失, ...

  6. 【python】os.getcwd和getcwdu

    print os.getcwd(), type(os.getcwd()) print os.getcwdu(), type(os.getcwdu()) 结果如下: C:\Users\Administr ...

  7. 对学Oracle数据库初学者的开场篇

    前言:因为项目原因,近期开始学习Oracle数据库.Oracle是目前最流行的数据库之一,功能强大,性能卓越,相对的学习的难度还是不小.我打算将自己的学习过程记录下来,做个积累,方便自己和其他的学习者 ...

  8. java rsa加密解密

  9. Linux 重要文件目录

    文件系统层次化标准(Filesystem Hierarchy Standard)[FHS] 树形结构 /boot 开机所需文件——内核开机菜单以及所需的配置文件等 /dev 以文件形式存放任何设备与接 ...

  10. python 遍历xml所有节点

    1.xml文件 2.代码 #coding:utf-8 import xml import xml.etree.ElementTree as ET """ 实现从xml文件 ...