【CodeForces】915 D. Almost Acyclic Graph 拓扑排序找环

【题目】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=600,maxm=100010;
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]==-1)return;
    if(vis[x]==1){p=x;suc[fa]=x;return;}
    vis[x]=1;
    for(int i=first[x];i;i=e[i].from)if(deg[e[i].v]>0){
        dfs(e[i].v,x);
        if(p){if(fa&&!suc[p])suc[fa]=x;break;}
    }
    if(!p)vis[x]=-1;
}
bool solve(int o){
    cnt=0;
    for(int i=1;i<=n;i++){deg[i]=in[i];if(i==e[o].v)deg[i]--;if(deg[i]==0)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]==0)Q.push(e[i].v),cnt++;
    }
    if(cnt==n)return 1;
    return 0;
}
int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++){
        int u,v;
        scanf("%d%d",&u,&v);
        insert(u,v);map[u][v]=tot;
    }
    cnt=0;
    for(int i=1;i<=n;i++){deg[i]=in[i];if(in[i]==0)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]==0)Q.push(e[i].v),cnt++;
    }
    if(cnt==n){printf("YES");return 0;}
    for(int i=1;i<=n;i++)if(deg[i]>0&&!p)dfs(i,0);
    int pp=p;
    do{
        if(solve(map[p][suc[p]])){printf("YES");return 0;}
        p=suc[p];
    }while(p!=pp);
    printf("NO");
    return 0;
}

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

原文地址:https://www.cnblogs.com/onioncyc/p/8287645.html

时间: 2024-11-10 18:01:09

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

HDOJ 5222 Exploration 并查集+拓扑排序 找环

Exploration Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 715    Accepted Submission(s): 197 Problem Description Miceren likes exploration and he found a huge labyrinth underground! This

Legal or Not(拓扑排序判环)

http://acm.hdu.edu.cn/showproblem.php?pid=3342 Legal or Not Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5788    Accepted Submission(s): 2678 Problem Description ACM-DIY is a large QQ group w

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

One of my friends is always drunk. So, sometimes I get a bit confused whether he is drunk or not. So, one day I was talking to him, about his drinks! He began to describe his way of drinking. So, let me share his ideas a bit. I am expressing in my wo

HDU 3342 Legal or Not(拓扑排序判环)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3342 题目: Problem Description ACM-DIY is a large QQ group where many excellent acmers get together. It is so harmonious that just like a big family. Every day,many "holy cows" like HH, hh, AC, ZT, lc

拓扑排序判环

拓扑排序的核心就是每次找入度为0的点,进入输出队列 ,然后将与此点相连的节点入度减1重复做以上操作.当做n-1 次后还有点没进输出队列 那么这些点就是环上的 因为环上的各点入度都为1 没有0的 就不能更新.也就是说拓扑排序一遍之后,如果是DAG所有点都恰好入队一次如果有环,那么一定存在没有入队的点. 例题: Legal or NotTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Pr

拓扑排序找最大环最小环

找最大环 P5145 漂浮的鸭子 题意很明确:求图中的最大环 今天新学到的一种方法--拓扑排序求环 由于拓扑排序每次都是从入度为0的点开始,而环上的点的入度都不会为0,所以环上的点就不会参加排序,也就是说,经过拓扑排序后剩下的边和点构成的都是环. 这样我们就可以直接把每个环扫一遍记录最大环就结束了. //2019/09/27 #include<bits/stdc++.h> using namespace std; template <typename T>inline void r

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

Almost Acyclic Graph CodeForces - 915D time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output You are given a directed graph consisting of n vertices and m edges (each edge is directed, so it can

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/29/C Description One day Bob got a letter in an envelope. Bob knows that when Berland's post officers send a letter directly from city «A» to city «B

codeforces#285--C - Misha and Forest(拓扑排序变形)

C - Misha and Forest Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Description Let's define a forest as a non-directed acyclic graph (also without loops and parallel edges). One day Misha played with the