Tarjin + 缩点
链接:https://www.nowcoder.com/acm/contest/81/C
来源:牛客网
题目描述
输出一个尽可能小的点集,使得从这些点出发能够到达任意一点,如果有多个这样的集合,输出这些集合升序排序后字典序最小的。
输入描述:
第一行为两个整数 1 ≤ n, m ≤ 10
5
,
接下来 M 行,每行两个整数 1 ≤ u, v ≤ 10
5
表示从点 u 至点 v 有一条有向边。
数据保证没有重边、自环。
输出描述:
第一行输出一个整数 z,表示作为答案的点集的大小;
第二行输出 z 个整数,升序排序,表示作为答案的点集。
输入
7 10
4 5
5 1
2 5
6 5
7 2
4 2
1 2
5 3
3 5
3 6
输出
2
4 7 题意 : 寻找最小的点集,使得从这些点出发的可以遍历整张图
思路分析 : Trajin + 缩点
代码示例 :
const int maxn = 1e5+10; vector<int>ve[maxn];
vector<int>id[maxn]; // 强连通的编号
int n, m;
int dfn[maxn], low[maxn]; // 每个点在这棵树中,最小的子树的根
int tot = 0, key = 0;
int Stack[maxn], belong[maxn]; // 缩点
bool instack[maxn];
int scc; // 强连通分量的个数 void tarjin(int x){
low[x] = dfn[x] = ++key; // 注意是 ++在前,因为下面下面深搜的判断是为0表示没访问过的点,才去搜
Stack[tot++] = x;
instack[x] = true; for(int i = 0; i < ve[x].size(); i++){
int to = ve[x][i]; if (!dfn[to]) {
tarjin(to);
low[x] = min(low[x], low[to]);
}
else if (instack[to]){
low[x] = min(low[x], dfn[to]);
}
}
if (low[x] == dfn[x]){
scc++;
int v;
do{
v = Stack[--tot];
instack[v] = false;
belong[v] = scc;
id[scc].push_back(v);
}
while(v != x);
}
}
int u[maxn], v[maxn], in[maxn];
vector<int>ans; void solve(){ } int main() {
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
cin >> n >> m;
int a, b; memset(dfn, 0, sizeof(dfn));
memset(low, 0, sizeof(low));
memset(instack, false, sizeof(instack));
memset(in, 0, sizeof(in));
for(int i = 1; i <= m; i++){
scanf("%d%d", &u[i], &v[i]);
ve[u[i]].push_back(v[i]);
}
for(int i = 1; i <= n; i++){
if (!dfn[i]) tarjin(i);
} //for(int i = 1; i <= n; i++) printf("%d ", belong[i]);
for(int i = 1; i <= m; i++){
if (belong[u[i]] == belong[v[i]]) continue;
in[belong[v[i]]]++;
}
for(int i = 1; i <= scc; i++) sort(id[i].begin(), id[i].end());
for(int i = 1; i <= scc; i++){
if (!in[i]) {
ans.push_back(id[i][0]);
}
}
sort(ans.begin(), ans.end());
printf("%d\n", ans.size());
for(int i = 0; i < ans.size(); i++){
printf("%d%c", ans[i], i==ans.size()-1?'\n':' ');
}
return 0;
}
Tarjin + 缩点的更多相关文章
- Warm up-HUD4612(树的直径+Tarjin缩点)
http://acm.hdu.edu.cn/showproblem.php?pid=4612 题目大意:求加一条边最小的桥数 先用Tarjin缩点求出一棵树,然后用bfs求出树的直径,树的直径就是加一 ...
- poj 2186 强连通入门题目
每头牛的梦想就是成为牛群中最受欢迎的牛. 在一群N(1 <= N <= 10,000)母牛中, 你可以得到M(1 <= M <= 50,000)有序的形式对(A,B),告诉你母 ...
- 洛谷 P3420 [POI2005]SKA-Piggy Banks
P3420 [POI2005]SKA-Piggy Banks 题目描述 Byteazar the Dragon has NN piggy banks. Each piggy bank can eith ...
- 国庆 day 7 下午
思路:见博客. #include<iostream> #include<cstdio> #include<cstring> #include<algorith ...
- hdu 4674 Trip Advisor(缩点+倍增lca)
花了一天半的时间,才把这道题ac= = 确实是道好题,好久没敲这么长的code了,尤其是最后的判定,各种销魂啊~ 题目中给出的条件最值得关注的就是:每个点最多只能在一个环内->原图是由一个个边连 ...
- HDU 5934 Bomb 【图论缩点】(2016年中国大学生程序设计竞赛(杭州))
Bomb Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- POJ 2186 Popular Cows(Targin缩点)
传送门 Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 31808 Accepted: 1292 ...
- poj2186--tarjan+缩点
题目大意: 每一头牛的愿望就是变成一头最受欢迎的牛.现在有N头牛,给你M对整数(A,B),表示牛A认为牛B受欢迎. 这 种关系是具有传递性的,如果A认为B受欢迎,B认为C受欢迎,那么牛A也 ...
- POJ3160 Father Christmas flymouse[强连通分量 缩点 DP]
Father Christmas flymouse Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 3241 Accep ...
随机推荐
- H3C 收敛速度慢
- python基础五之字典
python数据的可变性 通过数据的可变性,可将数据分为可变数据类型和不可变数据类型. 可变数据类型:list,dict (不可哈希) 不可变数据类型:元祖,bool,int,str (可哈希) py ...
- Codeforces Round #561 (Div. 2)
C. A Tale of Two Lands 题意: 给出 n 个数,问有多少点对(x,y)满足 |x-y| ≤ |x|,|y| ≤ |x+y|: (x,y) 和 (y,x) 表示一种答案: 题解: ...
- vue项目安装scss,以及安装scss报错(this.getResolve is not a function)
1.安装scss: npm install node-sass sass-loader vue-style-loader --save-dev //安装node-sass sass-loader vu ...
- addEventListener() 方法,事件监听(去哪儿网用到过)
addEventListener() 方法,事件监听 你可以使用 removeEventListener() 方法来移除事件的监听. 语法 element.addEventListener(event ...
- Hamcrest Tutorial
Java Hamcrest Home Hamcrest Tutorial Introduction Hamcrest is a framework for writing matcher object ...
- 阿里云 CentOS8 Repo
# CentOS-Base.repo # # The mirror system uses the connecting IP address of the client and the # upda ...
- 【Jenkins】构建一个maven项目
一 .Ubuntu18.04安装Maven 官方安装文档:http://maven.apache.org/install.html ①去官网下载maven: ②解压到/opt/maven目录(我安装在 ...
- Dubbo-本地Bean测试
Dubbo本地测试API的Bean 一.建立一个测试类文件 二.测试API // 自己要测试的API public static final XxApi xxApi; 三.注入Bean static ...
- vue学习笔记(二)vue的生命周期和钩子函数
前言 通过上一章的学习,我们已经初步的了解了vue到底是什么东西,可以干什么,而这一篇博客主要介绍vue的生命周期和它常用的钩子函数,如果有学过java的园友可能有接触到在学习servlet的时候学过 ...