【题目】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. Struts2(五)

    以下内容是基于导入struts2-2.3.32.jar包来讲的 1.文件上传 A.单文件上传 <body> <form action="${pageContext.requ ...

  2. QMap使用

    版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:QMap使用     本文地址:https://www.techieliang.com/201 ...

  3. 【第九周】beta-review阶段贡献分分配

    组名: 新蜂 组长: 武志远 组员: 宫成荣 谢孝淼 杨柳 李峤 项目名称: java俄罗斯方块NEO 武志远 武志远 武志远 武志远 武志远 宫成荣 宫成荣 杨柳 宫成荣 宫成荣 李峤 杨柳 李峤 ...

  4. 初探Android动画之门

    原文地址:http://www.cnblogs.com/kross/p/3376451.html 最近自学了下动画的相关知识,总结为今天的文章,希望对大家有帮助. Android中的动画大致分为三种: ...

  5. react-router之代码分离

    概念 无需用户下载整个应用之后才能访问访问它.即边访问边下载.因此我们设计一个组件<Bundle>当用户导航到它是来动态加载组件. import loadSomething from 'b ...

  6. Django 2.0 学习(17):Django 用户认证(auth模块)

    Django 用户认证(auth模块) 一.认证登陆 在进行用户登陆验证的时候,如果是自己写代码,就必须要先查询数据库,看用户输入的用户名是否存在于数据库中:如果用户存在于数据库中,然后再验证用户输入 ...

  7. [BZOJ1588][HNOI2002]营业额统计 无旋Treap

    [HNOI2002]营业额统计 时间限制: 5 Sec  内存限制: 162 MB 题目描述 营业额统计 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以 ...

  8. TechDay公开课实录:PaddlePaddle车牌识别实战和心得

    车牌识别作为一种常见的图像识别的应用场景,已经是一个非常成熟的业务了,在传统的车牌识别中,可以使用字符分割+字符识别的方式来进行车牌识别,而深度学习兴起后,出现了很多端到端的车牌识别模型,不用分割字符 ...

  9. Docker学习笔记三:Docker部署Java web系统

    Docker部署Java Web系统 1.在root目录下创建一个路径test/app mkdir test && cd test&& mkdir app && ...

  10. 【BZOJ1391】Order(网络流,最小割)

    [BZOJ1391]Order(网络流,最小割) 题面 BZOJ权限题... 良心洛谷 题目描述 有N个工作,M种机器,每种机器你可以租或者买过来. 每个工作包括若干道工序,每道工序需要某种机器来完成 ...