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

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

    转:http://blog.csdn.net/kevingao/article/details/8125683 activation~与javaMail有关的jar包,使用javaMail时应与mai ...

  2. dom4j.jar 的调试方法

    1.将jar包的路径写在 classpath下 在cmd窗口中,查看 classpath的内容是否已经加上该路径,win7 下cmd窗口一定要是管理员身份执行 2.在D盘新建一个DOM4JWriter ...

  3. Build Tool-自动化构建工具

    输入:工程文件+编译说明文件: 处理:自动化构建工具+编译器: 输出:可执行文件. 相对于手动编译. Historically, build automation was accomplished t ...

  4. C# 处理URL地址

    calendarset.do?start=1548518400&end=1552147200&_=1546421856958calendarset.do?start=155093760 ...

  5. 1、DataGridView

    DataGridView赋值后 通过RowPostPaint事件绘制行号 private void AddXh() { DataGridViewTextBoxColumn col = new Data ...

  6. JavaScript 复杂判断的优雅写法

    JavaScript 复杂判断的优雅写法 <div> <input type="button" name="btn" value=" ...

  7. 推荐系统相关比赛-kaggle

    from: 七月在线 电商推荐与销量预测相关案例 一.预测用户对哪个事件感兴趣(感兴趣不一定去参加) 用户历史参加事件.社交信息.浏览信息(app).要预测的事件 recall:召回率 准确率: 协同 ...

  8. Linux:用户和组总结

    从创建文件说起:useradd xiaomi           这里是创建了xiaomi用户 默认系统还会创建:/home/xiaomi  /var/mail/xiaomi        即家目录和 ...

  9. VS单元测试"未能加载文件或程序集,或它的某一个依赖项"

    Autofac.Core.DependencyResolutionException : An error occurred during the activation of a particular ...

  10. PHP中的几个随机数生成函数

    PHP中的几个随机数生成函数 rand() 基于 libc 的随机种子发生器 mt_rand() 基于 Mersenne Twister 算法返回随机整数.它可以产生随机数值的平均速度比 libc 提 ...