poj2553 强连通缩点
| Time Limit: 3000MS | Memory Limit: 65536K | |
| Total Submissions: 10114 | Accepted: 4184 |
Description
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 Gand 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.
Input
Output

Sample Input
3 3
1 3 2 3 3 1
2 1
1 2
0
Sample Output
1 3
2
题意:
n个点,m条边,并且是单向边。求有多少个顶点,满足它能到的点也能够到达它。
思路:
对于强连通中的点,肯定能够互相到达,所以可以强连通缩点,此时只要找到出度为0的点,其连通分量连所有的点就是答案。因为
如果该点的出度不为0,那么肯定有新的该点能够到达的点,由于已经缩点了,不可能出现有强连通的情况,所以出度不为0的点不满足要求。
/*
* Author: sweat123
* Created Time: 2016/6/25 14:32:24
* File Name: main.cpp
*/
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<string>
#include<vector>
#include<cstdio>
#include<time.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#define INF 1<<30
#define MOD 1000000007
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define pi acos(-1.0)
using namespace std;
const int MAXN = ;
struct node{
int from;
int to;
int next;
}edge[MAXN*];
int pre[MAXN],vis[MAXN],dfn[MAXN],low[MAXN],n,m,ind;
int f[MAXN],siz[MAXN],num,dep,out[MAXN],in[MAXN];
stack<int>s;
vector<int>p[MAXN];
void add(int x,int y){
edge[ind].from = x;
edge[ind].to = y;
edge[ind].next = pre[x];
pre[x] = ind ++;
}
void dfs(int rt){
dfn[rt] = low[rt] = ++dep;
vis[rt] = ;
s.push(rt);
for(int i = pre[rt]; i != -; i = edge[i].next){
int t = edge[i].to;
if(!dfn[t]){
dfs(t);
low[rt] = min(low[rt],low[t]);
} else if(vis[t]){
low[rt] = min(low[rt],dfn[t]);
}
}
if(low[rt] == dfn[rt]){
++num;
while(!s.empty()){
int tp = s.top();
s.pop();
vis[tp] = ;
f[tp] = num;
siz[num] ++;
p[num].push_back(tp);
if(tp == rt)break;
}
}
}
void setcc(){
num = ;
dep = ;
memset(in,,sizeof(in));
memset(out,,sizeof(out));
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low));
for(int i = ; i <= n; i++){
if(!dfn[i]){
dfs(i);
}
}
memset(pre,-,sizeof(pre));
int ret = ind;
for(int i = ; i < ret; i++){
int x = f[edge[i].from];
int y = f[edge[i].to];
if(x == y)continue;
add(x,y);
out[x] ++;
in[y] ++;
}
vector<int>ans;
for(int i = ; i <= num; i++){
if(!out[i]){
for(int j = ; j < p[i].size(); j++){
ans.push_back(p[i][j]);
}
}
}
sort(ans.begin(),ans.end());
for(int i = ; i < ans.size(); i++){
if(i == )printf("%d",ans[i]);
else printf(" %d",ans[i]);
}
printf("\n");
}
int main(){
while(~scanf("%d",&n)){
if(n == )break;
scanf("%d",&m);
if(n == ){
printf("1\n");
continue;
}
ind = ;
for(int i = ; i <= n; i++){
p[i].clear();
}
memset(pre,-,sizeof(pre));
while(!s.empty())s.pop();
memset(f,-,sizeof(f));
memset(siz,,sizeof(siz));
for(int i = ; i <= m; i++){
int x,y;
scanf("%d%d",&x,&y);
add(x,y);
}
setcc();
}
return ;
}
| Time Limit: 3000MS | Memory Limit: 65536K | |
| Total Submissions: 10114 | Accepted: 4184 |
Description
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 Gand 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.
Input
Output

Sample Input
3 3
1 3 2 3 3 1
2 1
1 2
0
Sample Output
1 3
2
poj2553 强连通缩点的更多相关文章
- hdu 4635 Strongly connected 强连通缩点
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4635 题意:给你一个n个点m条边的图,问在图不是强连通图的情况下,最多可以向图中添多少条边,若图为原来 ...
- BZOJ 1051: [HAOI2006]受欢迎的牛 强连通缩点
题目链接: http://www.lydsy.com/JudgeOnline/problem.php?id=1051 题解: 强连通缩点得到DAG图,将图转置一下,对入度为零的点跑dfs看看能不能访问 ...
- hdu 2767 Proving Equivalences 强连通缩点
给出n个命题,m个推导,问最少添加多少条推导,能够使全部命题都能等价(两两都能互推) 既给出有向图,最少加多少边,使得原图变成强连通. 首先强连通缩点,对于新图,每一个点都至少要有一条出去的边和一条进 ...
- UVA - 11324 The Largest Clique 强连通缩点+记忆化dp
题目要求一个最大的弱联通图. 首先对于原图进行强连通缩点,得到新图,这个新图呈链状,类似树结构. 对新图进行记忆化dp,求一条权值最长的链,每一个点的权值就是当前强连通分量点的个数. /* Tarja ...
- poj-1904(强连通缩点)
题意:有n个王子,每个王子都有k个喜欢的女生,王子挑选喜欢的女生匹配,然后再给你n个王子最开始就定好的匹配,每个王子输出能够结合且不影响其他王子的女生匹配 解题思路:强连通缩点,每个王子与其喜欢的女生 ...
- NOIP2017提高组Day1T3 逛公园 洛谷P3953 Tarjan 强连通缩点 SPFA 动态规划 最短路 拓扑序
原文链接https://www.cnblogs.com/zhouzhendong/p/9258043.html 题目传送门 - 洛谷P3953 题目传送门 - Vijos P2030 题意 给定一个有 ...
- BZOJ1179 [Apio2009]Atm Tarjan 强连通缩点 动态规划
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1179 题意概括 有一个有向图,每一个节点有一个权值,其中有一些结束点. 现在,你要从S出发,到达任 ...
- BZOJ1051 [HAOI2006]受欢迎的牛 Tarjan 强连通缩点
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1051 题意概括 有n只牛,有m个羡慕关系. 羡慕关系具有传递性. 如果A羡慕B,B羡慕C,那么我们 ...
- 强连通缩点— HDU1827
强连通缩点以后最终形成的是一棵树 我们可以根据树的性质来看缩点以后的强连通分量图,就很好理解了 /* gyt Live up to every day */ #include<cstdio> ...
随机推荐
- Java虚拟机详解----JVM常见问题总结
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...
- mysql中char与varchar的区别分析
char 固定长度,所以在处理速度上要比varchar快速很多,但是对费存储空间,所以对存储不大,但在速度上有要求的可以使用char类型,反之可以用varchar类型来实例 建意: myisam 存储 ...
- GIT安装和使用
GIT 首先登陆github账号 在本地创建一个文件夹 点击文件夹,右键,选择Git create repository here 操作之后,会生成一个.git文件(这个文件为隐藏文件) 在此 ...
- Sql-Server应用程序的高级注入
本文作者:Chris Anley 翻译: luoluo [luoluonet@hotmail.com] [目 录] [概要] [介绍] [通过错误信息获取信息] [更深入的访问] [xp_cmdshe ...
- BZOJ 1304: [CQOI2009]叶子的染色
1304: [CQOI2009]叶子的染色 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 566 Solved: 358[Submit][Statu ...
- 从零开始使用Jenkins来构建Docker容器(Ubuntu 14.04)
当开发更新了代码,提交到Gitlab上,然后由测试人员触发Jenkins,于是一个应用的新版本就被构建了.听起来貌似很简单,duang~duang~duang,我用了是这样,你们用了也是这样,看起来这 ...
- LeetCode 2. Add Two Numbers swift
// // main.swift // leetcode02 // // Created by GuoLa on 16/1/21. // Copyright © 2016年 GuoLa. All ri ...
- C#.NET 大型通用信息化系统集成快速开发平台 4.0 版本 - 标准省市县数据的公司选择窗口实现
若全国各地有上千个分公司,加盟店,网点,那就需要一个友善的选择分公司的功能,得有标准的全国省市县的划分数据.这样有了牢靠的基础数据后,才能开发程序得心应手了.当习惯了开发一个公司内部系统时,全国性大公 ...
- Scrapy 爬虫
Scrapy 爬虫 使用指南 完全教程 scrapy note command 全局命令: startproject :在 project_name 文件夹下创建一个名为 project_name ...
- web—第三章XHTML
web—第三章XHTML 又是一周 我们学的了做表单:一开始我以为表单是表格.但结果:表单是以采集和提交用户输入数据的,这样讲很迷,说简单点就是登陆端.比如:Facebook.twitter.Ins ...