codeforces982F
The Meeting Place Cannot Be Changed
Petr is a detective in Braginsk. Somebody stole a huge amount of money from a bank and Petr is to catch him. Somebody told Petr that some luxurious car moves along the roads without stopping.
Petr knows that it is the robbers who drive the car. The roads in Braginsk are one-directional and each of them connects two intersections. Petr wants to select one intersection such that if the robbers continue to drive the roads indefinitely, they will sooner or later come to that intersection. The initial position of the robbers is unknown. Find such an intersection that fits the requirements.
Input
The first line of the input contains two integers nn and mm (2≤n≤1052≤n≤105, 2≤m≤5⋅1052≤m≤5⋅105) — the number of intersections and the number of directed roads in Braginsk, respectively.
Each of the next mm lines contains two integers uiui and vivi (1≤ui,vi≤n1≤ui,vi≤n, ui≠viui≠vi) — the start and finish of the ii-th directed road. It is guaranteed that the robbers can move along the roads indefinitely.
Output
Print a single integer kk — the intersection Petr needs to choose. If there are multiple answers, print any. If there are no such intersections, print −1−1.
Examples
5 6
1 2
2 3
3 1
3 4
4 5
5 3
3
3 3
1 2
2 3
3 1
1
Note
In the first example the robbers can move, for example, along the following routes: (1−2−3−1)(1−2−3−1), (3−4−5−3)(3−4−5−3), (1−2−3−4−5−3−1)(1−2−3−4−5−3−1). We can show that if Petr chooses the 33-rd intersection, he will eventually meet the robbers independently of their route.
sol : 题意是说让你找到所有环的交点,但我想了很久感觉非常不可做。。。
看了题解很想A掉出题人:先思考如何判断-1,如果有两个环的交集为0,就puts-1,高潮来了,如果超过0.9秒还没找到那个点,就puts-1
在想如何找到那个点,暴力枚举那个点,强制不能经过,如果有环说明那个点不合法,其他不在环上的点也都不合法
#include <bits/stdc++.h>
using namespace std;
typedef int ll;
inline ll read()
{
ll s=;
bool f=;
char ch=' ';
while(!isdigit(ch))
{
f|=(ch=='-'); ch=getchar();
}
while(isdigit(ch))
{
s=(s<<)+(s<<)+(ch^); ch=getchar();
}
return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
if(x<)
{
putchar('-'); x=-x;
}
if(x<)
{
putchar(x+''); return;
}
write(x/);
putchar((x%)+'');
return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=,M=;
int n,m;
#define FIND_TIME clock()/(double)CLOCKS_PER_SEC
namespace Pic
{
bool No[N];
int Bo[N];
int tot=,Next[M],to[M],head[N];
struct Point
{
int Out,Id;
}P[N];
inline bool cmp_Out(Point p,Point q)
{
return p.Out<q.Out;
}
inline void add(int x,int y)
{
Next[++tot]=head[x];
to[tot]=y;
head[x]=tot;
}
bool Flag;
inline void dfs(int x)
{
Bo[x]=;
int i;
for(i=head[x];i;i=Next[i])
{
if(!Bo[to[i]]) dfs(to[i]);
else if(Bo[to[i]]==)
{
Flag=;
}
if(Flag) return;
}
Bo[x]=;
}
inline bool Check(int x)
{
int i,j;
memset(Bo,,sizeof Bo);
Bo[x]=;
Flag=;
for(i=;i<=n;i++) if(!Bo[i])
{
dfs(i);
if(Flag)
{
for(j=;j<=n;j++) if(Bo[j]!=) No[j]=;
return false;
}
}
return true;
}
inline void Solve()
{
int i;
for(i=;i<=m;i++)
{
if(i<=n) P[i].Id=i;
int x,y; R(x); R(y);
add(x,y); P[x].Out++;
}
sort(P+,P+n+,cmp_Out);
for(i=;i<=n;i++)
{
if(FIND_TIME>0.9) break;
if((!No[P[i].Id])&&Check(P[i].Id))
{
Wl(P[i].Id);
return;
}
}
puts("-1");
}
}
int main()
{
int i;
R(n); R(m);
Pic::Solve();
return ;
}
/*
Input
5 6
1 2
2 3
3 1
3 4
4 5
5 3
Output
3 Input
3 3
1 2
2 3
3 1
Output
1
*/
codeforces982F的更多相关文章
随机推荐
- hbuilder IOS APP 打包与发布2
			
在上一篇的<hbuilder IOS APP 打包与发布>中,请求 的 是APP ids . ios开发证书 .和开发配置文件 , 这一篇中就不写发布证书和配置文件的请求,因为流程 ...
 - SpringBoot整合Swagger2搭建API在线文档
			
Swagger,中文"拽"的意思,它是一个功能强大的在线API在线文档,目前它的版本为2.x,所以称为Swagger2.Swagger2提供了在线文档的查阅和测试功能.利用Swag ...
 - java 日志框架总结
			
在项目开发过程中,我们可以通过 debug 查找问题.而在线上环境我们查找问题只能通过打印日志的方式查找问题.因此对于一个项目而言,日志记录是一个非常重要的问题.因此,如何选择一个合适的日志记录框架也 ...
 - Flask发送邮件
			
参考:官方文档:https://pythonhosted.org/Flask-Mail/ 1.安装插件 Flask-Mail (pip install Flask-Mail) 2.配置 Flask- ...
 - UVA - 12169 -扩展欧几里得算法
			
#include<iostream> #include<string.h> #include<algorithm> #include<stdio.h> ...
 - matplotlib 入门之Sample plots in Matplotlib
			
文章目录 Line Plot One figure, a set of subplots Image 展示图片 展示二元正态分布 A sample image Interpolating images ...
 - 设计模式原则——依赖倒转&里氏代换原则
			
设计模式一共有六大原则: 单一原则.开放封闭原则.接口分离原则.里氏替换原则.最少知识原则.依赖倒置原则. 这篇博客是自己对依赖倒转&里氏代换原则的一些拙见,有何不对欢迎大家指出. 依赖倒转原 ...
 - 【转】ubuntu 双机热备
			
1.关于软件安装 sudo apt-get install libssl-dev sudo apt-get install openssl sudo apt-get install libpopt-d ...
 - hibernate设置二级缓存时报错java.lang.NoClassDefFoundError: org/hibernate/engine/jndi/JndiNameException
			
错误提示大概意思是,没有类定义错误,就是找不到要使用的hibernate二级缓存管理引擎类.我在这用的是ehcache二级轻量级缓存,报错原因可能是导入的jar包版本和使用的hibernate框架核心 ...
 - 漫谈数组去重复方法(亮点是ES6的新API)
			
方法1: 利用遍历的思想来进行. <!DOCTYPE html><html lang="en"><head> <meta charset= ...