The Bottom of a Graph Poj 2553

牛客网

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的这个题都不支持头文件,否则编译错误

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/320003.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

P7244-章节划分【RMQ,贪心,递归】

正题 题目链接:https://www.luogu.com.cn/problem/P7244?contestId38911 题目大意 nnn个数字&#xff0c;分成连续非空的kkk段要求每一段的最大值的gcdgcdgcd最大。 解题思路 首先答案一定是最大值的约数&#xff0c;这些数不多我们可以枚举这些数xxx。然后我们称xxx的倍数的…

[XSY3382] 专家系统(二分+线段树)

XSY3382 二分ccc&#xff0c;问题变成能否用一个长ccc宽ccc的矩形框住至少kkk个点。 二维数点问题考虑用扫描线解决。将所有点按照xxx从小到大排序。 枚举一段xxx坐标相差不超过ccc的点&#xff08;双指针推进&#xff09;&#xff0c;初始想法是根据这些点的yyy值建一棵权值…

关于.NET Core是否应该支持WCF Hosting的争论

本文要点本文试图回答“.NET Core 是否应该支持 Windows 通信基础&#xff08;WCF&#xff09; Hosting&#xff1f;”的问题&#xff1b;支持者论据&#xff1a;许多工程师喜欢把 WCF 作为一种编程模型&#xff0c;不希望因为迁移到 .NET Core 而产生&#xff08;机会成本&…

战争尾声(nowcoder 215073)

战争尾声 nowcoder 215073 题目大意 在平面直角坐标系&#xff08;x,y范围均为1~200&#xff09;上&#xff0c;给你n个点&#xff0c;让你找到一个点&#xff0c;使其到所有点直线距离相等&#xff08;答案坐标均为整数&#xff09; 输入样例#1 2 1 2 2 1输出样例#1 1 1…

HDU1269 迷宫城堡(模板题)

HDU1269 迷宫城堡 文章目录Problem Description题解&#xff1a;Problem Description 为了训练小希的方向感&#xff0c;Gardon建立了一座大城堡&#xff0c;里面有N个房间(N<10000)和M条通道(M<100000)&#xff0c;每个通道都是单向的&#xff0c;就是说若称某通道连通…

照看小猫(nowcoder 217602)

照看小猫 nowcoder 217602 题目大意 有n只小猫&#xff0c;对于第i只小猫&#xff0c;给它取一个以小写字母组成的名字&#xff08;长度不大于aia_iai​&#xff09;&#xff0c;问你使所有小猫名字不同的方案数 样例#1 输入样例#1 1 1输出样例#1 26样例解释#1 猫咪的名…

AT3955-[AGC023D]Go Home【结论,递归】

正题 题目链接:https://www.luogu.com.cn/problem/AT3955 题目大意 nnn个房子在一个坐标轴上&#xff0c;第iii个有pip_ipi​个人在位置xix_ixi​&#xff0c;开始所有人都在一辆在位置sss的车上&#xff0c;每一时刻&#xff0c;每个人都会投票决定车的走向&#xff0c;票多的…

[XSY3383]多线程(笛卡尔树,DP)

%%%tjytjytjy的笛卡尔树做法&#xff1a; 设dp(l,r,Amin,Bmin)dp(l,r,Amin,Bmin)dp(l,r,Amin,Bmin)为把c[l],c[l1],...,c[r]c[l],c[l1],...,c[r]c[l],c[l1],...,c[r]划到A,BA,BA,B两线程中&#xff0c;且划到AAA线程的数>Amin>Amin>Amin&#xff0c;划到BBB线程的数&…

ASP.NET Core 2.0使用Autofac实现IOC依赖注入竟然能如此的优雅简便

初识ASP.NET Core的小伙伴一定会发现&#xff0c;其几乎所有的项目依赖都是通过依赖注入方式进行链式串通的。这是因为其使用了依赖注入 (DI) 的软件设计模式&#xff0c;代码的设计是遵循着“高内聚、低耦合”的原则&#xff0c;使得各个类与类之间的关系依赖于接口&#xff0…

POJ1236 Network of Schools

POJ1236 Network of Schools 文章目录Description题意&#xff1a;题解&#xff1a;代码&#xff1a;Description A number of schools are connected to a computer network. Agreements have been developed among those schools: each school maintains a list of schools t…

【模拟】签订协议(nowcoder 217601)

签订协议 nowcoder 217601 题目大意 给出n个数&#xff0c;现在有一个协议书&#xff0c;让你从1传到n&#xff0c;然后传回1&#xff0c;继续传下去 对于第i个数&#xff0c;如果前面i-1个数已经匹配过了&#xff0c;那么当协议书传过来时即可匹配&#xff0c;否则无法匹配…

P4196-[CQOI2006]凸多边形/[模板]半平面交【计算几何】

正题 题目链接:https://www.luogu.com.cn/problem/P4196 题目大意 给出nnn个凸多边形&#xff0c;求它们交的面积。 解题思路 就是把凸多边形上每条边作为一个半平面限制然后求一遍半平面交就好了。 具体做法是我们先将点按照级角排序&#xff0c;然后以此把半平面加入双端…

连续段问题小结

一个好用的工具——析合树 oi-wiki 例题 CF526F 题意&#xff1a; 给出一个1~nnn的排列&#xff0c;问有多少个区间的值域是连续的。 题解&#xff1a; 线段树单调栈做法 分治做法 析合树做法 图论做法 CF997E 题意&#xff1a; 给出一个1~nnn的排列&#xff0c;有qqq次…

实战中的asp.net core结合Consul集群Docker实现服务治理

一、前言在写这篇文章之前&#xff0c;我看了很多关于consul的服务治理&#xff0c;但发现基本上都是直接在powershell或者以命令工具的方式在服务器上面直接输入consul agent .... 来搭建启动consul集群&#xff0c;一旦把命令工具关掉&#xff0c;则consul无法再后台启动&…

POJ3177 Redundant Paths

POJ3177 Redundant Paths 文章目录Description题意&#xff1a;题解&#xff1a;代码&#xff1a;Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 21945 Accepted: 9056Description In order to get from one of the F (1 < F < 5,000) grazing fields (…

【最小生成树】路线规划(nowcoder 217603)

路线规划 nowcoder 217603 题目大意 给一个无向连通图&#xff0c;问你在经过的边最少的前提下&#xff0c;从1走过所有点&#xff0c;再走回1的最短距离 样例#1 输入样例#1 5 5 5 4 3 4 3 5 2 3 7 1 2 4 2 4 1输出样例#1 26样例解释#1 最少时间的路径: 1 →2 →…

计算几何学习小记

文章目录前言正题平面运算加减乘积常见问题直线/线段规范交点求垂线/点问题判断点在多边形的内/外求两个圆的交点前言 因为懒得画图理解计算几何所以要来这里鼓励一下自己 以后新学的应该也会写在这里。就当我是水博客 应该都是二维的计算几何&#xff0c;三维的有生之年再学 …

[XSY3343] 程序锁(DP)

XSY3343 先考虑如何判定一个填好的序列会不会gg&#xff1a; 若∃p,q,使s′[p1]t′[q1]−1\exist p,q,使s[p1]t[q1]-1∃p,q,使s′[p1]t′[q1]−1且∑i1ps′[i]∑j1qt′[j]≤0\sum_{i1}^{p}s[i]\sum_{j1}^{q}t[j]\leq 0∑i1p​s′[i]∑j1q​t′[j]≤0&#xff0c;则这个序列必g…

Asp.Net Core SignalR 用泛型Hub优雅的调用前端方法及传参

继续学习最近一直在使用Asp.Net Core SignalR(下面成SignalR Core)为小程序提供websocket支持,前端时间也发了一个学习笔记&#xff0c;在使用过程中稍微看了下它的源码,不得不说微软现在真的强大,很多事情都帮你考虑到了,比如使用Redis,使用Redis后,你的websocket就支持横向扩…

Network POJ-3694

Network POJ-3694 文章目录Description题意&#xff1a;样例分析&#xff1a;题解&#xff1a;代码&#xff1a;Description A network administrator manages a large network. The network consists of N computers and M links between pairs of computers. Any pair of com…