poj 2553 The Bottom of a Graph
求解的是有向图中满足“自己可达的顶点都能到达自己”的顶点个数
如果强连通分量中某个顶点,还能到达分量外的顶点,则该连通分量不满足要求
// 因此,本题要求的是将强连通分量缩点后所构造的新图中出度为0的顶点个数 // 如果某个新图的节点出度数为0 且是缩点而来 那么该强连通分量里面的点都是符合要求的
#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
#include <math.h>
#include <stdio.h>
#include <string.h>
using namespace std;
#define MOD 1000000007
#define maxn 60100
#define maxm 10010
struct Edge{
int to;
int next;
Edge(){};
Edge(int u,int v){to=u;next=v;}
}E[maxn];
stack<int> S;
int V[maxm],num;
int belong[maxm];
int pre[maxm];
int dfst,scc;
int ans;
bool tag[maxm];
int out[maxm];
void init(int n){
dfst=scc=;
num=;
ans=;
while(!S.empty())
S.pop();
for(int i=;i<=n;i++){
V[i]=-;
pre[i]=;
belong[i]=;
}
}
void add(int u,int v){
E[num].to=v;
E[num].next=V[u];
V[u]=num++;
}
int tarjan(int u){
int lowu=pre[u]=++dfst;
int v,e;
S.push(u);
for(e=V[u];e!=-;e=E[e].next){
v=E[e].to;
if(!pre[v]){
int lowv=tarjan(v);
lowu=min(lowu,lowv);
}
else if(!belong[v]) lowu=min(lowu,pre[v]);
}
if(lowu==pre[u]){
scc++;
for(;;){
int x=S.top();S.pop();
belong[x]=scc;
if(x==u) break;
}
}
return lowu;
}
int main()
{
int n,m,T;
int u,v;
int i,j=;
//scanf("%d",&T);
while(scanf("%d",&n),n){
scanf("%d",&m);
init(n);
for(i=;i<=m;i++){
scanf("%d %d",&u,&v);
add(u,v);
}
for(i=;i<=n;i++)
if(!pre[i]) tarjan(i);
// for(i=1;i<=n;i++) printf("%d ",belong[i]);
for(i=;i<=scc;i++) out[i]=,tag[i]=;
int e,u,v;
for(i=;i<=n;i++)
{
for(e=V[i];e!=-;e=E[e].next){
u=belong[i];
v=belong[E[e].to];
if(u!=v)
out[u]++;//,printf("v=%d ",v);
}
}
int flag=,rc;
for(i=;i<=scc;i++) if(!out[i]) tag[i]=,flag=;
if(!flag){printf("\n\n");continue;}
flag=;
for(i=;i<=n;i++)
if(tag[belong[i]]) {
if(flag)printf(" %d",i);
else {flag=;printf("%d",i);}
}
printf("\n");
}
return ;
}
poj 2553 The Bottom of a Graph的更多相关文章
- POJ 2553 The Bottom of a Graph(强连通分量)
POJ 2553 The Bottom of a Graph 题目链接 题意:给定一个有向图,求出度为0的强连通分量 思路:缩点搞就可以 代码: #include <cstdio> #in ...
- 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 (Tarjan)
The Bottom of a Graph Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 11981 Accepted: ...
- 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 (强连通分量)
题目地址:POJ 2553 题目意思不好理解.题意是:G图中从v可达的全部点w,也都能够达到v,这种v称为sink.然后升序输出全部的sink. 对于一个强连通分量来说,全部的点都符合这一条件,可是假 ...
- 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 : tarjan O(n) 存环中的点
/** problem: http://poj.org/problem?id=2553 将所有出度为0环中的点排序输出即可. **/ #include<stdio.h> #include& ...
- 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 TarJan算法题解
本题分两步: 1 使用Tarjan算法求全部最大子强连通图.而且标志出来 2 然后遍历这些节点看是否有出射的边,没有的顶点所在的子强连通图的全部点,都是解集. Tarjan算法就是模板算法了. 这里使 ...
随机推荐
- gcd,lcm,ext_gcd,inv
Least Common Multiple http://acm.hdu.edu.cn/showproblem.php?pid=1019 #include<cstdio> int gcd( ...
- 导入 github 步骤
https://github.com/dotnet/corefx 如果出现未能找到解决方案的情况,则找项目文件打开,如:
- tangent space /handness
normal tangent bitangent 三者互相垂直. 组成一个tangent space 表示一个点 对于原本位置的偏移(扰动) 考虑到这是为了 normalmap做出虚假的normal来 ...
- 解决myeclipse每次启动注册码过期输入注册码
每次都需要重新输入一个新的注册码,搞的很不愉快,后来发现原来是因为在[我的文档]下面有一个myeclipse的配置文件,叫.myeclipse.properties 你可以全盘搜索一下这个文件,然后改 ...
- jquery css 简单笔记
内容 要点:清空表单中所有数据,除去 button,submit,reset,hidden 的数据 $(':input','#myform') .not(':button, :submit, :res ...
- 同一机器 部署 两个 jboss
当jboss和oracle在同一机器上时,通常oracle占用8080端口,这时只需要去修改\deploy\jbossweb-tomcat50.sar\server.xml中.当在同一台机器上运行两个 ...
- linux源码阅读笔记 #define 语句的妙用
#define 语句用于宏定义,在c中,我们可以用其实现函数的功能.如下语句 #define test(a,b) a>b?a:b 很显然,这是一个比较大小的语句.这里a,b相当于函数中的参数. ...
- HDU4612 Warm up 边双连通分量&&桥&&树直径
题目的意思很简单,给你一个已经连通的无向图,我们知道,图上不同的边连通分量之间有一定数量的桥,题目要求的就是要你再在这个图上加一条边,使得图的桥数目减到最少. 首先要做的就是找出桥,以及每个点所各自代 ...
- POJ 1734
#include<iostream> #include<stdio.h> #define MAXN 105 #define inf 123456789 using namesp ...
- hadoop倒排索引
1.前言 学习hadoop的童鞋,倒排索引这个算法还是挺重要的.这是以后展开工作的基础.首先,我们来认识下什么是倒拍索引: 倒排索引简单地就是:根据单词,返回它在哪个文件中出现过,而且频率是多少的结果 ...