【题目】D. Almost Acyclic Graph

【题意】给定n个点的有向图(无重边),问能否删除一条边使得全图无环。n<=500,m<=10^5。

【算法】拓扑排序

【题解】找到一个简单环,则欲删除的边一定经过该环。尝试环上的每一条边(至多n条边)后再次拓扑排序判断全图是否有环。

拓扑排序后定位到简单环:剩余图是环+环内DAG,DFS过程中将走入死路的点标-1,访问过标1,找到访问过的点就是简单环。换起始点直到找到环为止。

复杂度O(nm)。

#include<cstdio>
#include<queue>
#include<algorithm>
using namespace std;
const int maxn=,maxm=;
struct edge{int v,from;}e[maxm];
int map[maxn][maxn],tot,cnt,n,m,first[maxn],p,vis[maxn],in[maxn],deg[maxn],suc[maxn];
queue<int>Q;
void insert(int u,int v){tot++;e[tot].v=v;e[tot].from=first[u];first[u]=tot;in[v]++;}
void dfs(int x,int fa){
if(p||vis[x]==-)return;
if(vis[x]==){p=x;suc[fa]=x;return;}
vis[x]=;
for(int i=first[x];i;i=e[i].from)if(deg[e[i].v]>){
dfs(e[i].v,x);
if(p){if(fa&&!suc[p])suc[fa]=x;break;}
}
if(!p)vis[x]=-;
}
bool solve(int o){
cnt=;
for(int i=;i<=n;i++){deg[i]=in[i];if(i==e[o].v)deg[i]--;if(deg[i]==)Q.push(i),cnt++;}
while(!Q.empty()){
int x=Q.front();Q.pop();
for(int i=first[x];i;i=e[i].from)if(i!=o&&--deg[e[i].v]==)Q.push(e[i].v),cnt++;
}
if(cnt==n)return ;
return ;
}
int main(){
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++){
int u,v;
scanf("%d%d",&u,&v);
insert(u,v);map[u][v]=tot;
}
cnt=;
for(int i=;i<=n;i++){deg[i]=in[i];if(in[i]==)Q.push(i),cnt++;}
while(!Q.empty()){
int x=Q.front();Q.pop();
for(int i=first[x];i;i=e[i].from)if(--deg[e[i].v]==)Q.push(e[i].v),cnt++;
}
if(cnt==n){printf("YES");return ;}
for(int i=;i<=n;i++)if(deg[i]>&&!p)dfs(i,);
int pp=p;
do{
if(solve(map[p][suc[p]])){printf("YES");return ;}
p=suc[p];
}while(p!=pp);
printf("NO");
return ;
}

另一种解法:枚举点i,in[i]--,拓扑排序找环。这样相当于删除一条指向n的边后全图找环。

【CodeForces】915 D. Almost Acyclic Graph 拓扑排序找环的更多相关文章

  1. codeforces 915D Almost Acyclic Graph 拓扑排序

    大意:给出一个有向图,问能否在只去掉一条边的情况下破掉所有的环 解析:最直接的是枚举每个边,将其禁用,然后在图中找环,如果可以就YES,都不行就NO 复杂度O(N*M)看起来不超时 但是实现了以后发现 ...

  2. bzoj2938 AC自动机 + 拓扑排序找环

    https://www.lydsy.com/JudgeOnline/problem.php?id=2938 题意:给出N个01病毒序列,询问是否存在一个无限长的串不存在病毒序列 正常来说,想要寻找一个 ...

  3. Legal or Not(拓扑排序判环)

    http://acm.hdu.edu.cn/showproblem.php?pid=3342 Legal or Not Time Limit: 2000/1000 MS (Java/Others)   ...

  4. POJ 1094 Sorting It All Out(拓扑排序+判环+拓扑路径唯一性确定)

    Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 39602   Accepted: 13 ...

  5. LightOJ1003---Drunk(拓扑排序判环)

    One of my friends is always drunk. So, sometimes I get a bit confused whether he is drunk or not. So ...

  6. Almost Acyclic Graph CodeForces - 915D (思维+拓扑排序判环)

    Almost Acyclic Graph CodeForces - 915D time limit per test 1 second memory limit per test 256 megaby ...

  7. Codeforces Beta Round #29 (Div. 2, Codeforces format) C. Mail Stamps 离散化拓扑排序

    C. Mail Stamps Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/problem ...

  8. Educational Codeforces Round 25 E. Minimal Labels 拓扑排序+逆向建图

    E. Minimal Labels time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  9. CodeForces 1213F (强联通分量分解+拓扑排序)

    传送门 •题意 给你两个数组 p,q ,分别存放 1~n 的某个全排列: 让你根据这两个数组构造一个字符串 S,要求: (1)$\forall i \in [1,n-1],S_{pi}\leq S _ ...

随机推荐

  1. 关于双系统下Ubuntu不能访问Windows中某个盘的问题

    1.问题描述   在Ubuntu系统下访问Windows系统中磁盘时出现无法访问的情况,具体如下显示:   该问题为磁盘挂载错误,需要进行修复. 2.解决办法   (1)打开终端:如果没有安装ntfs ...

  2. PAT L1-032 Left-pad

    https://pintia.cn/problem-sets/994805046380707840/problems/994805100684361728 根据新浪微博上的消息,有一位开发者不满NPM ...

  3. VC的常用调试方法

    前言 VS是非常强大的IDE,所以掌握VSVC的常用方法,将会使得我们找出问题解决问题事半功倍. 目录 VSVC的常用调试方法 前言 1. Watch窗口查看伪变量 2. 查看指针指向的一序列值 3. ...

  4. 关于SIGPIPE信号

    对一个对端已经关闭的socket调用两次write, 第二次将会生成SIGPIPE信号, 该信号默认结束进程.具体的分析可以结合TCP的"四次握手"关闭. TCP是全双工的信道, ...

  5. fiddler启动报错Unable to bind to port [8888],ErrorCode:10106

    启动运行fiddler 报错,提示Unable to bind to port [8888],ErrorCode:10106 解决方式: 使用Fiddler或其他类似的监听工具出现这种错误时, Una ...

  6. 第188天:extend拷贝创建对象的原理

    一.拷贝创建对象的原理 //拷贝创建对象核心代码 function extend(target,source) { //遍历对象 for(var i in source){ target[i] = s ...

  7. 【.Net】win10 uwp unix timestamp 时间戳 转 DateTime

    有时候需要把网络的 unix timestamp 转为 C# 的 DateTime ,在 UWP 可以如何转换? 转换函数可以使用下面的代码 private static DateTime UnixT ...

  8. 辣鸡蒟蒻Klaier的一些计划

    需要熟练的东西:cdq分治,堆,树链剖分,tarjan及其它一些图论算法,网络流,kmp,字符串哈希,线段树主席树,树状数组,斜率优化dp 需要学的东西:lct,后缀数组,AC自动机,平衡树 球队收益 ...

  9. 【Java并发编程】之四:守护线程与线程阻塞的四种情况

    守护线程 Java中有两类线程:User Thread(用户线程).Daemon Thread(守护线程) ​ 用户线程即运行在前台的线程,而守护线程是运行在后台的线程. 守护线程作用是为其他前台线程 ...

  10. Golang的第一个程序-Hello, World !

    安装Golang: 1. 下载安装包 https://golang.google.cn/dl/ 我这里使用压缩包,下载后解压到D盘(自定义). 2. 添加环境变量:把解压后的bin目录添加到环境变量中 ...