HDU 3594 Cactus

Cactus

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


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
解题思路:论文题,直接上仙人掌图的性质
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<stack>
#include<algorithm>
#include<utility>
using namespace std;
const int maxn = 20005;
struct nd
{
        int to,next;
}edge[maxn*5];

int dfn[maxn],vis[maxn];
int flag[maxn],head[maxn];
int ecnt,cnt,idx,ok,low[maxn];
stack<int>st;
void add(int s,int t)
{
        edge[ecnt].to = t;
        edge[ecnt].next = head[s];
        head[s] = ecnt++;
}
void tarjan(int u)
{
        int i,v;
        if(ok)return;
        dfn[u] = low[u] = ++idx;
        vis[u] = 1;
        st.push(u);
        for(i = head[u]; i != -1; i = edge[i].next)
        {
                v = edge[i].to;
                if(dfn[v]==0){
                        tarjan(v);
                        low[u] = min(low[v],low[u]);
                }else if(vis[v]){
                        low[u] = min(dfn[v],low[u]);
                        if(dfn[v]!=low[v]) ok = 1;
                }
                if(ok)return;
        }
        if(dfn[u]==low[u])
        {
                cnt++;
                if(cnt>1)ok=1;
                do{
                        v = st.top();
                        st.pop();
                        vis[v] = 0;
                }while(st.top()!=u);
        }
}
int main()
{
        int i,j,s,t,n,cas;
        scanf("%d",&cas);
        while(cas--)
        {
                scanf("%d",&n);
                memset(vis,0,sizeof(vis));
                memset(head,-1,sizeof(head));
                memset(flag,0,sizeof(flag));
                memset(dfn,0,sizeof(dfn));
                ecnt = cnt = idx = 0;

                scanf("%d %d",&s,&t);
                while(s+t){
                        ++s; ++t;
                        add(s,t);
                        scanf("%d %d",&s,&t);
                }

                while(!st.empty()) st.pop();
                ok = 0;
                for(i = 1; i <= n; ++ i)if(!dfn[i]) tarjan(i);
                if(ok||cnt!=1)puts("NO");
                else puts("YES");
        }
        return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值