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 ...
随机推荐
- Jquery | 基础 | 导航条在项目中的应用
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Android课程设计第三天帧动画区间动画
注意:课程设计只为完成任务,不做细节描述~ 点火是帧动画,发射是区间动画,于是 <?xml version="1.0" encoding="utf-8"? ...
- `<img>`放到`<div>`中底部出现空隙 以及解放方案.
形成情况: 外部的 不设置宽高, 由内部图片撑开 撑开后, 底部会有空隙 问题原因: div元素中的行内元素的默认vertical-align对齐方式是基线baseline img是行内元素, 所以会 ...
- 组件的 state 和 setState
state 我们前面提到过,一个组件的显示形态是可以由它数据状态和配置参数决定的.一个组件可以拥有自己的状态,就像一个点赞按钮,可以有“已点赞”和“未点赞”状态,并且可以在这两种状态之间进行切换.Re ...
- MVC:html动态追加行及取值
先一个button id=addRow 点击事件进行添加 $("#addRow").bind("click", function () { var addH ...
- WPF日常需要使用的操作
窗体如何居中弹出 在窗体上添加属性 WindowStartupLocation="CenterScreen" 窗体如何隐藏掉windows边框 添加属性WindowStyle=& ...
- 小程序java解密
<dependency> <groupId>org.bouncycastle</groupId> <artifactId>bcprov-jdk16< ...
- IO 优化
转自 BlackJack_ #define fastcall __attribute__((optimize("-O3"))) #define IL __inline__ __at ...
- Web前端攻防,一不小心就中招了
随着各浏览器安全功能的提高,前端防御面临的问题也没有之前那么复杂,但浏览器的防御措施并不能百分百的保证网站的安全. 浏览器的XSS Auditor,使得反射型xss几乎被废:CSP(Content-S ...
- 伟景行 citymaker 从入门到精通(2)——工程图层树加载
工程树是指explorer左边这棵树 本例子实现了图层树加载,点击节点切换可视状态 树控件使用easyui的树 html部分 onCheck:treeProjectTreeOnCheck是指树节点的o ...