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算法就是模板算法了. 这里使 ... 
随机推荐
- ios开发之多线程资源争夺
			上一篇介绍了常用的多线程技术,目前开发中比较常用的是GCD,其它的熟悉即可.多线程是为了同步完成多项任务,不是为了提高运行效率,而是为了提高资源使用率来提高系统的整体性能,但是会出现多个线程对同一资源 ... 
- Machine Learning Done Wrong
			Machine Learning Done Wrong Statistical modeling is a lot like engineering. In engineering, there ar ... 
- Unity3D脚本中文系列教程(十六)
			Unity3D脚本中文系列教程(十五) ◆ function OnPostprocessAudio (clip:AudioClip):void 描述:◆ function OnPostprocess ... 
- override equals in Java
			equals() (javadoc) must define an equality relation (it must be reflexive, symmetric, and transitive ... 
- gcc / g++ 编译选项
			g++ -Wall -m64 -W -O2 a.cpp b.cpp -o a 用下面的命令编译,生成libtriangle.so 先生成动态库 g++ -g -fpic -shared -o lib ... 
- asp.net-(含:模拟登陆,照片列表)
			一.画好用户登录界面 同时换下请求的地址. 获取用户信息及判断代码插入位置: 一.画好用户登录界面 同时换下请求的地址. 获取用户信息及判断代码插入位置: <%@ WebHandler Lang ... 
- Project Euler 91:Right triangles with integer coordinates 格点直角三角形
			Right triangles with integer coordinates The points P (x1, y1) and Q (x2, y2) are plotted at integer ... 
- 不要将缓存服务器与Tomcat放在单台机器上,否则出现竞争内存问题
			缓存分为本地缓存和远程分布式缓存,本地缓存访问速度更快但缓存数据量有限,同时存在与应用程序争用内存的情况. 1.不要将缓存服务器与Tomcat放在单台机器上,否则出现竞争内存问题 2.不要将缓存服务器 ... 
- Android:Logcat中找不到本应该输出的Log调试信息
			1.有没有设置Logcat的filter, 2.如果选中了自定义的filter,Tag是否和程序中想查看的那条输出信息的Tag相同: 3.Level等级是否设置的太高. filter设置 点击loca ... 
- java--面向抽象编程
			所谓面向抽象编程是指当设计某种重要的类时,不让该类面向具体的类,而是面向抽象类,及所设计类中的重要数据是抽象类声明的对象,而不是具体类声明的对象.就是利用abstract来设计实现用户需求. 比如:我 ... 
