zoukankan      html  css  js  c++  java
  • HDU 3594.Cactus 仙人掌图

    Cactus

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 2206    Accepted Submission(s): 1039


    Problem Description
    1. It is a Strongly Connected graph.
    2. Each edge of the graph belongs to a circle and only belongs to one circle.
    We call this graph as CACTUS.



    There is an example as the figure above. The left one is a cactus, but the right one isn’t. Because the edge (0, 1) in the right graph belongs to two circles as (0, 1, 3) and (0, 1, 2, 3).
     
    Input
    The input consists of several test cases. The first line contains an integer T (1<=T<=10), representing the number of test cases.
    For each case, the first line contains a integer n (1<=n<=20000), representing the number of points.
    The following lines, each line has two numbers a and b, representing a single-way edge (a->b). Each case ends with (0 0).
    Notice: The total number of edges does not exceed 50000.
     
    Output
    For each case, output a line contains “YES” or “NO”, representing whether this graph is a cactus or not.

     
    Sample Input
    2
    4
    0 1
    1 2
    2 0
    2 3
    3 2
    0 0
    4
    0 1
    1 2
    2 3
    3 0
    1 3
    0 0
     
    Sample Output
    YES
    NO
     
    Author
    alpc91
     
    Source
     
    Recommend
    zhengfeng   |   We have carefully selected several similar problems for you:  3593 3600 3599 3598 3595 
     
    题意:判断是否是仙人掌图。
    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<cstring>
    #include<algorithm>
    #include<queue>
    #include<stack>
    #include<vector>
    #include<map>
    #include<set>
    #include<bitset>
    using namespace std;
    #define PI acos(-1.0)
    #define eps 1e-8
    typedef long long ll;
    typedef pair<int,int> P;
    const int N=1e5+100,M=1e5+100;
    const int inf=0x3f3f3f3f;
    const ll INF=1e18+7,mod=1e9+7;
    struct edge
    {
        int from,to;
        int next;
    };
    edge es[M];
    int cut,head[N];
    int scc_cut=0,dfs_clock=0;
    int pre[N],low[N];
    int vis[N],fa[N];
    stack<int>s;
    void init()
    {
        cut=0;
        memset(head,-1,sizeof(head));
    }
    void addedge(int u,int v)
    {
        cut++;
        es[cut].from=u,es[cut].to=v;
        es[cut].next=head[u];
        head[u]=cut;
    }
    bool findfa(int u,int pa)
    {
        while(fa[u]!=pa)
        {
            if(++vis[u]>1) return false;
            u=fa[u];
        }
        return true;
    }
    bool dfs(int u)
    {
        pre[u]=low[u]=++dfs_clock;
        s.push(u);
        for(int i=head[u]; i!=-1; i=es[i].next)
        {
    
            int v=es[i].to;
            if(!pre[v])
            {
                fa[v]=u;
                if(!dfs(v)) return false;
                low[u]=min(low[u],low[v]);
            }
            else
            {
                low[u]=min(low[u],pre[v]);
                if(!findfa(u,v)) return false;
            }
        }
        if(pre[u]==low[u])
        {
            if(++scc_cut>1) return false;
            while(!s.empty())
            {
                int v=s.top();
                s.pop();
                if(v==u) break;
            }
        }
        return true;
    }
    bool solve(int n)
    {
        scc_cut=dfs_clock=0;
        memset(pre,0,sizeof(pre));
        memset(low,0,sizeof(low));
        memset(vis,0,sizeof(vis));
        for(int i=0; i<n; i++)
            if(!pre[i]&&!dfs(i)) return false;
        return true;
    }
    int main()
    {
        int T;
        scanf("%d",&T);
        while(T--)
        {
            init();
            int n,u,v;
            scanf("%d",&n);
            while(scanf("%d%d",&u,&v)&&!(u==0&&v==0))
                addedge(u,v);
            if(solve(n)) puts("YES");
            else puts("NO");
        }
        return 0;
    }
    强联通分量 仙人掌图
    代码:
  • 相关阅读:
    【已解决】ERR_BLOCKED_BY_XSS_AUDITOR:Chrome 在此网页上检测到了异常代码:解决办法
    【已解决】Microsoft visual c++ 14.0 is required问题解决办法
    爬虫处理网站的bug---小于号未转化为实体符
    pymysql 在数据库中插入空值
    python 正则括号的使用及踩坑
    pymysql 解决 sql 注入问题
    python3 操作MYSQL实例及异常信息处理--用traceback模块
    LeetCode 837. 新21点 | Python
    LeetCode 面试题64. 求1+2+…+n | Python
    LeetCode 101. 对称二叉树 | Python
  • 原文地址:https://www.cnblogs.com/GeekZRF/p/7582927.html
Copyright © 2011-2022 走看看