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

Description

We will use the following (standard) definitions from graph theory. Let V be a nonempty and finite set, its elements being called vertices (or nodes). Let E be a subset of the Cartesian product V×V, its elements being called edges. Then G=(V,E) is called a directed graph. 
Let n be a positive integer, and let p=(e1,...,en) be a sequence of length n of edges ei∈E such that ei=(vi,vi+1) for a sequence of vertices (v1,...,vn+1). Then p is called a path from vertex v1 to vertex vn+1 in G and we say that vn+1 is reachable from v1, writing (v1→vn+1)
Here are some new definitions. A node v in a graph G=(V,E) is called a sink, if for every node w in G that is reachable from vv is also reachable from w. The bottom of a graph is the subset of all nodes that are sinks, i.e., bottom(G)={v∈V|∀w∈V:(v→w)⇒(w→v)}. You have to calculate the bottom of certain graphs.

Input

The input contains several test cases, each of which corresponds to a directed graph G. Each test case starts with an integer number v, denoting the number of vertices of G=(V,E), where the vertices will be identified by the integer numbers in the set V={1,...,v}. You may assume that 1<=v<=5000. That is followed by a non-negative integer e and, thereafter, e pairs of vertex identifiers v1,w1,...,ve,we with the meaning that (vi,wi)∈E. There are no edges other than specified by these pairs. The last test case is followed by a zero.

Output

For each test case output the bottom of the specified graph on a single line. To this end, print the numbers of all nodes that are sinks in sorted order separated by a single space character. If the bottom is empty, print an empty line.

Sample Input

3 3
1 3 2 3 3 1
2 1
1 2
0

Sample Output

1 3
2

Source

定义:点v是汇点须满足 --- 对图中任意点u,若v可以到达u则必有u到v的路径;若v不可以到达u,则u到v的路径可有可无。

题意:在n个点m条边的有向图里面,问有多少个点是汇点。

分析:若SCC里有一点不是汇点,那么它们全不是,反之也如此。所以一个SCC里面的点要么全是,要么全不是。在求出SCC并缩点后,任一个编号为A的SCC若存在指向编号为B的SCC的边,那么其所有点必不是汇点(因为编号为B的SCC不可能存在指向编号为A的SCC的边)。若编号为A的SCC没有到达其他SCC的路径,那么该SCC里面所有点必是汇点。因此判断的关键在于SCC的出度是否为0.

思路:先用tarjan求出所有SCC,然后缩点后找出所有出度为0的SCC,并用数字存储点,升序排列后输出。

代码:

 #include"bits/stdc++.h"

 #define db double
#define ll long long
#define vl vector<ll>
#define ci(x) scanf("%d",&x)
#define cd(x) scanf("%lf",&x)
#define cl(x) scanf("%lld",&x)
#define pi(x) printf("%d\n",x)
#define pd(x) printf("%f\n",x)
#define pl(x) printf("%lld\n",x)
#define rep(i, n) for(int i=0;i<n;i++)
using namespace std;
const int N = 1e6 + ;
const int mod = 1e9 + ;
const int MOD = ;
const db PI = acos(-1.0);
const db eps = 1e-;
const ll INF = 0x3fffffffffffffff;
int n,m;
struct P{int to,nxt;}e[N];
int head[N];
bool ins[N];
int beg[N];
int low[N],dfn[N];
int out[N];
stack<int> s; int cnt,num,id;
void add(int u,int v){
e[cnt].to=v;
e[cnt].nxt=head[u];
head[u]=cnt++;
}
void tarjan(int u)
{
low[u]=dfn[u]=++id;
ins[u]=;
s.push(u);
for(int i=head[u];~i;i=e[i].nxt){
int v=e[i].to;
if(!dfn[v]) tarjan(v),low[u]=min(low[u],low[v]);
else if(ins[v]) low[u]=min(low[u],dfn[v]);
}
if(low[u]==dfn[u]){
int v;
do{
v=s.top();s.pop();
beg[v]=num;//强连通分量编号
ins[v]=;
}while(u!=v);
num++;
}
}
int main(){
while (scanf("%d%d",&n,&m)==&&n){
memset(head,-, sizeof(head));
memset(low,, sizeof(low));
memset(dfn,, sizeof(dfn));
memset(ins,, sizeof(ins));
memset(out,, sizeof(out));
cnt=num=id=;
for(int i=;i<m;i++){
int x,y;
ci(x),ci(y);
add(x,y);
}
for(int i=;i<=n;i++)
if(!dfn[i]) tarjan(i);
for(int i=;i<=n;i++){
for(int j=head[i];~j;j=e[j].nxt){
int v=e[j].to;
if(beg[i]!=beg[v]) out[beg[i]]++;
}
}
bool ok=;
for(int i=;i<=n;i++){
if(!out[beg[i]]){//所在强连通分量出度为0
if(ok) printf("%d",i),ok=;
else printf(" %d",i);
}
}
puts("");
}
return ;
}

POJ2553 汇点个数(强连通分量的更多相关文章

  1. Ex3_15 判断图是否是一个强连通分量 判断点是否在汇点强连通分量中_十一次作业

    (a) 可以用图中的每一个顶点表示街道中的每个十字路口,由于街道都是单行的,所以图是有向图,若从一个十字路口都有一条合法的路线到另一个十字路口,则图是一个强连通图.即要验证的是图是否是一个强连通图. ...

  2. HDU 1269 迷宫城堡(判断有向图强连通分量的个数,tarjan算法)

    迷宫城堡 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  3. POJ3180(有向图强连通分量结点数>=2的个数)

    The Cow Prom Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1451   Accepted: 922 Descr ...

  4. POJ2186 (强连通分量缩点后出度为0的分量内点个数)

    Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 27820   Accepted: 11208 De ...

  5. UVALive 4262——Trip Planning——————【Tarjan 求强连通分量个数】

    Road Networks Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Stat ...

  6. poj2186--tarjan+缩点(有向图的强连通分量中点的个数)

    题目大意:       每一头牛的愿望就是变成一头最受欢迎的牛.现在有N头牛,给你M对整数(A,B),表示牛A认为牛B受欢迎. 这 种关系是具有传递性的,如果A认为B受欢迎,B认为C受欢迎,那么牛A也 ...

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

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

  8. 【poj2553】The Bottom of a Graph(强连通分量缩点)

    题目链接:http://poj.org/problem?id=2553 [题意] 给n个点m条边构成一幅图,求出所有的sink点并按顺序输出.sink点是指该点能到达的点反过来又能回到该点. [思路] ...

  9. (转)求有向图的强连通分量个数(kosaraju算法)

    有向图的连通分量的求解思路 kosaraju算法 逛了很多博客,感觉都很难懂,终于找到一篇能看懂的,摘要记录一下 原博客https://www.cnblogs.com/nullzx/p/6437926 ...

随机推荐

  1. bind 详解

    请看我的有道云笔记: http://note.youdao.com/noteshare?id=eaf4194473cf4294776fbc263ffe6b89&sub=5CB214C594E0 ...

  2. 一点一点学写Makefile-1

    相信很多Linux开发者 都得自己来写Makefile,刚开始学习学写这个的时候都会碰到很多困难,我之前没有自己独立完成过Makefile,都是在公司已有的模板上添加.现在突然有一个很大的想法就是从零 ...

  3. Jerry的CRM Middleware(中间件)文章合集

    我在SAP成都研究院做过的CRM中间件的项目其实并不是很多: 1. 2013年下半年和2014年上半年曾经支持过中联重科和蒙牛的CRM项目相关的中间件问题; 2. 2014年上半年做过一个CRM物料主 ...

  4. python接口测试-项目实践(二)获取接口响应,取值(re、json)

    一 分别请求3个接口,获取响应. 第三方接口返回有两种:1 纯字符串  2 带bom头的json字串 import requests api1 = 'url1' response1 = request ...

  5. sublime text3 英文版转为中文版

    第一步设置好:https://packagecontrol.io/installation#st3 简单几步 : 1. 点击菜单栏中“preferences”,弹出选项中找到“package cont ...

  6. oracle spatial下对wkt字符串操作遇到srid的解决方案

    <span style="font-size:18px;">select fid from vgnss where SDO_WITHIN_DISTANCE(geom,  ...

  7. react中修改antd的默认样式

    最近在做react+antd项目.不可避免的遇到了修改antd默认样式的问题. 比如,table组件的表头背景色设置,如果直接使用元素样式,会修改整个项目的table.这里我用的方法是,给table添 ...

  8. php常见的几种排序以及二分法查找

    <?php 1.插入排序 思想: 每次将一个待排序的数据元素插入到前面已经排好序的数列中,使数列依然有序,知道待排序数据元素全部插入完为止. 示例: [初始关键字] [49] 38 65 97 ...

  9. AngularJS web应用程序

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  10. Vue 封装的组件生命周期钩子

    export default { // ... // 在组件初始化时调用,可以简单理解为页面加载时 created () { // 存在 localStorage 的缓存内容 if (localSto ...