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的更多相关文章
随机推荐
- Java IO(三)——字节流
一.流类 Java的流式输入/输出是建立在四个抽象类的基础上的:InputStream.OutputStream.Reader.Writer.它们用来创建具体的流式子类.尽管程序通过具体子类执行输入/ ...
- python:利用logbook模块管理日志
日志管理作为软件项目的通用部分,无论是开发还是自动化测试过程中,都显得尤为重要. 最初是打算利用python的logging模块来管理日志的,后来看了些github及其他人的自动化框架设计,做了个比对 ...
- shell笔记-常用
shell提取文件名: http://blog.csdn.net/u011544778/article/details/50773053 一.使用${} 1.${var##*/}该命令的作用是去掉变量 ...
- devops工具-Ansible进阶playbook&roles
一.playbook介绍 playbook 是 Ansible 管理配置.部署应用的核心所在,一个playbook由有多“play组成”,而一个play实际就是一个task,每个task是由多 ...
- GitHub Or Subversion
上一次转载了介绍GitHub的博文点我,我想对于初学GitHub的同学们还是有不清楚的地方,毕竟有些概念的理解比较费力.我觉得作为一个对于配置库技术已经有一定基础的同学们,要学习GitHub,最快以及 ...
- Mike and distribution CodeForces - 798D (贪心+思维)
题目链接 TAG: 这是我近期做过最棒的一道贪心思维题,不容易想到,想到就出乎意料. 题意:给定两个含有N个正整数的数组a和b,让你输出一个数字k ,要求k不大于n/2+1,并且输出k个整数,范围为1 ...
- Centos6.x升级内核方法支持Docker
Centos6升级内核方法_百度经验https://jingyan.baidu.com/article/7e4409531bda252fc1e2ef4c.html
- MYSQL mydumper & myloader
第三方逻辑备份工具myduper和myloader | xiaoyu的数据库小窝-技术交流http://www.dbaxiaoyu.com/archives/1643 myloader原理0 - ze ...
- PyCharm3.0默认快捷键
PyCharm3.0默认快捷键(翻译的) 1.编辑(Editing) Ctrl + Space 基本的代码完成(类.方法.属性) Ctrl + Alt + Space 快速导入任意类 Ctrl + S ...
- h5小功能_classList和自定义属性data
###1.classList返回一个对象集 通过obj.classList.add()或obj.classList.remove()可操作对象的class属性值 classList.toggle( ) ...