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

解析:最直接的是枚举每个边,将其禁用,然后在图中找环,如果可以就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. 使用jquery animate实现锚点慢慢平滑滚动效果

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. Angular——数据绑定

    基本介绍 angularjs可以实现数据的双向绑定:(1)视图到模型的数据绑定,(2)模型到数据的绑定 基本使用 1.ng-model可以实现视图到模型的数据传输 2.{{name}}可以实现模型到视 ...

  3. 【sqli-labs】 less50 GET -Error based -Order By Clause -numeric -Stacked injection(GET型基于错误的整型Order By从句堆叠注入)

    报错没有关闭,直接可以用UpdateXml函数 http://192.168.136.128/sqli-labs-master/Less-50/?sort=1 and UpdateXml(1,conc ...

  4. Centos安装smokeping教程

    Centos安装smokeping教程 一 .安装基本依赖包 ntpdate time.windows.com #64bit rpm -Uhv http://apt.sw.be/redhat/el6/ ...

  5. js 时间 Fri Dec 12 2014 08:00:00 GMT+0800

    第一种var d = new Date('Fri Dec 12 2014 08:00:00 GMT+0800'); ) + '-' + d.getDate() + ' ' + d.getHours() ...

  6. day03-执行python方式、变量及数据类型简介

    目录 执行Python程序的两种方式 1. 第一种:交互式 2. 第二种:命令式 3. Python执行程序的三个阶段 变量 变量 什么是变量 Python中的变量 变量名的命名规范 内存管理 定义变 ...

  7. 关于MySQL中自增的理解和设置

    show create table t10;--查看表的创建结果 show create table t10\G;--竖列查看 --设置自增为20 ); insert into t2(name) va ...

  8. 第四节:Web爬虫之pyquery解析库

    PyQuery库也是一个非常强大又灵活的网页解析库,如果你有前端开发经验的,都应该接触过jQuery,那么PyQuery就是你非常绝佳的选择,PyQuery 是 Python 仿照 jQuery 的严 ...

  9. 《AlwaysRun!》第八次团队作业:Alpha冲刺 第二天

    项目 内容 这个作业属于哪个课程  老师链接 这个作业的要求在哪里  实验十二 团队作业8:软件测试与Alpha冲刺 团队名称  Always Run! 作业学习目标 (1)掌握软件测试基础技术. ( ...

  10. vue 使用echarts

    import echarts from 'echarts'   <div id="kocGrow" style="width: 600px;height: 300p ...