POJ 2553 The Bottom of a Graph(强连通分量的出度)
题意:
求出图中所有汇点
定义:点v是汇点须满足 --- 对图中任意点u,若v可以到达u则必有u到v的路径;若v不可以到达u,则u到v的路径可有可无。
模板:http://www.cnblogs.com/Jadon97/p/8328750.html

分析:
很显然, 图中强连通分量中所有的点属性都是一样的, 要么都是汇点, 要么都不是。
如果有一个强连通分量A的边连向强连通分量B, 那么A一定不是汇点, 因为B不会有边连向A(如果有的话A、B就是同一个强连通分量了)。
求出所有强连通分量, 然后再求一下出度即可
#include <stack>
#include <cstdio>
#include <vector>
#include <iostream>
#include <cstring>
using namespace std;
const int maxn = ;
vector<int> G[maxn];
int n , m;
int dfn[maxn], low[maxn], color[maxn], out_degree[maxn];
int dfs_num = , col_num = ;
bool vis[maxn];//标记元素是否在栈中
stack<int> s;
void Tarjan(int u)
{
dfn[ u ] = dfs_num;
low[ u ] = dfs_num++;
vis[u] = true; //标记访问
s.push(u); // 入栈
for(int i = ; i < G[u].size(); i++)
{
int v = G[u][i];
if( ! dfn[v])
{
Tarjan( v );
low[u] = min(low[v], low[u]);
}
else if(vis[v]) //如果在v栈中 , 更新low[u]
{
low[u] = min(low[u], dfn[v]);
}
}
if(dfn[u] == low[u])
{
vis[u] = false;
color[u] = col_num;
int t;
for(;;){
int t = s.top(); s.pop();
color[t] = col_num;
vis[t] = false;
if(t == u) break;
}
col_num++;
}
}
int main()
{
while(~scanf("%d %d", &n,&m))
{
if(n == ) break;
for(int i = ; i < maxn; i++) G[i].clear();
memset(dfn, , sizeof(dfn));
memset(vis, , sizeof(vis));
memset(low, , sizeof(low));
memset(color, , sizeof(color));
memset( out_degree, ,sizeof(out_degree));
dfs_num = , col_num = ;
for(int i = ; i < m; i++)
{
int u , v;
scanf("%d %d", &u, &v);
G[u].push_back(v);
} for(int i = ; i <= n; i++){
if(!dfn[i])
Tarjan(i);
} for(int u = ; u <= n; u++){ //
for(int i = ; i < G[u].size(); i++){//枚举每一条边
int v = G[u][i];
if(color[u] != color[v]){ //如果有一条u到v的边, 但u,v不是同一个强连通分量, 说明u所在的强连通分量有一条出边指向v, u中都不是题目所求
out_degree[color[u]]++;
}
}
} int cnt = , ans[maxn];
for(int u = ; u <= n; u++){
if(out_degree[color[u]] == ) ans[cnt++] = u;
}
printf("%d",ans[]);
for(int i = ;i < cnt; i++) printf(" %d", ans[i]); puts("");
}
return ;
}
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 2553 The Bottom of a Graph (强连通分量)
题目地址:POJ 2553 题目意思不好理解.题意是:G图中从v可达的全部点w,也都能够达到v,这种v称为sink.然后升序输出全部的sink. 对于一个强连通分量来说,全部的点都符合这一条件,可是假 ...
- 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-2552-The Bottom of a Graph 强连通分量
链接: https://vjudge.net/problem/POJ-2553 题意: We will use the following (standard) definitions from gr ...
- 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 (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 ...
随机推荐
- C++面向对象程序设计举例
[例8.1]最简单的例子. #include <iostream> using namespace std; class Time //定义Time类 { public : //数据成员为 ...
- Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) C
Description In the army, it isn't easy to form a group of soldiers that will be effective on the bat ...
- Linux下cpu过高问题排查
原文地址:https://blog.csdn.net/chenjunan888/article/details/80447800 在服务器报cpu过高时,可使用以下命令,快速导出堆栈信息,以方便查看具 ...
- Android屏幕适配-安卓切图
一.Android中的单位 1.dp(dip):density-independent pixels,这并不是一个绝对的单位,而只是一个相对的概念,代表的是屏幕写对角线上每inch上像素点的个数. 2 ...
- python_函数嵌套(4)
第1章 名称空间 1.1 定义 1.2 变量运行流程 1.3 临时名称空间 1.4 python三种名称空间 第2章 作用域 2.1 作用域分类 2.2 加载顺序 2.3 取值顺序 函数嵌套 2.4 ...
- CentOS6.5下安装Redis2.8.6和phpredis2.2.4扩展
一.版本说明 CentOS版本 [plain]view plaincopyprint? [root@localhost ~]# uname Linux [root@localhost ~]# unam ...
- return false 的其中一种用法
return false 的具体是做什么的在这里就不多说了,因为我觉得我形容不明白.....避免大家弄混乱,就不给大家添麻烦了~~ 直接上例子: 1.先看看下面一段代码,指出其中的错误所在: //点击 ...
- 洛谷P2774 方格取数问题(最小割)
题意 $n \times m$的矩阵,不能取相邻的元素,问最大能取多少 Sol 首先补集转化一下:最大权值 = sum - 使图不连通的最小权值 进行黑白染色 从S向黑点连权值为点权的边 从白点向T连 ...
- 51nod 1432 独木舟
基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 n个人,已知每个人体重.独木舟承重固定,每只独木舟最多坐两个人,可以坐一个人或者两个人.显然要求总重量不超过独木舟承 ...
- codevs 2600 13号星期几?
时间限制: 1 s 空间限制: 8000 KB 题目等级 : 黄金 Gold 题目描述 Description 从1900年1月1日(星期一) 开始经过的n年当中,每个月的13号这一天是星期一.星 ...