POJ 2553 The Bottom of a Graph (强连通分量)
题目地址:POJ 2553
题目意思不好理解。题意是:G图中从v可达的全部点w,也都能够达到v,这种v称为sink。然后升序输出全部的sink。
对于一个强连通分量来说,全部的点都符合这一条件,可是假设这个分量还连接其它分量的话,则肯定都不是sink。所以仅仅须要找出度为0的强连通分量就可以。
代码例如以下:
#include <iostream>
#include <string.h>
#include <math.h>
#include <queue>
#include <algorithm>
#include <stdlib.h>
#include <map>
#include <set>
#include <stdio.h>
using namespace std;
#define LL long long
#define pi acos(-1.0)
const int mod=1e9+7;
const int INF=0x3f3f3f3f;
const double eqs=1e-9;
const int MAXN=5000+10;
int head[MAXN], Ecnt, top, indx, scc;
int low[MAXN], dfn[MAXN], belong[MAXN], instack[MAXN], stk[MAXN], out[MAXN];
struct node
{
int u, v, next;
}edge[1000000];
void add(int u, int v)
{
edge[Ecnt].v=v;
edge[Ecnt].next=head[u];
head[u]=Ecnt++;
}
void tarjan(int u)
{
low[u]=dfn[u]=++indx;
instack[u]=1;
stk[++top]=u;
for(int i=head[u];i!=-1;i=edge[i].next){
int v=edge[i].v;
if(!dfn[v]){
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(instack[v]){
low[u]=min(low[u],dfn[v]);
}
}
if(low[u]==dfn[u]){
scc++;
while(1){
int v=stk[top--];
belong[v]=scc;
instack[v]=0;
if(u==v) break;
}
}
}
void init()
{
memset(head,-1,sizeof(head));
memset(dfn,0,sizeof(dfn));
memset(instack,0,sizeof(instack));
memset(out,0,sizeof(out));
Ecnt=top=indx=scc=0;
}
int main()
{
int n, m, i, j, u, v, flag;
while(scanf("%d",&n)!=EOF&&n){
scanf("%d",&m);
init();
while(m--){
scanf("%d%d",&u,&v);
add(u,v);
}
for(i=1;i<=n;i++){
if(!dfn[i]) tarjan(i);
}
for(i=1;i<=n;i++){
for(j=head[i];j!=-1;j=edge[j].next){
v=edge[j].v;
if(belong[i]!=belong[v]){
out[belong[i]]++;
}
}
}
flag=0;
for(i=1;i<=n;i++){
if(out[belong[i]]==0){
if(!flag){
printf("%d",i);
flag=1;
}
else printf(" %d",i);
}
}
puts("");
}
return 0;
}
POJ 2553 The Bottom of a Graph (强连通分量)的更多相关文章
- 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 - 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(强连通分量)
POJ 2553 The Bottom of a Graph 题目链接 题意:给定一个有向图,求出度为0的强连通分量 思路:缩点搞就可以 代码: #include <cstdio> #in ...
- 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[Tarjan强连通分量]
题意: 求出度为0的强连通分量. 思路: 缩点 具体有两种实现: 1.遍历所有边, 边的两端点不在同一强连通分量的话, 将出发点所在强连通分量出度+1. #include <cstdio> ...
- POJ 2553 The Bottom of a Graph(强连通分量的出度)
题意: 求出图中所有汇点 定义:点v是汇点须满足 --- 对图中任意点u,若v可以到达u则必有u到v的路径:若v不可以到达u,则u到v的路径可有可无. 模板:http://www.cnblogs.co ...
- 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 Tarjan找环缩点(题解解释输入)
Description We will use the following (standard) definitions from graph theory. Let V be a nonempty ...
- poj 2553 The Bottom of a Graph
求解的是有向图中满足“自己可达的顶点都能到达自己”的顶点个数如果强连通分量中某个顶点,还能到达分量外的顶点,则该连通分量不满足要求// 因此,本题要求的是将强连通分量缩点后所构造的新图中出度为0的顶点 ...
随机推荐
- Android异步载入全解析之IntentService
Android异步载入全解析之IntentService 搞什么IntentService 前面我们说了那么多,异步处理都使用钦定的AsyncTask.再不济也使用的Thread,那么这个Intent ...
- NSIS:卸载加密码示例
原文 NSIS:卸载加密码示例 最近有几个同学问我关于卸载时加密码的问题,其实很简单,懂点基础就可以根据安装加密码那篇文章http://www.flighty.cn/html/bushu/201009 ...
- 用android LinearLayout和RelativeLayout实现精确布局(转)
先明确几个概念的区别: padding margin都是边距的含义,关键问题得明白是什么相对什么的边距. padding是控件的内容相对控件的边缘的边距. margin是控件边缘相对父控件的边距. a ...
- 通达OA web页面与精灵显示内容更新后不一致的问题
前一段就发现有这种问题.就是在开发的电话查询里更新的信息,可是在精灵对话窗体上显示的还是原来的信息.这样导致从新开发入口更新信息就不能使用.一開始还以为是厂家升级,变更了存储的表结构.感觉下载近期的升 ...
- cocos2d-x教程2:在windows下怎样批量转换pvr,ccz为png或jpg
这是一个非经常见的功能,可是找了全网,竟然找不到,于是借鉴别人的批处理文件,改了下,就能够把整个文件夹的所有一次批量转换. 将这个bat文件暂定为,myConvert.bat,运行时就把这个bat文件 ...
- 全网最全ASP.NET MVC 教程汇总
全网最全ASP.NET MVC 教程汇总 MVC架构已深得人心,微软也不甘落后,推出了Asp.net MVC.小编特意整理博客园乃至整个网络最具价值的MVC技术原创文章,为想要学习ASP.NET MV ...
- Hbase在的应用经验的统计
1. 需求统计 互联网上对于数据的统计,一个重要的应用就是对站点站点数据的统计,比如CNZZ站长统计.百度统计.Google Analytics.量子恒道统计等等. 站点站点统计工具无外乎有下面一些功 ...
- DevExpress XtraReports 入门五 创建交叉表报表
原文:DevExpress XtraReports 入门五 创建交叉表报表 本文只是为了帮助初次接触或是需要DevExpress XtraReports报表的人群使用的,为了帮助更多的人不会像我这样浪 ...
- Codeforces 10C Digital Root 法冠军
主题链接:点击打开链接 #include<stdio.h> #include<iostream> #include<string.h> #include<se ...
- Instruments-Automation: 通过命令行执行测试用例
为了实现该脚本的自己主动定时执行.我们需要开始在命令行和脚本,详细代码如下所示的: instruments -t /Applications/Xcode.app/Contents/Applicatio ...