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

解析:最直接的是枚举每个边,将其禁用,然后在图中找环,如果可以就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. html——ico

    下载: 网址+/favicon.ico 引用: 1.<link href="favicon.ico" rel="icon" /> 2.<lin ...

  2. [Windows Server 2008] 手工创建安全网站

    ★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频.★ 本节我们将带领大家:手工创建安全站 ...

  3. css图片高清适配

    同一张图片,在普通屏显示正常,但高清屏出现模糊.原因是原来一个像素的点分成的四个像素的点进行了显示. 解决方案:在高清屏中把图片变成二倍图,前提是二倍的高清图已经存在. .icon{ backgrou ...

  4. Python之操作Excel、异常处理、网络编程

    知识补充: 1.falsk模块中一些方法总结 import flask from flask import request,jsonify server = flask.Flask(__name__) ...

  5. common_functions.h:64:24: error: token ""__CUDACC_VER__ is no longer supported.

    问题在复现工程https://github.com/google/hdrnet时出现. 现象: 解决: TensorFlow版本问题,升级到版本1.10.0之后,问题解决.

  6. C++ 中 字符数组 和 指针 区别

    char str1[] = "abc"; char str2[] = "abc"; const char str3[] = "abc"; c ...

  7. rsync全网备份

    rsync备份企业方案 企业有Linux服务器又有windows服务器,备份用rsync(服务端),Linux(客户端),Windows(客户端,cwrsync,旧版本有免费版)打包压缩数据往服务器上 ...

  8. python json结构

    =====================================================json==============================import reques ...

  9. Dijkstra算法求最短路径

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include <limits.h&g ...

  10. 用 console.time()和 console.timeEnd() 测试你的 javascript 代码执行效率

    无意中学习到了一种测试 javascript 代码执行效率的一种方法,就记下来便于以后使用,用到了console对象中的  time  和  timeEnd  方法 . console.time('m ...