The Meeting Place Cannot Be Changed

CodeForces - 982F

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

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

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的更多相关文章

随机推荐

  1. SimpleDialogBox

    import 'package:flutter/material.dart';import 'dart:ui';import 'dart:async';enum Option{A,B,C}void m ...

  2. CustomScrollView + slivers + SliverAppBar

    import 'package:flutter/material.dart'; void main()=>runApp(MyApp()); class MyApp extends Statele ...

  3. 调试器GDB的基本使用方法

    GDB调试的三种方式: 1. 目标板直接使用GDB进行调试. 2. 目标板使用gdbserver,主机使用xxx-linux-gdb作为客户端. 3. 目标板使用ulimit -c unlimited ...

  4. linux使用.net core 创建简单的MVC

    1 创建MVC 2.修改默认绑定的端口方法 ,在Program.cs 的Build之前加入 .UseUrls("http://*:8888")

  5. C语言中数组变量和指针变量

    指针变量为什么需要类型? 数组变量和指针变量在使用sizeof时不同,sizeof(数组变量)是数组长度,sizeof(指针变量)是存储int的字节长度4或者8(64bit). 数组变量在参数传递中, ...

  6. Java关键字(六)——super

    在 Java关键字(五)——this 中我们说 this 关键字是表示当前对象的引用.而 Java 中的 super 关键字则是表示 父类对象的引用. 我们分析这句话“父类对象的引用”,那说明我们使用 ...

  7. ORM简介 单表添加修改删除表记录

    ---------------------------------------------------------------目标既定,在学习和实践过程中无论遇到什么困难.曲折都不灰心丧气,不轻易改变 ...

  8. tortoisegit密钥与git密钥配置

    在客户端生成密钥并将公钥上传到服务器可以避免每次连接git服务器都要登录的尴尬. 但git的私钥是不能直接用在tortoisegit上的,需要用tortoisegit的puttygen转换一下,详细过 ...

  9. Java对象的创建、内存布局和访问定位

    在Java运行时数据区中,我们知道了虚拟机内存的概况,本文介绍虚拟机内存中的数据的其它细节,如对象如何创建.如何布局以及如何访问. 基于实用的原则,这里以HotSpot虚拟机和常用的内存区域Java堆 ...

  10. 多线程系列之九:Worker Thread模式

    一,Worker Thread模式 也叫ThreadPool(线程池模式) 二,示例程序 情景:一个工作车间有多个工人处理请求,客户可以向车间添加请求.请求类:Request定义了请求的信息和处理该请 ...