【CodeForces】915 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 拓扑排序找环的更多相关文章
- codeforces 915D Almost Acyclic Graph 拓扑排序
大意:给出一个有向图,问能否在只去掉一条边的情况下破掉所有的环 解析:最直接的是枚举每个边,将其禁用,然后在图中找环,如果可以就YES,都不行就NO 复杂度O(N*M)看起来不超时 但是实现了以后发现 ...
- bzoj2938 AC自动机 + 拓扑排序找环
https://www.lydsy.com/JudgeOnline/problem.php?id=2938 题意:给出N个01病毒序列,询问是否存在一个无限长的串不存在病毒序列 正常来说,想要寻找一个 ...
- Legal or Not(拓扑排序判环)
http://acm.hdu.edu.cn/showproblem.php?pid=3342 Legal or Not Time Limit: 2000/1000 MS (Java/Others) ...
- POJ 1094 Sorting It All Out(拓扑排序+判环+拓扑路径唯一性确定)
Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 39602 Accepted: 13 ...
- LightOJ1003---Drunk(拓扑排序判环)
One of my friends is always drunk. So, sometimes I get a bit confused whether he is drunk or not. So ...
- Almost Acyclic Graph CodeForces - 915D (思维+拓扑排序判环)
Almost Acyclic Graph CodeForces - 915D time limit per test 1 second memory limit per test 256 megaby ...
- 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 ...
- 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 ...
- CodeForces 1213F (强联通分量分解+拓扑排序)
传送门 •题意 给你两个数组 p,q ,分别存放 1~n 的某个全排列: 让你根据这两个数组构造一个字符串 S,要求: (1)$\forall i \in [1,n-1],S_{pi}\leq S _ ...
随机推荐
- CentOS7实现RabbitMQ高可用集群
CentOS安装RabbitMQ集群 1.有3台已安装RabbitMQ的机器 192.168.38.133 rabbitmq1 192.168.38.137 rabbitmq2 192.168.38. ...
- Struts2(六)
以下内容是基于导入struts2-2.3.32.jar包来讲的 1.OGNL OGNL是Object-Graph Navigation Language的缩写,全称为对象图导航语言,是一种功能强大的表 ...
- List、Set、Map典型实现
1:集合 Collection(单列集合) List(有序,可重复) ArrayList 底层数据结构是数组,查询快,增删慢 线程不安全,效率高 Vector 底层数据结构是数组,查询快,增删慢 线程 ...
- 【C++】构造函数不能是虚函数
1 虚函数对应一个vtable,这大家都知道,可是这个vtable其实是存储在对象的内存空间的.问题出来了,如果构造函数是虚的,就需要通过 vtable来调用,可是对象还没有实例化,也就是内存空间还没 ...
- 【Python】python 反射机制在实际的应用场景讲解
剖析python语言中 "反射" 机制的本质和实际应用场景一. 前言 def s1(): print("s1是这个函数的名字!") s = "s1&q ...
- An In-Depth Look at the HBase Architecture--转载
原文地址:https://www.mapr.com/blog/in-depth-look-hbase-architecture In this blog post, I’ll give you an ...
- 【Java】全站编码过滤器GenericEncodingFilter代码与配置
编码过滤器GenericEncodingFilter: package com.fuck.web.filter; import java.io.IOException; import java.i ...
- 【BZOJ3242】【NOI2013】快餐店(动态规划)
[BZOJ3242][NOI2013]快餐店(动态规划) 题面 BZOJ 题解 假设我们要做的是一棵树,那么答案显然是树的直径的一半. 证明? 假设树的直径是\(2d\),那么此时最远点的距离是\(d ...
- 洛谷 P1325 雷达安装 解题报告
P1325 雷达安装 题目描述 描述: 假设海岸线是一条无限延伸的直线.它的一侧是陆地,另一侧是海洋.每一座小岛是在海面上的一个点.雷达必须安装在陆地上(包括海岸线),并且每个雷达都有相同的扫描范围d ...
- 解题:HNOI 2012 永无乡
题面 并查集维护连通性,然后暴力启发式合并就完了,记得合并时边DFS边清空数组 #include<cstdio> #include<cstring> #include<a ...