题意:给你一张有向图,某人会任意选择起点然后走无穷多步,问是否存在一个点(要求输出)不管他起点在何处怎么走都必经?n<=100005,m<=500005.

标程:

 #include<bits/stdc++.h>
using namespace std;
const int N=;
int cnt,vis[N],n,m,u,v,a[N],tried[N],head[N];
struct node{int to,next;}num[N*];
void add(int x,int y)
{num[++cnt].to=y;num[cnt].next=head[x];head[x]=cnt;}
int dfs(int x,int ban)
{
if (x==ban) return ;
vis[x]=;
for (int i=head[x];i;i=num[i].next)
if (!vis[num[i].to])
{if (dfs(num[i].to,ban)) return ;}
else if (vis[num[i].to]==){
for (int j=;j<=n;j++)
if (vis[j]!=) tried[j]=;
return ;
}
vis[x]=;
return ;
}
bool check(int ban)
{
for (int i=;i<=n;i++) vis[i]=;
for (int i=;i<=n;i++)
if (!vis[i])
if (dfs(i,ban)) return ;
return ;
}
int main()
{
srand(time(NULL));
scanf("%d%d",&n,&m);
for (int i=;i<=m;i++) scanf("%d%d",&u,&v),add(u,v);//注意单向连边
for (int i=;i<=n;i++) a[i]=i;
random_shuffle(a+,a+n+);
for (int i=;i<=n;i++)
{
if ((double)clock()/CLOCKS_PER_SEC>0.95) break;
if (!tried[a[i]])
if (check(a[i])) return printf("%d\n",a[i]),;
}
puts("-1");
return ;
}

易错点:1.注意单向连边。

2.random_shuffle可以有效防卡,要srand。

题解: dfs找环

枚举要选择的点,dfs找是否存在一条带环的不经过该点的路径,如果不存在,那么该点为答案;如果存在,那么该路径之外的点一定不可能是答案,标记掉不找。加上卡时就可以过掉。

CF982F The Meeting Place Cannot Be Changed的更多相关文章

  1. codeforces 782B The Meeting Place Cannot Be Changed (三分)

    The Meeting Place Cannot Be Changed Problem Description The main road in Bytecity is a straight line ...

  2. code force 403B.B. The Meeting Place Cannot Be Changed

    B. The Meeting Place Cannot Be Changed time limit per test 5 seconds memory limit per test 256 megab ...

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

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

  4. Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) B. The Meeting Place Cannot Be Changed

    地址:http://codeforces.com/contest/782/problem/B 题目: B. The Meeting Place Cannot Be Changed time limit ...

  5. AC日记——The Meeting Place Cannot Be Changed codeforces 780b

    780B - The Meeting Place Cannot Be Changed 思路: 二分答案: 代码: #include <cstdio> #include <cstrin ...

  6. Codeforces 782B The Meeting Place Cannot Be Changed(二分答案)

    题目链接 The Meeting Place Cannot Be Changed 二分答案即可. check的时候先算出每个点可到达的范围的区间,然后求并集.判断一下是否满足l <= r就好了. ...

  7. codeforces 782B The Meeting Place Cannot Be Changed+hdu 4355+hdu 2438 (三分)

                                                                   B. The Meeting Place Cannot Be Change ...

  8. CodeForce-782B The Meeting Place Cannot Be Changed(高精度二分)

    https://vjudge.net/problem/CodeForces-782B B. The Meeting Place Cannot Be Changed time limit per tes ...

  9. B. The Meeting Place Cannot Be Changed

    B. The Meeting Place Cannot Be Changed time limit per test 5 seconds memory limit per test 256 megab ...

随机推荐

  1. SpringCloud广告系统随想

    广告系统需要两个方面:一个广告投放系统,一个是广告检索系统. 两个系统针对的人群不同: 1.广告投放系统针对的是广告主,他们出资进行广告的投放,将广告的一些数据投放到系统中. 2.广告检索系统针对的是 ...

  2. GYM 101933E 状态压缩 + 记忆化搜索

    题意:我方有n个士兵,敌方有m个,每方士兵都有一个血量,现在有k轮无差别炮火打击,每次都会从存活的士兵中随机选一人,这名士兵的HP就-1,问对方被团灭的概率有多大? 思路:因为n和m的范围很小,我们可 ...

  3. [转载]A星寻路算法介绍

    转载自:http://www.raywenderlich.com/zh-hans/21503/a%E6%98%9F%E5%AF%BB%E8%B7%AF%E7%AE%97%E6%B3%95%E4%BB% ...

  4. canvas 压缩图片上传

    问题:前端开发过程中难免会将数据提交到后台,但若是提交的数据过大,特别上传图片这类需求,如果不对上传的图片进行压缩处理,就难免会出现请求时间过长的情况,对于用户体验肯定就不是太友好,那么这时候该如何将 ...

  5. MapReduce分区数据倾斜

    什么是数据倾斜? 数据不可避免的出现离群值,并导致数据倾斜,数据倾斜会显著的拖慢MR的执行速度 常见数据倾斜有以下几类 1.数据频率倾斜   某一个区域的数据量要远远大于其他区域 2.数据大小倾斜  ...

  6. Delphi 取整函数round、trunc、ceil和floor

    Delphi 取整函数round.trunc.ceil和floor 1.Round(四舍六入五留双)功能说明:对一个实数进行四舍五入.(按照银行家算法)例:var i, j: Integer;begi ...

  7. NX二次开发-UFUN判断文件是否存在UF_CFI_ask_file_exist

    #include <uf.h> #include <uf_ui.h> #include <uf_cfi.h> UF_initialize(); //判断文件是否存在 ...

  8. NX二次开发-UFUN获取一个图层类别的tag UF_LAYER_ask_category_tag

    NX11+VS2013 #include <uf.h> #include <uf_ui.h> #include <uf_layer.h> UF_initialize ...

  9. <Java编程思想>读书笔记(1)-对象导论、一切都是对象

    1.面向对象编程:OOP (Object-oriented Programming) 2.Alan Kay 总结的面向对象语言5个基本特性: 1) 万物皆为对象 2) 程序是对象的集合,他们通过发送消 ...

  10. 其它课程中的python---4、Matplotlib最最最最简单使用

    其它课程中的python---4.Matplotlib最最最最简单使用 一.总结 一句话总结: 慢慢来吧,不着急,心态平和和沉稳:每次和世界交互,你就能感受到无比的自信 1.如何区别python2和p ...