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的更多相关文章
随机推荐
- /dev/mem可没那么简单
这几天研究了下/dev/mem.发现功能非常奇妙,通过mmap能够将物理地址映射到用户空间的虚拟地址上.在用户空间完毕对设备寄存器的操作,于是上网搜了一些/dev/mem的资料. 网上的说法也非常统一 ...
- HandlerThread解析
HandlerThread是一种具有消息循环的线程.HandlerThread可以接收消息并处理消息,并执行一些耗时操作,这样UI线程就可以把一些耗时的操作命令发送给HandlerThread,由该线 ...
- 基于Vue.js 2.0 + Vuex打造微信项目
一.项目简介 基于Vue + Vuex + Vue-router + Webpack 2.0打造微信界面,实现了微信聊天.搜索.点赞.通讯录(快速导航).个人中心.模拟对话.朋友圈.设置等功能. 二. ...
- [Luogu4916]魔力环[Burnside引理、组合计数、容斥]
题意 题目链接 分析 sπo yyb 代码 #include<bits/stdc++.h> using namespace std; typedef long long LL; #defi ...
- NO NEWS IS GOOD NEWS
从客户那传来一个噩耗:要求每个表单在保存之后,要在页面上弹一个 “ 保存成功 ” 的对话框. 客户代表志得意满地说这样用户体验更好,略带谴责意味地傲娇道,“你们早该想到的”.呵呵…… 可不是嘛,我刚入 ...
- OM1、OM2、OM3和OM4光纤之间的区别
“OM”stand for optical multi-mode,即光模式,是多模光纤表示光纤等级的标准.不同等级传输时的带宽和最大距离不同,从以下几个方面分析它们之间的区别. 一.OM1.OM2. ...
- mysql安装设置mysql字符集utf8及修改密码
MySQL的下载,建议下载MySQL的解压缩版本 MySQL官网下载推荐别下最新版本的原因是因为很多之前用的jar包和工具类不兼容最新版本的 可以下5.多的和六点多的 这样的压缩包解压再配置就行了 安 ...
- oc之封装「可输入值」的UIStepper
2017.07.17 18:10* 字数 66 阅读 644评论 0喜欢 5 oc之封装「可输入值」的UIStepper 最终效果 要求: Platform: iOS7.0+ Language: Ob ...
- windows下使用cmake编译zlib与libpng libjpeg
win7下使用VS2010编译jpeglib 1.下载源代码下载地址:http://www.ijg.org/files/, 选择最新版本的windows版本压缩包,进行下载. jpeg ...
- MySQL 性能调优之索引
原文:http://bbs.landingbj.com/t-0-245452-1.html 对于索引的优化,我们第一需要找到合适的字段,第二创建索引找到合适的顺序,第三要找到合适的比例,第四是要做合适 ...