点击打开链接

F. The Meeting Place Cannot Be Changed

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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

Copy

5 6
1 2
2 3
3 1
3 4
4 5
5 3

output

Copy

3

input

Copy

3 3
1 2
2 3
3 1

output

Copy

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.

题意:
劫匪的初始位置未知,他沿着道路不停的移动,城市的道路是单向的,并且每条道路都连接两个交叉路口,
选择一个交叉路口,如果劫匪继续无限期地开车,他们迟早会到达那个交叉路口。

分析:

其实这个问题可以看做是一个强连通分量里面的所有简单环是否存在至少一个公共点
那么我们假设一个点满足条件就等价于从这个点出发,一定不会滞留在一个死循环中,
而是最后回到了出发点.对所有路径都满足。如果这个点不满足,那么应该再去找哪个点呢?

这个点滞留在死循环中,那么就说明它不属于这个死循环的环,所以接下来的那个点一定是这两个环的交点,

因为该点绝对满足不会进入这两个环的死循环,接下来就是这样找下去,直到遇到交点是已经遇到过的,

那么说明有两个交集点为0的环,则输出-1.由于这样找在有解的情况是最快的,

所以时间一多说明几乎是无解的,则可以限时查询的时间。

#include<bits/stdc++.h>
using namespace std;
const int N=5e5+50;//数据量
int n,m;//节点数、边数
int head[N],to[N],nxt[N],tot;//存储有向图
inline void add(int x,int y)//x为起点,y为终点建边
{to[tot]=y;nxt[tot]=head[x];head[x]=tot++;
}
int vis[N];//标记已经访问过的节点
int in[N],id[N];
bool dt[N],k;
const bool cmp(int a,int b)//间接排序函数
{return in[a]<in[b];//按入度排序
}
//计算搜索时间
inline double FIND_TIME()
{return clock()/(double)CLOCKS_PER_SEC;}
void dfs(int x)
{vis[x]=1;//标记节点已经访问过for(int i=head[x];~i;i=nxt[i]){if(!vis[to[i]])//下一个节点没有访问过dfs(to[i]);if(vis[to[i]]==1)//下一个节点访问过k=1;if(k)   return ;}vis[x]=2;//DFS回溯
}
inline bool check(int x)//检查每一个点
{memset(vis,0,sizeof(vis));vis[x]=2;k=0;for(int i=1;i<=n;i++)if(!vis[i]){dfs(i);if(k){for(int i=1;i<=n;i++)if(vis[i]!=1)dt[i]=1;return false;}}return true;
}int main()
{memset(head,-1,sizeof(head));scanf("%d%d",&n,&m);for(int i=1;i<=m;i++){int u,v;scanf("%d%d",&u,&v);add(u,v);//建边++in[u];//更新度}for(int i=1;i<=n;i++)id[i]=i;//节点编号sort(id+1,id+1+n,cmp);//排序for(int i=1;i<=n;i++){if(FIND_TIME()>0.9) break;//时间限制为1sif(!dt[id[i]]&&check(id[i]))//此点合法{printf("%d\n",id[i]);//输出路口的编号return 0;}
}puts("-1");//不存在这样的点return 0;
}

用邻接表来存储有向图

#include<bits/stdc++.h>
using namespace std;
const int mx = 1e5 + 10;
int n,m,in[mx],ty,type[mx],d,mark;
vector <int> vec[mx];
int dfn[mx],id[mx],size,is,sta[mx];
int vis[mx],flag;
bool vic[mx];
void tarjan(int x)
{dfn[x] = id[x] = ++is;vis[x] = 1;sta[++size] = x;for(int i=0;i<vec[x].size();i++){int son = vec[x][i];if(!dfn[son]){tarjan(son);id[x] = min(id[x],id[son]);}else if(vis[son]) id[x] = min(id[x],dfn[son]);}if(dfn[x]==id[x]){ty++;if(sta[size]!=x) d++,mark = ty;while(sta[size]!=x){type[sta[size]] = ty;vis[sta[size]] = 0;size--;}type[x] = ty,size--,vis[x] = 0;}
}
void dfs(int x,int num)
{if(flag) return ;for(int i=0;i<vec[x].size();i++){if(flag) return;int son = vec[x][i];if(vis[son]==num){if(vic[son]) flag = -1;else flag = son,vic[son] = 1;}if(vis[son]<num) vis[son] = num,dfs(son,num);}vis[x] = num + 1;
}
double TIME(){//获得单位运行时间return 1.0*clock()/CLOCKS_PER_SEC;
}
int check(int x,int num)
{while(TIME()<0.5){vis[x] = num + 1;dfs(x,num);if(!flag) return x;if(flag==-1) return -1;x = flag,num += 2,flag = 0;}return -1;
}
int main()
{scanf("%d%d",&n,&m);int a,b,c = 0,ans;for(int i=1;i<=m;i++){scanf("%d%d",&a,&b);vec[a].push_back(b);}for(int i=1;i<=n;i++)if(!dfn[i]) tarjan(i);if(d>1) puts("-1");else{for(int i=1;i<=n;i++)if(type[i]==mark){printf("%d\n",check(i,1));break;}}return 0;
}

CodeForces 982F. The Meeting Place Cannot Be Changed相关推荐

  1. Cf Round #403 B. The Meeting Place Cannot Be Changed(二分答案)

    The Meeting Place Cannot Be Changed 我发现我最近越来越zz了,md 连调程序都不会了,首先要有想法,之后输出如果和期望的不一样就从输入开始一步一步地调啊,tmd现在 ...

  2. 782B The Meeting Place Cannot Be Changed(二分)

    链接:http://codeforces.com/problemset/problem/782/B 题意: N个点,需要找到一个点使得每个点到这个点耗时最小,每个点都同时开始,且都拥有自己的速度 题解 ...

  3. 【CodeForces - 144B 】Meeting (暴力枚举,水题,计算几何)

    题干: The Super Duper Secret Meeting of the Super Duper Secret Military Squad takes place in a Super D ...

  4. Codeforces Round #403 (Div. 1, based on Technocup 2017 Finals)

    Div1单场我从来就没上过分,这场又剧毒,半天才打出B,C挂了好几次最后还FST了,回紫了. AC:AB Rank:340 Rating:2204-71->2133 Div2.B.The Mee ...

  5. CodeForces Round #403 (Div.2) A-F

    精神不佳,选择了在场外同步划水 没想到实际做起来手感还好,早知道就报名了-- 该打 未完待续233 A. Andryusha and Socks 模拟,模拟大法好.注意每次是先判断完能不能收进柜子,再 ...

  6. python60行绘图程序_天底下最简单的QT画图板,就一个类,60行代码

    有吧友需要PDF的下载站点,好吧,我这边汇总一下 [经验]谈谈怎么找自己想要的资源吧~ http://www.cnblogs.com/dunitian/p/4715482.html PDF Free ...

  7. 【无标题】PMP强化练习正确题一

    单选题 (每题1分,共133道题) 1. [单选] 项目经理收到项目可交付成果的验收,并举行了经验教训会议,若要结束该项目,项目经理下一步应该做什么? A project manager receiv ...

  8. 【考前冲刺整理】20220812

    20. 项目经理正在谋求与关键相关方一起验证某项目是否符合公司战略,但其中的几位相关方却一再拒绝开会请求.项目经理接下来应该如何进行该验证工作? The project manager is seek ...

  9. PMP考前冲刺题2022(正题)含解析

    多选题 (每题1分,共151道题) 1. [多选] 项目进度落后了,项目经理观察到,项目团队似乎把大部分时间花在了写文件上,而不是执行项目任务.项目经理应该做什么来帮助确保该团队专注于项目任务? Th ...

最新文章

  1. 嵌入式的我们为什么要学ROS
  2. 保存ip地址和计算机名称,批量设置IP地址和计算机名
  3. 顾险峰: 庞加莱猜测的证明和应用
  4. [VB] Option Explicit
  5. ubuntu安装VMware Tools
  6. python基本数据类型包括哪些_python入门3——基本数据类型
  7. c语言奇葩错误,6个奇葩的(hello,world)C语言版(转)
  8. REVERSE-PRACTICE-CTFSHOW-5
  9. 关于controller的总结 2021-04-22
  10. Python 面向对象 —— 特殊函数(setattr、getattr、hasattr)
  11. python3如何安装numpy_python3怎么安装numpy
  12. GNS3使用Docker
  13. Onvif客户端与服务器通信时鉴权的自实现
  14. 在微型计算机中,ram的特点是___.,2017计算机基础模拟试题「附答案」
  15. Android自定义相机实现定时拍照
  16. jlink修复固件教程
  17. Robot Toolbox (一):Puma机器人仿真
  18. 2022年各大高校最新博士薪资汇总~
  19. 计算机毕业论文有必要建模吗,本科生真有必要写毕业论文吗
  20. IP-GUARD如何禁止除了银行UKEY设备外的其他USB设备?

热门文章

  1. Python中安装bs4后,pycharm依然报错ModuleNotFoundError: No module named 'bs4'
  2. SpringCloud基本模块分配搭建以及负载均衡
  3. 0728pm 控制器
  4. String定义字符串,实际操作
  5. HADOOP都升级到2.5啦~~~
  6. Implementation:Bellman-ford
  7. 写程序过程中写程序的注意事项
  8. C# 十六进制字符串与数值类型之间转换
  9. java generate()_Java IntStream generate()用法及代码示例
  10. 把执行结果转成json对象报错_JSONObject获取值后为一个对象,将对象转为JSONObject时报错...