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的更多相关文章
随机推荐
- CentOS下安装PHP
今天終於進行了第三次的在linux下的php安裝,在安裝之前我進行了網站訪問測試. 第一步,先查看mysql.apache這兩項系統服務是否已經自動啟動了,chkconfig –list mysql. ...
- leetcode 4:Median of Two Sorted Arrays
public double FindMedianSortedArrays(int[] nums1, int[] nums2) { int t=nums1.Length+nums2.Length; in ...
- Vue-computed的set和get
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 简单的自定义ViewGroup
自定义ViewGroup需要重写onMeasure, onLayout等方法.下面是一个实例,4个View分别显示在四个角. public class MyGroup extends ViewGrou ...
- List对象去重碎碎念之神叨叨
前言 List集合操作去除重复数据的这种情况经常会碰到,博客园里面也有很多大神们做过,在这里主要是借鉴然后自己整理了一下,主要是为了方便自己,以后再次碰到这种去重问题,直接打开自己的链接拿起键盘就是干 ...
- 小L的项链切割 (回文串)
题目描述 小T送给了小L了一串项链.为了方便,我们把项链上形态不同钻石用不同的字母表示.这样小L的项链就变成了一个字符串.小L忽然想把这串项链优美地切割一下,她想把它切割成尽量少的回文项链,啊也就是回 ...
- poj3984 广度搜索BFS
迷宫问题 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1 ...
- B. School Marks(典型贪心)
链接 [https://codeforces.com/contest/540/problem/B] 题意 某个人有n门成绩,k门已知,剩下的他可以个瞎改,但有个要求,最后分数和不超过x,且每门成绩不超 ...
- Python2和Python3中urllib库中urlencode的使用注意事项
前言 在Python中,我们通常使用urllib中的urlencode方法将字典编码,用于提交数据给url等操作,但是在Python2和Python3中urllib模块中所提供的urlencode的包 ...
- Git更新代码到本地
一段时间没用git,发现一些东西记不住了,这里记一点常用的命令. 正规流程 git status(查看本地分支文件信息,确保更新时不产生冲突) 若出现冲突,会有提示的 git checkout – [ ...