题目链接:http://poj.org/problem?id=2553

【题意】

给n个点m条边构成一幅图,求出所有的sink点并按顺序输出。sink点是指该点能到达的点反过来又能回到该点。

【思路】

不难想象sink点一定是在强连通分量中,而且强连通分量缩点后出度为0,就可以说明该强连通分量内所有的点都是sink点。

之前wa了一发是因为写成了out[i],注意是从缩点构成的dag中找出度为0的点,而不是从原来的图中找。

【ac代码】

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <stack>
#include <vector>
#include <cstring>
using namespace std;
const int N = ;
int low[N], vis[N], dfn[N], col[N], out[N], ans[N];
vector<int>V[N];
stack<int>s;
int n, cnt, num;
void dfs(int u)
{
s.push(u);
vis[u] = ;
dfn[u] = low[u] = ++cnt;
for (int i = ; i < V[u].size(); i++)
{
int v = V[u][i];
if (!dfn[v])
{
dfs(v);
low[u] = min(low[u], low[v]);
}
else if (vis[v])
low[u] = min(low[u], dfn[v]);
}
if (low[u] == dfn[u])
{
int t;
num++;
do
{
t = s.top();
s.pop();
col[t] = num;
vis[t] = ;
}
while (t != u);
}
} void tarjan()
{
int i;
memset(dfn, , sizeof(dfn));
memset(low, , sizeof(low));
memset(vis, , sizeof(vis));
memset(col, , sizeof(col));
while (!s.empty()) s.pop();
cnt = num = ;
for (i = ; i <= n; i++)
if (!dfn[i]) dfs(i);
}
int main()
{
int m, i, j;
while(scanf("%d", &n), n)
{
scanf("%d", &m);
for(i = ; i <= n; i++) V[i].clear();
int a, b;
while(m--)
{
scanf("%d%d", &a, &b);
V[a].push_back(b);
}
tarjan();
memset(out, , sizeof out);
for(i = ; i <= n; i++)
for(j = ; j < V[i].size(); j++)
{
int v = V[i][j];
if(col[i] != col[v]) out[col[i]]++;//该颜色出度+1
}
cnt = ;
for(i = ; i <= n; i++)
if(!out[col[i]]) ans[++cnt] = i;
sort(ans+, ans++cnt);
for(i = ; i < cnt; i++) printf("%d ", ans[i]);
printf("%d\n", ans[cnt]);
}
return ;
}

【poj2553】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-2552-The Bottom of a Graph 强连通分量

    链接: https://vjudge.net/problem/POJ-2553 题意: We will use the following (standard) definitions from gr ...

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

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

  4. POJ2553 The Bottom of a Graph(强连通分量+缩点)

    题目是问,一个有向图有多少个点v满足∀w∈V:(v→w)⇒(w→v). 把图的强连通分量缩点,那么答案显然就是所有出度为0的点. 用Tarjan找强连通分量: #include<cstdio&g ...

  5. POJ1236Network of Schools[强连通分量|缩点]

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16571   Accepted: 65 ...

  6. POJ1236Network of Schools(强连通分量 + 缩点)

    题目链接Network of Schools 参考斌神博客 强连通分量缩点求入度为0的个数和出度为0的分量个数 题目大意:N(2<N<100)各学校之间有单向的网络,每个学校得到一套软件后 ...

  7. HD2767Proving Equivalences(有向图强连通分量+缩点)

    题目链接 题意:有n个节点的图,现在给出了m个边,问最小加多少边是的图是强连通的 分析:首先找到强连通分量,然后把每一个强连通分量缩成一个点,然后就得到了一个DAG.接下来,设有a个节点(每个节点对应 ...

  8. UVa11324 The Largest Clique(强连通分量+缩点+记忆化搜索)

    题目给一张有向图G,要在其传递闭包T(G)上删除若干点,使得留下来的所有点具有单连通性,问最多能留下几个点. 其实这道题在T(G)上的连通性等同于在G上的连通性,所以考虑G就行了. 那么问题就简单了, ...

  9. ZOJ3795 Grouping(强连通分量+缩点+记忆化搜索)

    题目给一张有向图,要把点分组,问最少要几个组使得同组内的任意两点不连通. 首先考虑找出强连通分量缩点后形成DAG,强连通分量内的点肯定各自一组,两个强连通分量的拓扑序能确定的也得各自一组. 能在同一组 ...

随机推荐

  1. UML_visio安装及使用

    安装:百度云安装visio 64bit 类图在visio当中 用例图                                 来自为知笔记(Wiz)

  2. hibernatetemplate find条件查询方法

    一.find(String queryString); 示例:this.getHibernateTemplate().find("from bean.User"); 返回所有Use ...

  3. Eclipse 中安装Browse Deployment Location插件

    Browse Deployment Location 功能的插件名称为: Pivotal tc Server Integration for Eclipse  可在Eclippse的Eclipse M ...

  4. $python打包工具pyinstaller的用法

    pyinstaller是一个很好用的python打包工具,在Windows环境下可以将python脚本打包成一个exe可执行文件,并且脚本中所依赖的各种第三方库在打包时候都会被统一处理到一起,这样打包 ...

  5. Ubuntu启动文件破坏启动恢复方法

    reboot后主机登录显示如下图: 解决步骤: 1.fs0:(回车) 2.edit startup.nsh 3.添加下面字段: fs0: cd EFI/ubuntu grubx64.efi 4.重启即 ...

  6. Django CRM 数据库增删改查

    原文链接 http://www.cnblogs.com/yangmv/p/5327477.html

  7. Saltstack 命令参数整理

    命令:salt-key # 测试 命令:salt-key -A # 管理所有minion 命令:salt-key -a # 管理定义minion 命令:salt-key -d ID名字 # 删除单个m ...

  8. Grid 行和列

    <Grid> <Grid.ColumnDefinitions> <ColumnDefinition></ColumnDefinition> <Co ...

  9. ubuntu 刚更改默认python3版本后更新包等

    一般来说ubuntu 刚更改为python3为默认版本后要进行一下更新包等等的内容(当然不更新一下也是可以的,最好更新一下,第一次更新较慢) 使用下面两行代码: sudo apt-get update ...

  10. php操作redis cluster集群成功实例

    java操作redis cluster集群可使用jredis php要操作redis cluster集群有两种方式: 1.使用phpredis扩展,这是个c扩展,性能更高,但是phpredis2.x扩 ...