POJ2553 汇点个数(强连通分量
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 12070 | Accepted: 4971 |
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 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.
Input
Output

Sample Input
3 3
1 3 2 3 3 1
2 1
1 2
0
Sample Output
1 3
2
Source
定义:点v是汇点须满足 --- 对图中任意点u,若v可以到达u则必有u到v的路径;若v不可以到达u,则u到v的路径可有可无。
题意:在n个点m条边的有向图里面,问有多少个点是汇点。
分析:若SCC里有一点不是汇点,那么它们全不是,反之也如此。所以一个SCC里面的点要么全是,要么全不是。在求出SCC并缩点后,任一个编号为A的SCC若存在指向编号为B的SCC的边,那么其所有点必不是汇点(因为编号为B的SCC不可能存在指向编号为A的SCC的边)。若编号为A的SCC没有到达其他SCC的路径,那么该SCC里面所有点必是汇点。因此判断的关键在于SCC的出度是否为0.
思路:先用tarjan求出所有SCC,然后缩点后找出所有出度为0的SCC,并用数字存储点,升序排列后输出。
代码:
#include"bits/stdc++.h" #define db double
#define ll long long
#define vl vector<ll>
#define ci(x) scanf("%d",&x)
#define cd(x) scanf("%lf",&x)
#define cl(x) scanf("%lld",&x)
#define pi(x) printf("%d\n",x)
#define pd(x) printf("%f\n",x)
#define pl(x) printf("%lld\n",x)
#define rep(i, n) for(int i=0;i<n;i++)
using namespace std;
const int N = 1e6 + ;
const int mod = 1e9 + ;
const int MOD = ;
const db PI = acos(-1.0);
const db eps = 1e-;
const ll INF = 0x3fffffffffffffff;
int n,m;
struct P{int to,nxt;}e[N];
int head[N];
bool ins[N];
int beg[N];
int low[N],dfn[N];
int out[N];
stack<int> s; int cnt,num,id;
void add(int u,int v){
e[cnt].to=v;
e[cnt].nxt=head[u];
head[u]=cnt++;
}
void tarjan(int u)
{
low[u]=dfn[u]=++id;
ins[u]=;
s.push(u);
for(int i=head[u];~i;i=e[i].nxt){
int v=e[i].to;
if(!dfn[v]) tarjan(v),low[u]=min(low[u],low[v]);
else if(ins[v]) low[u]=min(low[u],dfn[v]);
}
if(low[u]==dfn[u]){
int v;
do{
v=s.top();s.pop();
beg[v]=num;//强连通分量编号
ins[v]=;
}while(u!=v);
num++;
}
}
int main(){
while (scanf("%d%d",&n,&m)==&&n){
memset(head,-, sizeof(head));
memset(low,, sizeof(low));
memset(dfn,, sizeof(dfn));
memset(ins,, sizeof(ins));
memset(out,, sizeof(out));
cnt=num=id=;
for(int i=;i<m;i++){
int x,y;
ci(x),ci(y);
add(x,y);
}
for(int i=;i<=n;i++)
if(!dfn[i]) tarjan(i);
for(int i=;i<=n;i++){
for(int j=head[i];~j;j=e[j].nxt){
int v=e[j].to;
if(beg[i]!=beg[v]) out[beg[i]]++;
}
}
bool ok=;
for(int i=;i<=n;i++){
if(!out[beg[i]]){//所在强连通分量出度为0
if(ok) printf("%d",i),ok=;
else printf(" %d",i);
}
}
puts("");
}
return ;
}
POJ2553 汇点个数(强连通分量的更多相关文章
- Ex3_15 判断图是否是一个强连通分量 判断点是否在汇点强连通分量中_十一次作业
(a) 可以用图中的每一个顶点表示街道中的每个十字路口,由于街道都是单行的,所以图是有向图,若从一个十字路口都有一条合法的路线到另一个十字路口,则图是一个强连通图.即要验证的是图是否是一个强连通图. ...
- HDU 1269 迷宫城堡(判断有向图强连通分量的个数,tarjan算法)
迷宫城堡 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- POJ3180(有向图强连通分量结点数>=2的个数)
The Cow Prom Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1451 Accepted: 922 Descr ...
- POJ2186 (强连通分量缩点后出度为0的分量内点个数)
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 27820 Accepted: 11208 De ...
- UVALive 4262——Trip Planning——————【Tarjan 求强连通分量个数】
Road Networks Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Stat ...
- poj2186--tarjan+缩点(有向图的强连通分量中点的个数)
题目大意: 每一头牛的愿望就是变成一头最受欢迎的牛.现在有N头牛,给你M对整数(A,B),表示牛A认为牛B受欢迎. 这 种关系是具有传递性的,如果A认为B受欢迎,B认为C受欢迎,那么牛A也 ...
- POJ2553 The Bottom of a Graph(强连通分量+缩点)
题目是问,一个有向图有多少个点v满足∀w∈V:(v→w)⇒(w→v). 把图的强连通分量缩点,那么答案显然就是所有出度为0的点. 用Tarjan找强连通分量: #include<cstdio&g ...
- 【poj2553】The Bottom of a Graph(强连通分量缩点)
题目链接:http://poj.org/problem?id=2553 [题意] 给n个点m条边构成一幅图,求出所有的sink点并按顺序输出.sink点是指该点能到达的点反过来又能回到该点. [思路] ...
- (转)求有向图的强连通分量个数(kosaraju算法)
有向图的连通分量的求解思路 kosaraju算法 逛了很多博客,感觉都很难懂,终于找到一篇能看懂的,摘要记录一下 原博客https://www.cnblogs.com/nullzx/p/6437926 ...
随机推荐
- db2的count()函数和sum()函数的用法
一.count()函数可以使用参数,例如count(*)和count(列名) count(*)用来计算在指定条件下,满足条件的行数,例如: select count(*) from tablename ...
- github的初步认识
第一部分:我用代码写了一个简单的可以运行的helloworld代码. 其链接为:https://github.com/ljw305503/CK01.git 第二部分:GitHub是一个分布式的版本控制 ...
- 通过describe命令学习Kubernetes的pod属性详解
我们可以首先使用kubectl get pods命令得到pod列表,比如我们想研究pod nginx-storage-pod的明细: 使用命令kubectl describe pod nginx-st ...
- 前端:Bootstrap框架
一,bootstrap介绍 Bootstrap是Twitter开源的基于HTML.CSS.JavaScript的前端框架. 它是为实现快速开发Web应用程序而设计的一套前端工具包. 它支持响应式布局, ...
- Android学习笔记_43_网络通信之文件断点上传
1.建立服务端,用于接收上传的文件.这里使用Socket,文件可能会比较大.采用多线程编程,防止并发. package com.socket.service; import java.io.File; ...
- Android学习笔记_20_访问应用权限汇总
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com. ...
- POJ 2533 Longest Ordered Subsequence(裸LIS)
传送门: http://poj.org/problem?id=2533 Longest Ordered Subsequence Time Limit: 2000MS Memory Limit: 6 ...
- focal loss for dense object detection
温故知新 focal loss for dense object detection,知乎上一人的评论很经典.hard negative sampling, 就是只挑出来男神(还是最难追的),而foc ...
- viewpager中 pagerAdapter使用详解
必须覆盖以下方法instantiateItem(ViewGroup, int) 这个方法,return一个对象,这个对象表明了PagerAdapter适配器选择哪个对象*放在当前的ViewPager中 ...
- JavaScript函数变量作用域
变量作用域 在JavaScript中,用var申明的变量实际上是有作用域的. 如果一个变量在函数体内部申明,则该变量的作用域为整个函数体,在函数体外不可引用该变量. 如果两个不同的函数各自申明了同一变 ...