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 ...
随机推荐
- JAVA集合--Collection接口
本文首发于cartoon的博客 转载请注明出处:https://cartoonyu.github.io/cartoon-blog 在概述里面也说过:Collection是jav ...
- java并发之同步辅助类CountDownLatch
CountDownLatch 含义: CountDownLatch可以理解为一个计数器在初始化时设置初始值,当一个线程需要等待某些操作先完成时,需要调用await()方法.这个方法让线程进入休眠状态直 ...
- 30个优秀的CSS技术和实例 By 彬Go 2008-12-04
在这里可发现很多与众不同的技术,比如:图片集.阴影效果.可扩展按钮.菜单等…这些实例都是使用纯CSS和HTML实现的.单击每个实例的标题可以被转向到该技术实例的相关教程或说明页面(英文),单击每个实例 ...
- 利用纯css写三角形,弧度箭头,吃豆人,气泡。放大镜,标签的源码
1. 向上三角形
- 网址URL知识
URL由三部分组成:资源类型.存放资源的主机域名.资源文件名. URL的一般语法格式为: (带方括号[]的为可选项): protocol :// hostname[:port] / path / [; ...
- Excel如何快速渲染百万级别的数据
Excel主要经历1.查询2.渲染的方式 关于查询: 不同技术水平的人有不同的解决方案,目前我采用的是 1:多线程查询 2:一个异步后台线程每次查询100条便渲染,采用的“懒加载方式”,这样可以做到实 ...
- Android中onTouch方法的执行过程以及和onClick执行发生冲突的解决办法
$*********************************************************************************************$ 博主推荐 ...
- iOS ARC使用总结
在iOS ARC模式下,编译器会自动插入release 有些时候程序出现 message sent to deallocated object的时候,你不知道什么原因. 一种原因是因为你在ARC下使用 ...
- 检测API函数的InlineHook
BOOL GetProcHookStatus(LPCSTR lpModuleName, LPCSTR lpProcName) { HMODULE hModule = GetModuleHandleA( ...
- 最常见VC++编译错误信息集合
1.fatal error C1010: unexpected end of file while looking for precompiled header directive. 寻找预编译头文件 ...