zoukankan      html  css  js  c++  java
  • D. Almost Acyclic Graph 判断减一条边能不能得到DAG

    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");
    }
  • 相关阅读:
    008. redis 主从复制原理、断点续传、无磁盘化复制、过期 key 处理
    007.redis replication 以及 master 持久化对主从架构的安全意义
    006. redis 如何通过读写分离来承载读请求 QPS 超过 10 万 +?
    005.在项目中部署 redis 企业级数据备份方案以及各种踩坑的数据恢复容灾演练
    人月神话---向进度落后的项目中增加人手,只会使进度更加落后
    人月神话---空泛的估算
    人月神话---不为系统测试安排足够的时间简直就是一场灾难
    人月神话---成本的确随开发产品的人数和时间的不同,有着很大的变化,进度却不是如此
    springmvc其他类获取request记得web.xml
    gson转换问题
  • 原文地址:https://www.cnblogs.com/mfys/p/8401671.html
Copyright © 2011-2022 走看看