codeforces 915D Almost Acyclic Graph 拓扑排序
大意:给出一个有向图,问能否在只去掉一条边的情况下破掉所有的环
解析:最直接的是枚举每个边,将其禁用,然后在图中找环,如果可以就YES,都不行就NO
复杂度O(N*M)看起来不超时
但是实现了以后发现即使优化到不清空vis数组(时间戳标记),也仍然超时。
因为O(N*M)已经很接近时间复杂度上界,常数稍大就GG。
不过可以脑补一下取巧算法:在不超时的前提下,随机取K个边进行检验~~~。不过数据多了就非常容易GG。理论上还是可行的。
正解:从枚举边变为枚举点,删掉到达一个点的某条边可以认为是该点入度 -1 ,然后做拓扑排序。
如果所有点都能访问到,说明没有环,YES。
如果有的点不能访问到,则说明图中存在环,删到达该点的某条边不可行。
入度 -1 的正确性:
可以认为是暂时不具体考虑删掉的是那条边,到了这个点的入边只剩一个没有访问的时候,该点的入度为0,可以开始以该点为起点dfs(bfs也行),如果该点正好在某个环内,就直接破掉(遍历)了这个环。、
/*
Welcome Hacking
Wish You High Rating
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<string>
using namespace std;
int read(){
int xx=,ff=;char ch=getchar();
while(ch>''||ch<''){if(ch=='-')ff=-;ch=getchar();}
while(ch>=''&&ch<=''){xx=(xx<<)+(xx<<)+ch-'';ch=getchar();}
return xx*ff;
}
const int maxn=,maxm=;
int N,M,lin[maxn],len,in_[maxn],deg[maxn];
struct edge{
int y,next;
}e[maxm];
inline void insert(int xx,int yy){
e[++len].next=lin[xx];
lin[xx]=len;
e[len].y=yy;
in_[yy]++;
}
bool vis[maxn];
void dfs(int x){
vis[x]=;
for(int i=lin[x];i;i=e[i].next){
deg[e[i].y]--;
if(!vis[e[i].y]){
if(deg[e[i].y]<=)
dfs(e[i].y);
}
}
}
int main(){
//freopen("in.txt","r",stdin);
N=read(),M=read();
for(int i=;i<=M;i++){
int t1=read(),t2=read();
insert(t1,t2);
}
for(int i=;i<=N;i++){
for(int j=;j<=N;j++)
deg[j]=in_[j];
memset(vis,,sizeof(vis));
deg[i]--;
for(int j=;j<=N;j++)
if((!vis[j])&°[j]<=)
dfs(j);
bool OK=;
for(int j=;j<=N;j++)
if(!vis[j]){
OK=;
break;
}
if(OK){
printf("YES\n");
return ;
}
}
printf("NO\n");
return ;
}
codeforces 915D Almost Acyclic Graph 拓扑排序的更多相关文章
- 【CodeForces】915 D. Almost Acyclic Graph 拓扑排序找环
[题目]D. Almost Acyclic Graph [题意]给定n个点的有向图(无重边),问能否删除一条边使得全图无环.n<=500,m<=10^5. [算法]拓扑排序 [题解]找到一 ...
- CodeForces 915D Almost Acyclic Graph
Description You are given a directed graph consisting of \(n\) vertices and \(m\) edges (each edge i ...
- CodeForces 909E Coprocessor(无脑拓扑排序)
You are given a program you want to execute as a set of tasks organized in a dependency graph. The d ...
- Codeforces 919D:Substring(拓扑排序+DP)
D. Substring time limit: per test3 seconds memory limit: per test256 megabytes inputstandard: input ...
- CodeForces 510C Fox And Names (拓扑排序)
<题目链接> 题目大意: 给你一些只由小写字母组成的字符串,现在按一定顺序给出这些字符串,问你怎样从重排字典序,使得这些字符串按字典序排序后的顺序如题目所给的顺序相同. 解题分析:本题想到 ...
- Codeforces Round #290 (Div. 2) 拓扑排序
C. Fox And Names time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- Codeforces Beta Round #29 (Div. 2, Codeforces format) C. Mail Stamps 拓扑排序
C. Mail Stamps One day Bob got a letter in an envelope. Bob knows that when Berland's post offic ...
- Codeforces 875C National Property(拓扑排序)
题目链接 National Property 给定n个单词,字符集为m 现在我们可以把其中某些字母变成大写的.大写字母字典序大于小写字母. 问是否存在一种方案使得给定的n个单词字典序不下降. 首先判 ...
- 拓扑排序(Topological Sort)
Graph 拓扑排序(Topological Sort) 假设一个应用场景:你用 C 编写了一个爬虫工具,其中有很多自定义的库:queue.c.queue.h.stack.c.stack.h.heap ...
随机推荐
- JS——scroll
scrollWidth:父div宽度小于子div宽度,父div scrollWidth宽度为子div的宽度,大于则为本身的宽度width+padding scrollHeight:父div高度小于子d ...
- [Windows Server 2003] 安装网站伪静态
★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频.★ 本节我们将带领大家:安装IIS伪静 ...
- Spring学习笔记_day01_ioc
本文为博主辛苦总结,希望自己以后返回来看的时候理解更深刻,也希望可以起到帮助初学者的作用. 转载请注明 出自 : luogg的博客园 谢谢配合! Spring_day01 spring是一站式的框架, ...
- 小程序 之picker-view省市县
代码地址:https://github.com/yangsphp/area-picker
- FTP服务器访问主动模式、被动模式
在公司里面,经常需要访问外网FTP取资料等情况.但是有时用windows界面访问经常遇到各种问题. 下面介绍两种客户端是如何访问ftp服务器. 首先我们需要说明:防火墙,是阻拦外界与内部的通讯的一道关 ...
- pip/pip3国内源
Error 在使用pip3安装PySide2时出现ReadTimeoutError. $ pip3 install PySide2 Solution 使用国内源 例如: $ pip3 install ...
- Python-基本语法元素
#TempConvert.py TempStr = input("请输入带有符号的温度值: ") if TempStr[-1] in ['F', 'f']: C = (eval(T ...
- Huawei-R&S-网络工程师实验笔记20190530-FTP上传下载、STelnet登录、SFTP登录
>Huawei-R&S-网络工程师实验笔记20190530-FTP上传下载.STelnet登录.SFTP登录 >>实验开始,参考<Huawei-R&S-网络工程 ...
- springCloud学习-消息总线(Spring Cloud Bus)
1.简介 Spring Cloud Bus 将分布式的节点用轻量的消息代理连接起来.它可以用于广播配置文件的更改或者服务之间的通讯,也可以用于监控.本文要讲述的是用Spring Cloud Bus实现 ...
- [bzoj1592][Usaco09Feb]Making the Grade 路面修整_动态规划
Making the Grade 路面修整 bzoj-1592 题目大意:给你n段路,每段路有一个高度h[i],将h[i]修改成h[i]$\pm\delta$的代价为$\delta$,求将这n段路修成 ...