POJ-2552-The Bottom of a Graph 强连通分量
链接:
https://vjudge.net/problem/POJ-2553
题意:
We will use the following (standard) definitions from graph theory. Let V be a nonempty and finite set, its elements being called vertices (or nodes). Let E be a subset of the Cartesian product V×V, its elements being called edges. Then G=(V,E) is called a directed graph.
Let n be a positive integer, and let p=(e1,...,en) be a sequence of length n of edges ei∈E such that ei=(vi,vi+1) for a sequence of vertices (v1,...,vn+1). Then p is called a path from vertex v1 to vertex vn+1 in G and we say that vn+1 is reachable from v1, writing (v1→vn+1).
Here are some new definitions. A node v in a graph G=(V,E) is called a sink, if for every node w in G that is reachable from v, v is also reachable from w. The bottom of a graph is the subset of all nodes that are sinks, i.e., bottom(G)={v∈V|∀w∈V:(v→w)⇒(w→v)}. You have to calculate the bottom of certain graphs.
求哪些点能到的点都可以到自己
思路:
一个强连通分量内的点互相可达,所有一个强连通分量没有出度,则这个强连通内的点都满足条件。
代码:
#include <iostream>
#include <cstdio>
#include <vector>
#include <memory.h>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
#include <stack>
using namespace std;
const int MAXN = 5e3+10;
vector<int> G[MAXN];
stack<int> St;
int Dfn[MAXN], Low[MAXN];
int Dis[MAXN], Vis[MAXN];
int Fa[MAXN];
int times, cnt;
int n, m;
void Tarjan(int x)
{
Dfn[x] = Low[x] = ++times;
St.push(x);
Vis[x] = 1;
for (int i = 0;i < G[x].size();i++)
{
int nextnode = G[x][i];
if (Dfn[nextnode] == 0)
{
Tarjan(nextnode);
Low[x] = min(Low[x], Low[nextnode]);
}
else if (Vis[nextnode])
Low[x] = min(Low[x], Dfn[nextnode]);
}
if (Low[x] == Dfn[x])
{
cnt++;
while (St.top() != x)
{
Fa[St.top()] = cnt;
Vis[St.top()] = 0;
St.pop();
}
Fa[St.top()] = cnt;
Vis[St.top()] = 0;
St.pop();
}
}
void Init()
{
memset(Dis, 0, sizeof(Dis));
memset(Vis, 0, sizeof(Vis));
memset(Dfn, 0, sizeof(Dfn));
for (int i = 1;i <= n;i++)
G[i].clear(), Fa[i] = i;
times = cnt = 0;
while (!St.empty())
St.pop();
}
int main()
{
while (~scanf("%d", &n) && n)
{
Init();
scanf("%d", &m);
int l, r;
for (int i = 1;i <= m;i++)
{
scanf("%d%d", &l, &r);
G[l].push_back(r);
}
for (int i = 1;i <= n;i++)
if (Dfn[i] == 0)
Tarjan(i);
for (int i = 1;i <= n;i++)
{
for (int j = 0;j < G[i].size();j++)
{
int node = G[i][j];
if (Fa[i] != Fa[node])
Dis[Fa[i]]++;
}
}
int flag = 0;
for(int i=1;i<=n;i++)
{
if (Dis[Fa[i]] == 0)
{
if (!flag)
{
printf("%d", i);
flag = 1;
}
else printf(" %d", i);
}
}
printf("\n");
}
return 0;
}
POJ-2552-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. 对于一个强连通分量来说,全部的点都符合这一条件,可是假 ...
- 【poj2553】The Bottom of a Graph(强连通分量缩点)
题目链接:http://poj.org/problem?id=2553 [题意] 给n个点m条边构成一幅图,求出所有的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 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: ...
随机推荐
- sql server 查询存储过程返回值
SET QUOTED_IDENTIFIER ON SET ANSI_NULLS ON GO CREATE proc [dbo].[is_yy] ) out, ), ) as begin ' begin ...
- python指定pip安装源
python的pip默认的安装源的位置是国外的,导致有时候下载很慢或者直接失败我们可以切换国内的源 目前国内可用的我知道的有两个 豆瓣的:http://pypi.doubanio.com/simple ...
- pip install dal 失败问题
这个问题是我在看一本<Django企业开发实战>运行其中一个项目时遇到的 作为一个自学的python 这种问题挺头疼的 这不是代码逻辑的问题 没法像Debug 一样去找问题 我们能依据的 ...
- 【LeetCode】123、买卖股票的最佳时机 III
Best Time to Buy and Sell Stock III 题目等级:Hard 题目描述: Say you have an array for which the ith element ...
- 【神经网络与深度学习】Caffe源码中各种依赖库的作用及简单使用
1. Boost库:它是一个可移植.跨平台,提供源代码的C++库,作为标准库的后备. 在Caffe中用到的Boost头文件包括: (1).shared_ptr.hpp:智能指针,使用它可以不 ...
- Markdown基础语法总结
目录 区块元素 标题 列表 区块引用 代码区块 分隔线 段落和换行 区段元素 链接 强调 代码 图片 转义 标题 <a name="title"></a> ...
- (转).net中的session与cookies区别及使用方法
cookie数据存放在客户的浏览器上,session数据放在服务器上,cookie不是很安全,别人可以分析存放在本地的COOKIE并进行COOKIE欺骗,考虑到安全应当使用session 先介绍一 ...
- mavn Nexus Repository Manager漏洞
https://www.secpulse.com/archives/112290.html
- [转帖] 国产x86-海光禅定 2018年营收过亿?
中科曙光:全年业绩稳健,海光芯片营收过亿 X86服务器市场Intel占据绝对优势:X86处理器已经成为全球最广泛使用的处理器架构之一,尤其是在PC和服务器领域,其中在处理器市场的份额高达90%以上.中 ...
- redis基本操作和 过期时间设置以及持久化方案
Redis是NOSQL阵营中的一种数据库,主要用于存储缓存 五大数据类型:字符串(String).散列(hash).列表(list).集合(set).有序集合(SortedSett .zset) St ...