题干:

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 ;
}

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

  1. POJ 2553 The Bottom of a Graph

    POJ_2553 这个题目本来是比较容易下手的,最后只需要求所有出度为0的强连通分量所包含的点,然后按字典序输出即可.但是由于一开始用邻接表写代码的时候由于读入边和存储边的写法问题导致我一直TLE,后 ...

  2. poj 2553 The Bottom of a Graph 未完

    题目大意:如果v点能够到的点,反过来能够到达v点,则称这个点为sink点,输出所有的sink点 RE #include <iostream>//https://blog.csdn.net/ ...

  3. Network of Schools POJ - 1236 tarjan强连通分量缩点

    A number of schools are connected to a computer network. Agreements have been developed among those ...

  4. 2021.8.9【提高B组模拟1】T2 QYQ在艾泽拉斯(Tarjan强连通分量)(并查集)

    QYQ在艾泽拉斯 题目大意 输入样例 3 2 1 2 3 1 1 2 1 0 输出样例 4 样例说明: QYQ从3号点开始,走到2号点,最后走到1号点,结束旅程,共获得1+2+1=4价值的宝物 题目数 ...

  5. The King’s Problem(tarjan求强连通分量缩点+匈牙利求有向无环图的最小路径覆盖)

    Link:http://acm.hdu.edu.cn/showproblem.php?pid=3861 The King's Problem Time Limit: 2000/1000 MS (Jav ...

  6. 【tarjan强连通分量】洛谷P1726 上白泽慧音

    [tarjan强连通分量]洛谷P1726 上白泽慧音 题目传送门 妥妥的强连通模板啊(详细解释戳这里) #include <bits/stdc++.h> #define MAXN 5005 ...

  7. Tarjan算法超超超详解(ACM/OI)(强连通分量/缩点)(图论)(C++)

    本文将持续更新. I 前置芝士:深度优先搜索与边的分类 首先我们来写一段基本的DFS算法(采用链式前向星存图): bool vis[MAXN];void dfs(int u) {vis[u] = tr ...

  8. poj 1904 tarjan强连通分量(给国王的2000个儿子找老婆 )

    题意:有n个王子,每个王子i有ki个他喜欢的妹子,每个王子只能和喜欢的妹子结婚,大臣给出一个匹配表(假设肯定是对的),每个王子都和一个妹子结婚,但是国王不满意,他要求大臣给他另一个表,每个王子可以和几 ...

  9. POJ 2186 Popular Cows(强连通分量缩点,Tarjan算法)

    [题目链接] http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=16578 [解题报告] 给你一个有向图,问你有多少个点可以被其它 ...

最新文章

  1. C语言const专题
  2. dct变换的主要优点有哪些_发泡保温材料包括哪些成分?主要优点是什么?有没有发展前景?...
  3. 【ActiveMQ】消息生产者自动注入报错:Could not autowire. No beans of 'JmsMessagingTemplate' type found
  4. [Mac OSX技巧] 通过命令行开启安全性与隐私中的任何来源
  5. MyBatis架构图
  6. python3.7中文手册chm_python3.7 官方中文手册文档全套
  7. webstorm主题网址
  8. 计算机所建造全过程,Midas 桥梁设计建模计算,全过程图文解析!
  9. 小米盒子 计算机共享,小米盒子如何通过局域网共享安装软件
  10. iOS股票K线图、分时图绘制
  11. 【招聘】上海微创医疗机器人集团 - 软件工程师/图像算法工程师
  12. 面对电车难题,自动驾驶会怎么选?
  13. 前有阿里巴巴,后有拼多多,网易考拉要去哪儿?
  14. python从word中提取信息导入excel_使用python模块win32com提取word表格到excel
  15. [Leetcode] 741. Cherry Pickup 解题报告
  16. html字体颜色渐变
  17. [培训-DSP快速入门-3]:C54x DSP内存资源与内存空间分布
  18. 数据结构(C语言)第二版 第一章课后答案
  19. 阿里巴巴微服务架构的四大金刚利器
  20. 一度智信:拼多多商品历史最低价影响着什么

热门文章

  1. [密码学][困难问题][常见规约]密码学问题常见困难问题
  2. [Leetcode][第546题][JAVA][移除盒子][递归][动态规划]
  3. HDU-1251 统计难题 map写法
  4. Linux网络设备描述符,Linux
  5. java内存溢出让tomcat停止_java - 使用JVM Open J9一段时间后,应用程序(tomcat)停止响应 - 堆栈内存溢出...
  6. python stringstrip方法详解_Python 基础知识全篇-字符串(Strings)
  7. 站怎么点都是一样_老鼠被卡在轮胎里,像是被点了穴道一样:这可怎么办才好?...
  8. sql查询php,SQL查询或PHP?
  9. uilabel 自行撑开高度_IOS UILabel自適應里面的文字,自動調整寬度和高度的
  10. python3.6sysos_求大佬,这是什么情况啊