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;
    }
    强联通分量 仙人掌图
    代码:
  • 相关阅读:
    线段树 HDU 4217 Data Structure? 单点更新 区间查询
    线段树详解(转)这个博客很棒~
    hdu 1075 What Are You Talking About trie字典树
    hdu 1247 Hat’s Words trie 简单字典树
    hdu Just a Hook 线段树——成段操作 区域覆盖
    FLOYD
    做有效沟通的管理者
    幸福的四重境界
    5个馒头与100单生意
    成功和财富源于想法
  • 原文地址:https://www.cnblogs.com/GeekZRF/p/7582927.html
Copyright © 2011-2022 走看看