D. Almost Acyclic Graph 判断减一条边能不能得到DAG

本文探讨了一道关于通过删除一条边将有向图转换为无环图的问题。利用拓扑排序判断图中是否存在环,并介绍了 Tarjan 算法的应用。通过实例展示了如何寻找关键边来解决问题。
D. Almost Acyclic Graph
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 be traversed in only one direction). You are allowed to remove at most one edge from it.

Can you make this graph acyclic by removing at most one edge from it? A directed graph is called acyclic iff it doesn't contain any cycle (a non-empty path that starts and ends in the same vertex).

Input

The first line contains two integers n and m (2 ≤ n ≤ 500, 1 ≤ m ≤ min(n(n - 1), 100000)) — the number of vertices and the number of edges, respectively.

Then m lines follow. Each line contains two integers u and v denoting a directed edge going from vertex u to vertex v (1 ≤ u, v ≤ n, u ≠ v). Each ordered pair (u, v) is listed at most once (there is at most one directed edge from u to v).

Output

If it is possible to make this graph acyclic by removing at most one edge, print YES. Otherwise, print NO.

Examples
Input
3 4
1 2
2 3
3 2
3 1
Output
YES
Input
5 6
1 2
2 3
3 2
3 1
2 1
4 5
Output
NO
Note

In the first example you can remove edge , and the graph becomes acyclic.

In the second example you have to remove at least two edges (for example, and ) in order to make the graph acyclic

 

https://www.cnblogs.com/Blogggggg/p/8290354.html  //这篇博客给了两个解法。

判断是否存在环用的拓扑排序,我想到的一个问题是度数是由连接这个点的很多条边决定的,为什么度数减一能够契合那条关键边边并得到正确答案呢?

我臆想的答案是:  每个点的价值就是: 使 所通向的点的度数 -1,那么先实现这个价值肯定是好的,所以说只要度数变为0了,剩下的那条边就一定是关键边了。

 顺便复习Tarjan

#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
using namespace std;
const int N=508;
const int M=1e5+88;
int n,m,tot,x,y,head[N],to[M],nxt[M],ru[N];
bool vis[N],f,B[N];
void add(int u,int v){
    to[++tot]=v;nxt[tot]=head[u];head[u]=tot;
}
int dfn[N],q[N],low[N],top,sz,ry[N];
void Tajan(int u){
    if(f) return;
    low[u]=dfn[u]=++sz;
    vis[u]=1;
    q[++top]=u;
    for(int i=head[u];!f&&i;i=nxt[i]){
        int v=to[i];
        if(!dfn[v]) Tajan(v),low[u]=min(low[u],low[v]);
        else if(vis[v]&&low[u]>dfn[v]) low[u]=dfn[v];
    }
    if(low[u]==dfn[u]) {
        int x,p=0;
        do{
            x=q[top--];
            vis[x]=0;
            B[x]=1;
            ry[p++]=x;
        }while(x!=u);
        if(p>1) f=1;
        else B[x]=0;
    }
}
pair<int,int>re[N];
void dfs(int u,int pos){
    if(!f) return;
    for(int i=head[u];i&&f;i=nxt[i]) {
        int v=to[i];
        if(v==ry[0]) {re[pos].first=u,re[pos].second=v; sz=pos;f=0;return;}
        if(!B[v]||vis[v]) continue;
        else {vis[v]=1;re[pos].first=u,re[pos].second=v;dfs(v,pos+1);}
    }
}
int ru1[N];
bool Topsort(){
    int l=0,r=0,own=0;
    for(int i=1;i<=n;++i) if(!ru1[i]) q[r++]=i;
    while(l<r) {
        int u=q[l++];
        for(int i=head[u];i;i=nxt[i]) {
            --ru1[to[i]];
            if(!ru1[to[i]]) q[r++]=to[i];
        }
    }
    return r==n;
}
int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;++i) {
        scanf("%d%d",&x,&y);
        add(x,y);
        ++ru[y];
    }
    for(int i=1;!f&&i<=n;++i) if(!dfn[i]) Tajan(i);
    if(!f) {puts("YES");return 0;}
    memset(vis,0,sizeof(vis));
    dfs(ry[0],0);
    for(int i=0;i<=sz;++i) {
        --ru[re[i].second];
        for(int j=1;j<=n;++j) ru1[j]=ru[j];
        if(Topsort()) {puts("YES");return 0;}
        ++ru[re[i].second];
    }
    puts("NO");
}

 

转载于:https://www.cnblogs.com/mfys/p/8401671.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值