CF982F The Meeting Place Cannot Be Changed
题意:给你一张有向图,某人会任意选择起点然后走无穷多步,问是否存在一个点(要求输出)不管他起点在何处怎么走都必经?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的更多相关文章
- 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 ...
- 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 ...
- Cf Round #403 B. The Meeting Place Cannot Be Changed(二分答案)
The Meeting Place Cannot Be Changed 我发现我最近越来越zz了,md 连调程序都不会了,首先要有想法,之后输出如果和期望的不一样就从输入开始一步一步地调啊,tmd现在 ...
- 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 ...
- AC日记——The Meeting Place Cannot Be Changed codeforces 780b
780B - The Meeting Place Cannot Be Changed 思路: 二分答案: 代码: #include <cstdio> #include <cstrin ...
- Codeforces 782B The Meeting Place Cannot Be Changed(二分答案)
题目链接 The Meeting Place Cannot Be Changed 二分答案即可. check的时候先算出每个点可到达的范围的区间,然后求并集.判断一下是否满足l <= r就好了. ...
- codeforces 782B The Meeting Place Cannot Be Changed+hdu 4355+hdu 2438 (三分)
B. The Meeting Place Cannot Be Change ...
- 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 ...
- 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 ...
随机推荐
- VIM编辑器进阶配置
vim自定义设置 可以选择需要的功能添加至 ~/.vimrc 打开注释使之生效. " 让 vim 关闭所有扩展的功能,尽量模拟 vi 的行为. set nocompatible ...
- [已解决]报错:报错AttributeError: 'int' object has no attribute 'upper'
原因:openpyxl版本低,需升级 pip install --upgrade openpyxl
- 前端 JavaScript BOM & DOM
内容目录: 1. BOM 2. DOM BOM(Browser Object Model)是指浏览器对象模型,它使 JavaScript 有能力与浏览器进行"对话". DOM (D ...
- NFS 服务器的配置
1. 安装 NFS 服务器 [root@localhost btools]#rpm -q nfs-utils 如果没有安装,从对应 Linux 操作系统版本的安装光盘上找到 nfs-utils 的安装 ...
- C++11 auto 与 右值
auto: auto T = xxx; // 产生一个变量,自动推导变量类型. 存在变量拷贝的消耗.auto& T = xxx; // 产生一个变量的引用,自动推导变量类型.减少拷贝的消耗. ...
- leetcode-数组的相对排序
Python解法: def relativeSortArray(arr1, arr2): arr = [0 for _ in range(110)] new = [] for a in range(l ...
- 解析Spring第四天(Spring中的事物、Spring框架来管理模板类)
JDBC模板技术: Spring框架中提供了很多持久层的模板类来简化编程,使用模板类编写程序会变的简单 template 模板 都是Spring框架提供XxxTemplate 提供了JDBC模板,Sp ...
- CGLIB 详解
依赖 <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> ...
- ElasticSearch再学习
ElasticSearch参数详解 本次使用的windows的版本,如若Linux移步:https://www.cnblogs.com/msi-chen/p/10335794.html 配置文件参数 ...
- 揭秘!2周实现上云上市,阿里云SaaS上云工具包如何打造新云梯?
提到“上云”,很多人会理解成上IaaS,比如买一些计算.存储和网络云产品,把自己的应用系统部署上去.这的确是通常意义的上云.但对SaaS而言,需要从产品.商业.服务,三个维度考虑SaaS伙伴和客户的痛 ...