牛客网

poj 2553

文章目录

  • Description
  • 题意:
  • 题解:
  • 代码:

Description

We will use the following (standard) definitions from graph theory.
Let V be a nonempty and finite set, its elements being called vertices
(or nodes). Let E be a subset of the Cartesian product V×V, its
elements being called edges. Then G=(V,E) is called a directed graph.
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

The input contains several test cases, each of which corresponds to a
directed graph G. Each test case starts with an integer number v,
denoting the number of vertices of G=(V,E), where the vertices will be
identified by the integer numbers in the set V={1,…,v}. You may
assume that 1<=v<=5000. That is followed by a non-negative integer e
and, thereafter, e pairs of vertex identifiers v1,w1,…,ve,we with
the meaning that (vi,wi)∈E. There are no edges other than specified by
these pairs. The last test case is followed by a zero.

Output

For each test case output the bottom of the specified graph on a
single line. To this end, print the numbers of all nodes that are
sinks in sorted order separated by a single space character. If the
bottom is empty, print an empty line.


Sample Input

3 3
1 3 2 3 3 1
2 1
1 2
0

Sample Output

1 3
2

题意:

,一个点所能到达的任意一个点都能返回这个点,那么这个点称为bottom点,找出所有的bottom点。
读入:
第一行 v e(点数和边数)
第二行 具体边的方向
当读0时结束

题解:

先找到图中所有强连通图,强连通图内的每个点都是互通的,然后Tarjan缩点,得到新图,如果一个点没有出去的边(即出度为0,无须管入度),那么这个点就符合要求。因为我们要找互相能到达的点,这又是缩点后的图,一个点有出度,肯定不能再返回。
大致是这个意思,好好想想,这算是Tarjan的模板题
(读题太费劲了。。。)

代码:

#include<bits/stdc++.h>
#define mem(a) memset(a,0,sizeof(a))
using namespace std;
typedef long long ll;
const int maxn=5020;
vector<int>v[maxn];
int n,m,x,y,k,top,num,inf;
int pos[maxn],vis[maxn],low[maxn],dnf[maxn];
int ans[maxn],cnt[maxn],degree[maxn];
void init()
{k=0;top=0;num=0;for(int i=0;i<=n;i++)v[i].clear();mem(vis);mem(pos);mem(ans);mem(cnt);mem(dnf);mem(low);mem(degree);
}
void tarjan(int u)
{dnf[u]=low[u]=++k;pos[++top]=u;vis[u]=1;for(int i=0;i<v[u].size();i++){int to=v[u][i];if(!dnf[to]){tarjan(to);low[u]=min(low[u],low[to]);}else if(vis[to])low[u]=min(low[u],dnf[to]);}//以上为正常的tarjan求强连通分量 if(low[u]==dnf[u]){num++;while(1){inf=pos[top--];//栈中最上面的点 ans[inf]=num;// 同一连通分量的点上相同的颜色,相当于缩点 vis[inf]=0;//if(inf==u)break;//遍历完后退出 }}
}
void dfs(int u)
{cnt[u]=ans[u];//给点标记,防止重复搜索 for(int i=0;i<v[u].size();i++){int to=v[u][i];if(ans[u]!=ans[to])degree[ans[u]]++;//如果这个点有出度,值++ if(!cnt[to])dfs(to);//如果指向的点没被搜索过 }
}
void solve()
{for(int i=1;i<=n;i++)if(!dnf[i])tarjan(i);//如果这个点还没走过 for(int i=1;i<=n;i++)if(!cnt[i])dfs(i);//如果这个点没被走过 bool flag=1;for(int i=1;i<=n;i++){if(!degree[ans[i]])//判断这个点是否有出度 {if(flag)//这里主要是为了控制格式,第一个不带空格,后面带空格 {printf("%d",i);flag^=1;}else printf(" %d",i);}}cout<<endl;
}
int main()
{while(cin>>n&&n){cin>>m;init();//初始化所有数组与值 for(int i=0;i<m;i++){cin>>x>>y;v[x].push_back(y);//生成邻接表 }solve();}return 0;
}

对了,牛客网和poj的这个题都不支持头文件,否则编译错误

The Bottom of a Graph Poj 2553相关推荐

  1. 【POJ - 2553】The Bottom of a Graph(tarjan强连通分量缩点,模板题)

    题干: We will use the following (standard) definitions from graph theory. Let V be a nonempty and fini ...

  2. poj 2553 The Bottom of a Graph 未完

    题目大意:如果v点能够到的点,反过来能够到达v点,则称这个点为sink点,输出所有的sink点 RE #include <iostream>//https://blog.csdn.net/ ...

  3. POJ 2553 The Bottom of a Graph

    POJ_2553 这个题目本来是比较容易下手的,最后只需要求所有出度为0的强连通分量所包含的点,然后按字典序输出即可.但是由于一开始用邻接表写代码的时候由于读入边和存储边的写法问题导致我一直TLE,后 ...

  4. 【转载】图论 500题——主要为hdu/poj/zoj

    转自--http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...

  5. poj pku图论、网络流入门题总结、汇总

    poj pku图论.网络流入门题总结.汇总 分类: acm图论 2010-08-25 18:49 243人阅读 评论(0) 收藏 举报 网络算法networkgraphconstructioninte ...

  6. NOIP 好题推荐(DP+搜索+图论)POJ ZOJ

    NOIP好题推荐(DP+搜索+图论)POJ ZOJ 1370 Gossiping (数论->模线性方程有无解的判断)+(图论->DFS)  1090 Chain ->格雷码和二进制码 ...

  7. POJ 图论分类 + DP(较全 自己又加了点)

    DP -----------动态规划 状态压缩DP 2411 (棋盘规模较大)状态压缩DP+DFS+滚动数组 2664 (棋盘规模较小)直接递推即可(DP) 2506 (棋盘规模较小)直接递推即可(D ...

  8. hdu与poj题目分类

    POJ 初期: 一.基本算法: (1)枚举. (poj1753,poj2965) (2)贪心(poj1328,poj2109,poj2586) (3)递归和分治法. (4)递推. (5)构造法.(po ...

  9. 【HDOJ图论题集】【转】

    1 =============================以下是最小生成树+并查集====================================== 2 [HDU] 3 1213 How ...

最新文章

  1. Django restframework之Token验证的缺陷及jwt的简单使用
  2. Excel 公式 两个时间比大小
  3. Python中如何写控制台进度条的整理
  4. 久谦咨询python笔试题目_【久谦咨询面试|面试题】-看准网
  5. “\”C++中的换行符
  6. python可以代替plc吗_PLC可以代替安全控制器吗?电气工程师告诉你
  7. matlab的try函数,matlab – 是否可以在没有try块的情况下测试函数句柄?
  8. python获取对象的大小_Python实现计算对象的内存大小示例
  9. AI 时代下的海量业务智能监控实践
  10. python随机产生10个数然后前5个升序后5个降序_编写程序,生成包含 20 个随机数的列表,然后将前 10 个元素升序排列,后 10 个元素降序排列,并输出结果。_学小易找答案...
  11. 「Linux」VMware安装centos7(一)
  12. iOS:删除、插入、移动单元格
  13. Linux 小知识翻译 - 目录 (完结)
  14. 使用花生壳做内网穿透
  15. SPI通信协议详解(一)
  16. 使用ANSYS进行对称边界的模态分析,制作【春节快乐】
  17. 详细讲解WIN7系统上Virtualbox4.2.8安装RedHat Enterprise Linux 6.4
  18. python查看文件行数_python如何获取打开文件的行数?
  19. 让电商运营10倍提效的自动化工具,你get了吗?
  20. Java设计养老院系统_基于JavaWeb的养老院管理系统设计任务书

热门文章

  1. 我背着女朋友,用 Python 偷偷抓取了她的行踪
  2. 细数近年来机器学习研究的几大怪现状
  3. 为什么程序员发现不了自己的BUG?
  4. python如何退出命令行_如何退出python命令行
  5. android蓝牙设计与实现,一个Android客户端的蓝牙支付系统设计与实现
  6. c mysql binlog_Mysql Binlog
  7. c语言随机数循环延迟,C语言生成随机数的函数、延时函数
  8. leetcode977. 有序数组的平方(暴力+双指针)
  9. 岛屿类问题的广度优先深度优先双解法(Leetcode题解-Python语言)
  10. mysql id 字段类型转换_mysql 数据类型转换