【POJ - 2553】The Bottom of a Graph(tarjan强连通分量缩点,模板题)

题干:

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 vv 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.

Input

The input contains several test cases, each of which corresponds to a directed graphG. Each test case starts with an integer number v, denoting the number of vertices of G=(V,E), where the vertices will be identified by the integer numbers in the setV={1,...,v}. You may assume that 1<=v<=5000. That is followed by a non-negative integer e and, thereafter, e pairs of vertex identifiers v1,w1,...,ve,we with the meaning that (vi,wi)∈E. There are no edges other than specified by these pairs. The last test case is followed by a zero.

Output

For each test case output the bottom of the specified graph on a single line. To this end, print the numbers of all nodes that are sinks in sorted order separated by a single space character. If the bottom is empty, print an empty line.

Sample Input

3 3
1 3 2 3 3 1
2 1
1 2
0

Sample Output

1 3
2

题目大意:

定义点v是汇点须满足 --- 对图中任意点u,若v可以到达u则必有u到v的路径;若v不可以到达u,则u到v的路径可有可无。问在n个点m条边的有向图里面,问有多少个点是汇点。

解题报告:

缩点后看出度为0的点的个数就行了。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define F first
#define S second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e5 + 5;
vector<int> vv[MAX],ans;
int n,m;
int dfn[MAX],low[MAX],vis[MAX],out[MAX],cnt[MAX],col[MAX];
int stk[MAX],index,clk,scc;
void init() {for(int i = 1; i<=n; i++) {vv[i].clear();dfn[i]=low[i]=vis[i]=out[i]=cnt[i]=0;}clk = index = scc = 0;ans.clear();
}
void tarjan(int x) {stk[++index] = x;vis[x] = 1;dfn[x] = low[x] = ++clk;int up = vv[x].size();for(int i = 0; i<up; i++) {int v = vv[x][i];if(dfn[v] == 0) {tarjan(v);low[x] = min(low[x],low[v]);}else if(vis[v] == 1) low[x] = min(low[x],dfn[v]);}if(dfn[x] == low[x]) {scc++;while(1) {int tmp = stk[index];index--;col[tmp] = scc;cnt[scc]++;vis[tmp]=0;if(tmp == x) break;}}
}
int main()
{while(~scanf("%d",&n)) {if(n == 0) break;scanf("%d",&m);init();for(int u,v,i = 1; i<=m; i++) {scanf("%d%d",&u,&v);vv[u].pb(v);}for(int i = 1; i<=n; i++) {if(dfn[i] == 0) tarjan(i);}for(int up,u = 1; u<=n; u++) {up = vv[u].size();for(int v,j = 0; j<up; j++) {v = vv[u][j];if(col[u] == col[v]) continue;out[col[u]]++;}}for(int i = 1; i<=n; i++) {if(out[col[i]] == 0) ans.pb(i);}sort(ans.begin(),ans.end());int up = ans.size();for(int i = 0; i<up; i++) printf("%d%c",ans[i],i == up-1 ? '\n' : ' ');}return 0 ;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440152.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

机器学习笔记(1):Introduction

目录 1&#xff09;welcome 2&#xff09;What is Machine Learning 3&#xff09;Supervised Learning 4&#xff09;Unsupervised Learning 1&#xff09;welcome 第一个视频主要介绍了机器学习目前的案例&#xff0c;主要有&#xff1a;数据库挖掘、医疗记录、生物工程…

【POJ - 3352】Road Construction(Tarjan,边双连通分量)

题干&#xff1a; Its almost summer time, and that means that its almost summer construction time! This year, the good people who are in charge of the roads on the tropical island paradise of Remote Island would like to repair and upgrade the various roads…

机器学习简易入门-附推荐学习资料

目录 &#xff08;1&#xff09;机器学习正规学习路线 &#xff08;2&#xff09;机器学习快速入门 &#xff08;3&#xff09;总结 感谢黄海广博士的分享 原创&#xff1a; 机器学习初学者 机器学习初学者 今天 机器学习如何入门&#xff1f;目前没有明确的答案。本站面向…

C++11中的std::function

原文地址&#xff1a;http://www.jellythink.com/archives/771 看看这段代码 先来看看下面这两行代码&#xff1a; std::function<void(EventKeyboard::KeyCode, Event*)> onKeyPressed; std::function<void(EventKeyboard::KeyCode, Event*)> onKeyReleased; 这两…

【HDU - 3394】Railway(点双连通分量,Tarjan算法,思维tricks)

题干&#xff1a; There are some locations in a park, and some of them are connected by roads. The park manger needs to build some railways along the roads, and he would like to arrange tourist routes to each circuit. If a railway belongs to more than one …

飞机大战(简易版)

一、游戏分析 飞机大战中的主要“角色”有&#xff1a; 1.英雄 2.敌方飞机 3.英雄发射的子弹 我们需要控制的有&#xff1a; 1.绘制屏幕内的角色 2.控制角色的逻辑&#xff0c;比如&#xff1a;敌方飞机与我方飞机的碰撞检测&#xff0c;我方飞机发射的子弹与敌方飞机之间的碰撞…

在Ubuntu上安装Keras深度学习框架

目录 1&#xff09;安装pip 2&#xff09;安装Python科学套件 3&#xff09;安装TensorFlow 4&#xff09;安装keras 5&#xff09;安装Jupyter Notebook 6&#xff09;运行Keras 本文介绍如何在Ubuntu上安装Keras深度学习框架。 1&#xff09;安装pip 安装pip包&#…

【POJ - 1523】SPF(Tarjan求割点,求分割成的连通块数,模板题,tricks)

题干&#xff1a; Consider the two networks shown below. Assuming that data moves around these networks only between directly connected nodes on a peer-to-peer basis, a failure of a single node, 3, in the network on the left would prevent some of the still…

机器学习笔记(2):单变量线性回归

目录 1&#xff09;Model representation 2&#xff09;Cost function 3&#xff09;Cost function intuition 1 4&#xff09;Cost function intuition2 5&#xff09;Gradient descent 6&#xff09;Gradient descent intuition 7&#xff09;Gradient descent for li…

安装VMware tools

点击“虚拟机” 安装VMware tools提取图中文件到“下载” 提取登入root 进入 cd 下载/vmware-tools-distrib 执行 ./vmware-install-pl 输入yes或者点击“enter”出现图中&#xff0c;即为成功安装

Keras入门实战(1):MNIST手写数字分类

目录 1)首先我们加载Keras中的数据集 2&#xff09;网络架构 3&#xff09;选择编译(compile参数) 4&#xff09;准备图像数据 5) 训练模型 6&#xff09;测试数据 前面的博客中已经介绍了如何在Ubuntu下安装Keras深度学习框架。 现在我们使用 Keras 库来学习手写数字分…

【BZOJ - 2574】[Poi1999] Store-Keeper(点双连通分量,求割点,记忆化bfs)

题干&#xff1a; 有一个仓库被分成n*m 个矩形区域&#xff0c;如果两个区域有一条公共边&#xff0c;则被认为这两个区域相邻。包裹都放在一个区域中&#xff0c;剩余的区域或者空闲或者被集装箱占有&#xff0c;这是因为集装箱太重&#xff0c;仓库管理员不能将集装箱搬走。…

机器学习笔记(3):线性代数回顾

目录 1&#xff09;Matrices and vectors 2&#xff09;Addition and scalar multiplication 3&#xff09;Matrix-vector multiplication 4&#xff09;Matrix-matrix multiplication 5&#xff09;Matrix multiplication properties 6&#xff09;Inverse and transpos…

hadoop 安装

Hadoop单机和伪分布式安装 更新apt 用root用户登录 先更新一下 apt apt-get update然后安装vim apt-get install vim安装VMware tools tools 安装 安装SSH、配置SSH无密码登陆 单节点模式都需要用到 SSH 登陆&#xff0c;Ubuntu 默认已安装了 SSH client&#xff0c;此…

机器学习笔记(4):多变量线性回归

目录 1&#xff09;Multiple Features 2&#xff09;Gradient descent for multiple variables 3&#xff09;Gradient descent in practice 1: Feature Scaling 4&#xff09;Gradient descent in pratice2: Learning rate 5&#xff09;Features and polynomial regress…

zookeeper单节点部署

hadoop 安装 在/install-package目录下查看zookeeper的安装包 本文中安装的是zookeeper-3.4.12.tar.gz 下方为百度云链接 链接&#xff1a;https://pan.baidu.com/s/1bzq4ILH41owtS__3tBCcRQ 提取码&#xff1a;6q4r 把下载好的zookeeper-3.4.12.tar.gz 放到/install-packa…

机器学习笔记(五):逻辑回归

目录 1&#xff09;Classification 2&#xff09;Hypothesis Representation 3&#xff09;Decision boundary 4&#xff09;Cost function 5&#xff09;Simplified cost function and gradient descent 6&#xff09;Multi-class classification:One-vs-all 7&#xf…

xrdp完美实现Windows远程访问Ubuntu 16.04

前言&#xff1a; 在很多场景下&#xff0c;我们需要远程连接到Linux服务器(本文是Ubuntu)&#xff0c;传统的连接主要分为两种。 第一种&#xff1a;通过SSH服务&#xff08;使用xshell等工具&#xff09;来远程访问&#xff0c;编写终端命令&#xff0c;不过这个是无界面的&a…

机器学习笔记(六):正则化

目录 1&#xff09;The problem of overfitting 2&#xff09;Cost function 3&#xff09;Regularized linear regression 4&#xff09;Regularized logistic regression 我们已经学习了线性回归和逻辑回归算法&#xff0c;已经可以有效解决很多问题&#xff0c;但是在实…

Hbase单节点安装

zookeeper单节点部署 实验环境 操作系统&#xff1a;Ubuntu 16.04 Hadoop&#xff1a;Hadoop 2.7.5 Zookeeper&#xff1a;zookeeper 3.4.12 Java&#xff1a;java version 1.8.0 到/install-package目录下查看hbase安装包 #>ls /install-package本文中用的是hbase-1…