题意:给你一张有向图,某人会任意选择起点然后走无穷多步,问是否存在一个点(要求输出)不管他起点在何处怎么走都必经?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. python调用tushare获取A股上市公司基础信息

    接口:stock_company 描述:获取上市公司基础信息 积分:用户需要至少120积分才可以调取,具体请参阅最下方积分获取办法 注:tushare库下载和初始化教程,请查阅我之前的文章 输入参数 ...

  2. 32-Ubuntu-用户权限-03-修改文件权限

    chmod 简介 chmod可以修改用户或组对文件或目录的权限. 命令格式如下: chmod +/-rwx 文件名/目录名 修改文件权限 例:demo.txt 1.增加权限 例:增加demo.txt的 ...

  3. Vue之数据排序加签

    这篇随笔小编给大家带来的是数据排序加签: 所谓数据加签,就是把数据进行加密再传给后端,这样保证数据的秘密性.不容易被修改和获取:排序就是根据公司要求对字段进行排序,有些公司会把字段根据a-z排序,有些 ...

  4. Linux 父子进程实现复制文件内容到另一个文件内

    1. 子进程先拷贝前一半 ,父进程一直阻塞到子进程拷贝完再拷贝后一半 /* 子进程先拷贝前一半文件,父进程先阻塞等待子进程拷贝完前一半内容, * 然后父进程在拷贝,后一半内容 * */ #includ ...

  5. 构建单页Web应用——简单概述

    一.开发框架 ExtJS可以称为第一代单页应用框架的典型,它封装了各种UI组件,用户主要使用JavaScript来完成整个前端部分,甚至包括布局.随着功能逐渐增加,ExtJS的体积也逐渐增大,即使用于 ...

  6. 【学术篇】SDOI2009 SuperGCD

    特别说明: 为了避免以后搬家时的麻烦, 这里的文章继续沿用csdn的风格和分类好了~ Emmmm这个题是一道高精度的模板题啊~ 既然是高精度的裸题, 那我们这些懒人当然是选择:用python啦~ 懒癌 ...

  7. requests中text和content的区别

    # -*- coding: utf-8 -*- __author__ = "nixinxin" import re img_url = "https://f11.baid ...

  8. CentOS 7 編譯qBittorrent Web UI安裝指南

    前言 以下是安装qBittorrent教学. 适用于CentOS7或更新版本 适用于qBittorrent4.1.5或更新版本 安裝須知 qBittorrent基于libtorrent,所以必须先安装 ...

  9. mysql-5.5.17-win64 安装方法

    双击mysql-5.5.17-win64.msi,即开始安装,出现如下界面 2 点击next,出现如下界面,默认为Typical,改选为Custom,单击Next 3 选择MySQL Server后, ...

  10. Web API 接口参考

    Web API 接口参考:https://developer.mozilla.org/zh-CN/docs/Web/API