大意:给出一个有向图,问能否在只去掉一条边的情况下破掉所有的环

解析:最直接的是枚举每个边,将其禁用,然后在图中找环,如果可以就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])&&deg[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 拓扑排序的更多相关文章

  1. 【CodeForces】915 D. Almost Acyclic Graph 拓扑排序找环

    [题目]D. Almost Acyclic Graph [题意]给定n个点的有向图(无重边),问能否删除一条边使得全图无环.n<=500,m<=10^5. [算法]拓扑排序 [题解]找到一 ...

  2. CodeForces 915D Almost Acyclic Graph

    Description You are given a directed graph consisting of \(n\) vertices and \(m\) edges (each edge i ...

  3. CodeForces 909E Coprocessor(无脑拓扑排序)

    You are given a program you want to execute as a set of tasks organized in a dependency graph. The d ...

  4. Codeforces 919D:Substring(拓扑排序+DP)

    D. Substring time limit: per test3 seconds memory limit: per test256 megabytes inputstandard: input ...

  5. CodeForces 510C Fox And Names (拓扑排序)

    <题目链接> 题目大意: 给你一些只由小写字母组成的字符串,现在按一定顺序给出这些字符串,问你怎样从重排字典序,使得这些字符串按字典序排序后的顺序如题目所给的顺序相同. 解题分析:本题想到 ...

  6. Codeforces Round #290 (Div. 2) 拓扑排序

    C. Fox And Names time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  7. 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 ...

  8. Codeforces 875C National Property(拓扑排序)

    题目链接  National Property 给定n个单词,字符集为m 现在我们可以把其中某些字母变成大写的.大写字母字典序大于小写字母. 问是否存在一种方案使得给定的n个单词字典序不下降. 首先判 ...

  9. 拓扑排序(Topological Sort)

    Graph 拓扑排序(Topological Sort) 假设一个应用场景:你用 C 编写了一个爬虫工具,其中有很多自定义的库:queue.c.queue.h.stack.c.stack.h.heap ...

随机推荐

  1. (转)Hibernate快速入门

    http://blog.csdn.net/yerenyuan_pku/article/details/64209343 Hibernate框架介绍 什么是Hibernate 我们可以从度娘上摘抄这样有 ...

  2. C#斐波那契数列方法

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  3. Makefile精髓篇【转】

    什么是makefile?或许非常多Winodws的程序猿都不知道这个东西,由于那些Windows的IDE都为你做了这个工作,但我觉得要作一个好的和professional的程序猿,makefile还是 ...

  4. CorelDRAW 中文官网 618 48H秒杀开始,多重好礼即刻开抢!

    618我有诚意,你呢? 不花钱的618,是残缺的618 给自己一个放肆shopping的机遇 活动力度不够大? 继续升级,终极体验 6月17日—6月18日 618疯狂48小时!   同志们,如果你错过 ...

  5. SpringMVC参数绑定、Post乱码解决方法

    从客户端请求key/value数据,经过参数绑定,将key/value数据绑定到controller方法的形参上. springmvc中,接收页面提交的数据是通过方法形参来接收.而不是在control ...

  6. 5.Linux系统的vim与软件包管理

    5.1 Linux系统的vim编辑器 5.1.1 vim编辑器的概述 vim编辑器的简介 1.vim是什么? vim是一个类似vi的文本编辑器,它在vi的基础上增加了很多新特性 vim是vi发展出来的 ...

  7. idea SSM里配置redis

    参考文章 https://blog.csdn.net/qq_17635843/article/details/78522776

  8. Scrapy下载器中间件用法示例

    1.爬虫文件httpbin.py # -*- coding: utf-8 -*- import scrapy class HttpbinSpider(scrapy.Spider): name = 'h ...

  9. 一个电商项目的Web服务化改造7:Dubbo服务的调用,4个项目

    使用dubbo服务的过程,很简单,和之前学习的WebService完全一样,和本地接口调用也基本一致. dubbo和WebService的区别:我认为dubbo就是封装了WebService,然后提供 ...

  10. 【 Codeforces Global Round 1 B】Tape

    [链接] 我是链接,点我呀:) [题意] x轴上有m个连续的点,从1标号到m. 其中有n个点是特殊点. 让你用k段区间将这n个点覆盖. 要求区间的总长度最小. [题解] 一开始假设我们需要n个胶带(即 ...