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】
图论之强连通复习开始- - 题目大意:给你一个有向图,要你求出这样的点集:从这个点出发能到达的点,一定能回到这个点 思路:强连通分量里的显然都可以互相到达 那就一起考虑,缩点后如果一个点有出边,一定不 ...
随机推荐
- APP test
在讲APP测试之前,先讲一下,目前APP的操作系统以及APP相关基础知识. 一.APP基础知识 1.操作系统# 现在移动端的操作系统主流的分为两种:(1)安卓系统 (2)IOS系统. 2.安卓系统# ...
- Codeforces Round #470 Div. 1
A:暴力枚举x2的因子,由此暴力枚举x1,显然此时减去其最大质因子并+1即为最小x0. #include<iostream> #include<cstdio> #include ...
- Python中xlwt解析
1.导入模块 import xlwt 2.构造excel表 workbook = xlwt.Workbook() #返回一个工作簿对象 3.构造sheet w ...
- 进程PID 与PPID
# 同一个程序执行多次是多个进程 import time import os print('爹是:',os.getppid()) #查看父进程 print('me是: ',os.getpid()) # ...
- IDEA中Git分支未push的变更集如何合并到另一个分支
使用rebase命令 刚开始,A分支和B分支的代码是一样的,把A分支checkout 为当前分支,并且修改了代码,进行[commit]和[push],commit成功了,但是push没有权限. 这个时 ...
- npm 常规错误
Unexpected end of JSON input while parsing near 意外结束.JSON解析期间 解决办法: npm cache clean --force 解释:Force ...
- ZOJ 3846 GCD Reduce//水啊水啊水啊水
GCD Reduce Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge You are given a sequ ...
- 【比赛】NOIP2018 铺设道路
原题,而且还是CCF自己的 考虑对于一段最长不上升序列,无论如何都至少有序列第一个数的贡献,可以知道,这个贡献是可以做到且最少的 然后对于序列最后一位,也就是最小的那一个数,可以和后面序列拼起来的就拼 ...
- 支持markwon写ppt的工具marp-调研
1.需求 使用markdown写ppt 支持大部分markdown语法,含高亮.公式及表格等 支持转为html,在浏览器中展示 2.调研 符合条件的工具:marp 用法参考 Marp之简单编写PPT格 ...
- 自学华为IoT物联网_10 IoT联接管理平台配置及开发实验1
点击返回自学华为IoT物流网 自学华为IoT物联网_10 IoT联接管理平台配置及开发实验1 实验1:OceanConnect平台实验 通过基本的编程操作与配置,帮助读者熟悉O ...