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: ...
随机推荐
- Pytorch笔记 (3) 科学计算2
一.组织张量的元素 (1)重排张量元素 本节介绍在不改变 张量元素个数 和 各元素的值的情况下改变张量的大小 torch.Tensor类的成员方法 reshape() 参数是多个int类型的值. 如果 ...
- Akka系列(二):Akka中的Actor系统
前言......... Actor模型作为Akka中最核心的概念,所以Actor在Akka中的组织结构是至关重要,本文主要介绍Akka中Actor系统. 1.Actor系统 Actor作为一种封装状态 ...
- 【转贴】Linux查看物理CPU个数、核数、逻辑CPU个数
https://www.cnblogs.com/sparkbj/p/7161675.html 记不住 sort uniq wc grep 等命令集合 # 总核数 = 物理CPU个数 X 每颗物理C ...
- [转帖]Linux下批量替换文件内容方法
Linux下批量替换文件内容方法 https://www.cnblogs.com/fjping0606/p/4428850.html 刚才用到的命令 原作者写的挺好的记录一下 以后 用. 1:查找fi ...
- 【转】mysql卸载(windows)
作者:cxy_Summer 来源:CSDN 原文:https://blog.csdn.net/cxy_Summer/article/details/70142322 版权声明:本文为博主原创文章,转载 ...
- 小记---------linux远程连接集群内其他机器mysql库
mysql -h -u maxwell -p#10.0.15.145 远程机器ip#-P 注意是大写P 端口#-u 用户#-p 密码
- 洛谷 U78696 图书馆馆长的考验 题解
题面 1. 图书馆馆长的考验(library) 红魔馆的拥有者蕾米莉亚的好友帕秋莉是红魔馆的大图书馆的馆长.擅长操纵五行,名言是“万物都有属性.所谓的属性,和弱点是一样的”. 一天,因为魔理沙看了神之 ...
- [BZOJ 4820] [SDOI2017] 硬币游戏(高斯消元+概率论+字符串hash)
[BZOJ 4820] [SDOI2017] 硬币游戏(高斯消元+概率论+字符串hash) 题面 扔很多次硬币后,用H表示正面朝上,用T表示反面朝上,会得到一个硬币序列.比如HTT表示第一次正面朝上, ...
- CSA Lignts Out
csa 算是热身题吧 如果是每次操作一行或一列,那么无论怎么操作,本质不同的行最多只有两种,本质不同的列也最多只有两种,那么只要把某一种行和某一种列全部翻转使得全为0即可 现在是同时操作一行一列,显然 ...
- G1 垃圾收集器之对象分配过程
G1的年轻代由eden region 和 survivor region 两部分组成,新建的对象(除了巨型对象)大部分都在eden region中分配内存,如果分配失败,说明eden region已经 ...