POJ 2553 The Bottom of a Graph (Tarjan)
| Time Limit: 3000MS | Memory Limit: 65536K | |
| Total Submissions: 11981 | Accepted: 4931 |
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 v1to 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
思路:
终于过了。。。。
因为模板错误,让我痛不欲生。
这题完成缩点之后,找出没有出度的点就行了。
http://www.cnblogs.com/ZGQblogs/p/9104381.html
我的模板会在此更新,感谢感谢!
我的上一篇博客里面的代码(POJ 1236)的确是错的,这个里面的代码应该就没问题了。。
代码
#include<iostream>
#include<cstdio>
#include<vector>
#include<stack>
#include<cstring>
using namespace std;
int n,m;
int book[50008];
int low[50008],num[50008],cnt=1,index;
int color[50008];
bool flag[50008];
vector<int>u[50008];
stack<int>st;
int sig=0;
void Tarjan(int t)
{
num[t]=low[t]=++index;
st.push(t);
book[t]=true;
int siz=u[t].size();
for(int i=0;i<siz;i++){
if(!num[u[t][i]]){
Tarjan(u[t][i]);
low[t]=min(low[t],low[u[t][i]]);
}
else if(book[u[t][i]]){low[t]=min(low[t],low[u[t][i]]);}
} if(num[t]==low[t]){
sig++;
while(1){ cnt=st.top();
st.pop();
color[cnt]=sig;
book[cnt]=0;
if(cnt==t){break;}
}
}
} bool init()
{
scanf("%d",&n);
for(int i=1;i<=n;i++){
u[i].clear();
}
while(!st.empty()){
st.pop();
}
memset(book,0,sizeof(book));
memset(low,0,sizeof(low));
memset(flag,0,sizeof(flag));
memset(color,0,sizeof(color));
memset(num,0,sizeof(num));
index=0;
if(n==0){return false;}
scanf("%d",&m);
int x,y;
for(int i=1;i<=m;i++){
scanf("%d%d",&x,&y);
u[x].push_back(y);
}
return true;
} void solve()
{
int siz;
int tle=0;
for(int i=1;i<=n;i++){
siz=u[i].size();
for(int j=0;j<siz;j++){
if(color[u[i][j]]!=color[i]){flag[color[i]]=true;}
}
} for(int i=1;i<=n;i++){
if(!flag[color[i]]){
tle++?printf(" %d",i):printf("%d",i);
}
}
printf("\n");
} int main()
{
while(init()){
for(int i=1;i<=n;i++){
if(!num[i]){Tarjan(i);cnt++;}
}
solve();
}
}
POJ 2553 The Bottom of a Graph (Tarjan)的更多相关文章
- POJ 2553 The Bottom of a Graph Tarjan找环缩点(题解解释输入)
Description We will use the following (standard) definitions from graph theory. Let V be a nonempty ...
- POJ 2553 The Bottom of a Graph TarJan算法题解
本题分两步: 1 使用Tarjan算法求全部最大子强连通图.而且标志出来 2 然后遍历这些节点看是否有出射的边,没有的顶点所在的子强连通图的全部点,都是解集. Tarjan算法就是模板算法了. 这里使 ...
- [poj 2553]The Bottom of a Graph[Tarjan强连通分量]
题意: 求出度为0的强连通分量. 思路: 缩点 具体有两种实现: 1.遍历所有边, 边的两端点不在同一强连通分量的话, 将出发点所在强连通分量出度+1. #include <cstdio> ...
- POJ 2553 The Bottom of a Graph(强连通分量)
POJ 2553 The Bottom of a Graph 题目链接 题意:给定一个有向图,求出度为0的强连通分量 思路:缩点搞就可以 代码: #include <cstdio> #in ...
- poj 2553 The Bottom of a Graph(强连通分量+缩点)
题目地址:http://poj.org/problem?id=2553 The Bottom of a Graph Time Limit: 3000MS Memory Limit: 65536K ...
- poj 2553 The Bottom of a Graph【强连通分量求汇点个数】
The Bottom of a Graph Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 9641 Accepted: ...
- POJ 2553 The Bottom of a Graph (强连通分量)
题目地址:POJ 2553 题目意思不好理解.题意是:G图中从v可达的全部点w,也都能够达到v,这种v称为sink.然后升序输出全部的sink. 对于一个强连通分量来说,全部的点都符合这一条件,可是假 ...
- poj 2553 The Bottom of a Graph : tarjan O(n) 存环中的点
/** problem: http://poj.org/problem?id=2553 将所有出度为0环中的点排序输出即可. **/ #include<stdio.h> #include& ...
- POJ 2553 The Bottom of a Graph 【scc tarjan】
图论之强连通复习开始- - 题目大意:给你一个有向图,要你求出这样的点集:从这个点出发能到达的点,一定能回到这个点 思路:强连通分量里的显然都可以互相到达 那就一起考虑,缩点后如果一个点有出边,一定不 ...
随机推荐
- dataTable之自定义按钮实现全表 复制 打印 导出 重载
//本文对常用表格插件datatable 的自定义按钮功能键进行详细解释//其中 15-78行是定义表单//16 18 19 三行定义自定义功能按钮 实现对全表的 复制 打印 导出(csv即excel ...
- thinkphp5 实现搜索分页能下一页保留搜索条件
正常情况下: 搜索后分页了,点击第二页,进入页面之前的搜索条件没有了. 如代码 $keywords=$this->request->param('keywords'); $this-> ...
- centos无网络问题
- Clover file list
/Volumes/EFI//EFI: total 4 drwxrwxrwx 1 jianweiliu staff 512 Apr 20 12:17 APPLE drwxrwxrwx@ 1 jianwe ...
- ASP.NET Core 2.0 Cookie Authentication
using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Builder; using Microso ...
- Get started with Docker for Windows
Welcome to Docker for Windows! Docker is a full development platform for creating containerized apps ...
- Tom和Jerry在下棋
题目描述 方法: 状压DP #include <cstdio> #define bc(x) (__builtin_popcount(x)) ; ; << maxn][maxn ...
- [WC2018]即时战略——动态点分治(替罪羊式点分树)
题目链接: [WC2018]即时战略 题目大意:给一棵结构未知的树,初始时除1号点其他点都是黑色,1号点是白色,每次你可以询问一条起点为白色终点任意的路径,交互库会自动返回给你这条路径上与起点相邻的节 ...
- BZOJ3224普通平衡树——旋转treap
题目: 此为平衡树系列第一道:普通平衡树您需要写一种数据结构,来维护一些数,其中需要提供以下操作:1. 插入x数2. 删除x数(若有多个相同的数,因只删除一个)3. 查询x数的排名(若有多个相同的数, ...
- Git——入门操作加创建账号【三】
创建账号 GitHub https://github.com/ 码云 https://gitee.com/ 无论是github还是码云,创建账号都是非常简单快捷的,大家可以自行选择创建下,不过建议最好 ...