HDU 3594

HDU - 3594                                                            
Time Limit:                                                        1000MS                         Memory Limit: 32768KB 64bit IO Format:                            %I64d & %I64u                        

 

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
                

Hint

Source

2010 ACM-ICPC Multi-University Training Contest(16)――Host by NUDT
 
判断是仙人掌图的条件:
1.是强连通图;
2.不存在一条边在多个环中。

思路:

条件1 只需判断只有一个连通分量即可;

条件2 判断是否low[v] != dfn[v]。

 

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define maxn 20000+10
#define maxm 50000+10
int t, n;
int Stack[maxn], dfn[maxn], low[maxn], head[maxn];
int scc, top, index, flag, tot;
bool instack[maxn];
struct Edge
{
    int to, next;
}edge[maxm];
void addedge(int u, int v)
{
    edge[tot].next = head[u];
    edge[tot].to = v;
    head[u] = tot ++;
}
void init()
{
    memset(head, -1, sizeof(head));
    memset(dfn, 0, sizeof(dfn));
    memset(instack, 0, sizeof(instack));
    tot = index = scc = flag = top = 0;
}
void tarjan(int u)
{
    int v;
    dfn[u] = low[u] = ++index;
    Stack[top++] = u;
    instack[u] = 1;
    for(int i = head[u]; i != -1;i = edge[i].next)
    {
        v = edge[i].to;
        if(!dfn[v])
        {
            tarjan(v);
            low[u] = min(low[u], low[v]);
        }
        else if(instack[v])
        {
            low[u] = min(low[u], dfn[v]);
            if(low[v] != dfn[v])  // 此时必然有一条边被两个环共用
            {
                flag = 1;
                return ;
            }
        }
        if(flag) return;
    }
    if(dfn[u] == low[u])
    {
        scc ++;
        if(scc > 1)
        flag = 1;
        int j;
        do
        {
           j = Stack[-- top];
           instack[j] = false;

        }
        while(j != u);
    }
}
int main()
{
    int a, b;
    scanf("%d", &t);
    while(t --)
    {
        scanf("%d", &n);
        init();
        while( scanf("%d%d",&a, &b) != EOF && (a+b))
        {
            a++;
            b++;
            addedge(a, b);
        }
        for(int i = 1;i <= n;i ++)
        {
            if(flag || scc > 1) break;
            if(!dfn[i])
            tarjan(i);
        }
        if(flag || scc != 1)
             printf("NO\n");
        else printf("YES\n");
    }
    return 0;
}


 

 

 


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值