图论之强连通复习开始- -

题目大意:给你一个有向图,要你求出这样的点集:从这个点出发能到达的点,一定能回到这个点

思路:强连通分量里的显然都可以互相到达 那就一起考虑,缩点后如果一个点有出边,一定不在点集内,因为缩点后是DAG,无环,因此一定不能回到原来的点,所以找到出度为0的点即可

#include<cstdio>

#include<string.h>

#include<math.h>

#include<algorithm>

#include<iostream>

#include<queue>

#define maxn 90000

#define inf 0x3f3f3f3f

using namespace std;

int head[maxn],next[maxn],point[maxn],now=0;

int dfn[maxn],low[maxn],time,col,stack[maxn];

int top,belong[maxn],out[maxn];

bool instack[maxn];

void add(int x,int y)

{

next[++now]=head[x];

head[x]=now;

point[now]=y;

}

void tarjan(int k)

{

int u;

dfn[k]=low[k]=++time;

instack[k]=1;

stack[++top]=k;

for(int i=head[k];i;i=next[i])

{

u=point[i];

if(dfn[u]==0)

{

tarjan(u);

low[k]=min(low[u],low[k]);

}

else if(instack[u])low[k]=min(low[k],low[u]);

}

if(low[k]==dfn[k])

{

++col;

do

{

u=stack[top--];

instack[u]=0;

belong[u]=col;

}while(u!=k);

}

}

int main()

{

int n,m,x,y;

while(1)

{

scanf("%d",&n);

if(n==0)break;

scanf("%d",&m);

now=0;memset(head,0,sizeof(head));

top=0;memset(instack,0,sizeof(instack));

memset(out,0,sizeof(out));

memset(dfn,0,sizeof(dfn));

for(int i=1;i<=m;i++)

{

scanf("%d%d",&x,&y);

add(x,y);

}

for(int i=1;i<=n;i++)if(dfn[i]==0)tarjan(i);

for(int i=1;i<=n;i++)

{

for(int j=head[i];j;j=next[j])

{

int u=point[j];

if(belong[i]!=belong[u])out[belong[i]]++;

}

}

int flag=1;

for(int i=1;i<=n;i++)

{

if(flag && out[belong[i]]==0)

{

printf("%d",i);

flag^=flag;

}

else if(out[belong[i]]==0)printf(" %d",i);

}

printf("\n");

}

return 0;

}

POJ 2553 The Bottom of a Graph 【scc tarjan】的更多相关文章

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

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

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

    POJ 2553 The Bottom of a Graph 题目链接 题意:给定一个有向图,求出度为0的强连通分量 思路:缩点搞就可以 代码: #include <cstdio> #in ...

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

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

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

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

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

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

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

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

  7. poj 2553 The Bottom of a Graph : tarjan O(n) 存环中的点

    /** problem: http://poj.org/problem?id=2553 将所有出度为0环中的点排序输出即可. **/ #include<stdio.h> #include& ...

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

  9. poj 2553 The Bottom of a Graph

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

随机推荐

  1. [转]Android APK签名原理及方法

    准备知识:数据摘要 这个知识点很好理解,百度百科即可,其实他也是一种算法,就是对一个数据源进行一个算法之后得到一个摘要,也叫作数据指纹,不同的数据源,数据指纹肯定不一样,就和人一样. 消息摘要算法(M ...

  2. Spring的校验(Validator)

    使用Spring校验的大体流程: 最首先要有配置文件xml的支持(spring_validate.xml).(当然在web.xml中要有对该xml的体现) <beans xmlns=" ...

  3. 复习-PEP8规范(转)

    PEP8 Python 编码规范 一 代码编排1 缩进.4个空格的缩进(编辑器都可以完成此功能),不使用Tap,更不能混合使用Tap和空格.2 每行最大长度79,换行可以使用反斜杠,最好使用圆括号.换 ...

  4. CF782A Andryusha and Socks

    题意: Andryusha is an orderly boy and likes to keep things in their place. Today he faced a problem to ...

  5. postgresql 9.4.17 64位安装说明

    本文在win 7 64位下安装postgresql 9.4.17 64位版,安装过程有录入项的界面都有截图 运行安装程序开始安装 开始时可能自动安装c++运行环境 然后正式开始安装 安装目录位置,不要 ...

  6. android studio 生成 jniLibs 目录

    现在一般的项目都会加入第三方jar包,第三方jar包我们会新建一个文件夹:libs,然后jar包都放在这个文件夹中. 但我们会发现,只是新建一个文件加之后,在AndroidStudio的左侧并不会出现 ...

  7. 关于 propertychange 兼容性问题

    on 事件 $('body').on('property input','.class',function(){ alert(123); });

  8. docker的网络配置

    Docker的4种网络模式 我们在使用docker run创建Docker容器时,可以用–net选项指定容器的网络模式,Docker有以下4种网络模式: host模式:使用–net=host指定. c ...

  9. DROP SEQUENCE - 删除一个序列

    SYNOPSIS DROP SEQUENCE name [, ...] [ CASCADE | RESTRICT ] DESCRIPTION 描述 DROP SEQUENCE 从数据库中删除序列号生成 ...

  10. docker 框架概述

    docker的框架 docker 使用传统的client-server架构模式,用户端通过docker client 与docker  daemon 建立通信,并将请求发送给后者,而docker后端时 ...