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 ...
随机推荐
- js登陆验证错误不刷新页面
验证函数返回 false;返回到onclickonclick 其实也是一个函数.. 所以需要加 return;
- 深入浅出原生JS:One
Arguments 对象: 在函数代码中,使用特殊对象 arguments,开发者无需明确指出参数名,就能访问它们. 例如,在函数 sayHi() 中,第一个参数是 message.用 argumen ...
- Neo4j Cypher查询语言详解
Cypher介绍 "Cypher"是一个描述性的图形查询语言,允许不必编写图形结构的遍历代码对图形存储有表现力和效率的查询.Cypher还在继续发展和成熟,这也就意味着有可能会出现 ...
- Elasticsearch索引别名使用
背景 项目中使用的老的索引,由于数据冗余,会想影响性能.因此需要重新建立索引,但是这样必然需要更新服务中的索引名称,然后重新启动服务,可能会对服务的使用者产生一定的影响.因此,调研了Elasticse ...
- mongo之$max
原集合: { _id: 1, highScore: 800, lowScore: 200 } 应用: #意思是:更新_id 等于1 的记录,条件是highScore 950>原纪录的highSc ...
- mac下Python安装路径的说明
Python安装路径的说明 mac在安装Python时, 对不同的安装方式 不同的型号均会安装在不同的文件夹下 安装方式 路径 系统默认(2.7) /System/Library/Frameworks ...
- 利用VUE-CLI脚手架搭建VUE项目
前言 在学习完vue基础语法之后,学着利用vue-cli脚手架搭建一个项目,本篇随笔主要记录搭建的过程,供大家一起学习. 具体内容 搭建vue项目的准备工作 1.安装Nodejs.NPM以及VSCod ...
- Codeigniter Session: Configured save path is not a directory
Codeigniter 3.x Message: Session: Configured save path '' is not a directory, doesn't exist or canno ...
- Impala的安装和使用
通过本地yum源进行安装impala 所有cloudera软件下载地址 http://archive.cloudera.com/cdh5/cdh/5/ http://archive.cloudera. ...
- NX二次开发-UFUN添加工程图投影视图UF_DRAW_add_orthographic_view
NX9+VS2012 #include <uf.h> #include <uf_draw.h> #include <uf_obj.h> #include <u ...