【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 _ ...
随机推荐
- java下执行mongodb
1.1连单台mongodb Mongo mg = newMongo();//默认连本机127.0.0.1 端口为27017 Mongo mg = newMongo(ip);//可以指定ip 端口默认 ...
- 查看sqlserver数据库的编码格式
查询语句:SELECT COLLATIONPROPERTY('Chinese_PRC_Stroke_CI_AI_KS_WS', 'CodePage'): 查询结果: 936 简体中文GBK 950 ...
- mybatis_mysql
SELECT round(avg(c.AVG_DELAY_TIME)) as AVG_DELAY FROM `result_road_saturation_day` a LEFT JOIN info_ ...
- 如何在windows下Apache环境开启htaccess伪静态功能
以下文章来自于网络,只做学习用 很多国人习惯用windows服务器或者在windows系统下调试PHP程序,在调试货使用的时候就遇到开启伪静态的各种问题,今天在网络上搜集了一些开启伪静态需要注意 ...
- Java实现简单的RPC框架(美团面试)
一.RPC简介 RPC,全称为Remote Procedure Call,即远程过程调用,它是一个计算机通信协议.它允许像调用本地服务一样调用远程服务.它可以有不同的实现方式.如RMI(远程方法调用) ...
- Java对象空间分配流程
对象空间分配流程如下: 针对这个流程,分别解释一下每一个选项的使用场景. 栈上分配: 栈上分配的基础在于逃逸分析,逃逸分析可以得到三种对象的逃逸状态. 全局逃逸:一个对象的引用逃出了方法或者线程. ...
- iOS-开发者账号与证书
0.开发者账号的申请 1.iOS-证书相关 2.iOS-证书申请 3.iOS-APNS证书申请与使用 4.iOS-App发布证书的申请与使用
- 51nod 1674 区间的价值V2(思维+拆位+尺取法)
最近被四区题暴虐... 题意:lyk拥有一个区间. 它规定一个区间的价值为这个区间中所有数and起来的值与这个区间所有数or起来的值的乘积. 例如3个数2,3,6.它们and起来的值为2,or起来的值 ...
- 高斯消元模板(pascal)
洛谷P3389评测 program rrr(input,output); const eps=1e-8; var a:..,..]of double; n,i,j,k:longint; t:doubl ...
- poj1655 Balancing Act求树的重心
Description Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. Deleting any nod ...