hdu3594Cactus

Cactus

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


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

题意:判断是不是仙人掌图,仙人掌图的定义, 1.首先是强连通的 2.任意一个边只能属于一个环。

思路:对于连通性我们可以用targan判断,然后targan中遇到环就遍历环,给每条边计数,如果边出现次数大于1就说明该边出现于两个环中

代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
using namespace std;
const int N=20000+10;
int dfn[N],low[N],fa[N],in[N],belong[N];
int cnt,index;
bool instack[N];
int flag;
vector<int>e[N];
stack<int>s;
int n;
void init()
{
    cnt=index=0;
    while(!s.empty())
    {
        s.pop();
    }
    for(int i=0;i<=n;i++)
    {
        e[i].clear();
    }
    memset(fa,-1,sizeof(fa));
    memset(instack,false,sizeof(instack));
    memset(dfn,0,sizeof(dfn));
    memset(low,0,sizeof(low));
    memset(in,0,sizeof(in));
    memset(belong,0,sizeof(belong));
}
void targan(int u)
{
    dfn[u]=low[u]=++index;
    s.push(u);
    instack[u]=true;
    int v;
    for(int i=0;i<e[u].size();i++)
    {
        v=e[u][i];
        if(!dfn[v])
        {
            fa[v]=u;
            targan(v);
            low[u]=min(low[u],low[v]);
        }
        else if(instack[v])
        {
            low[u]=min(low[u],dfn[v]);
            int t=u;
            while(fa[t]!=v)
            {
                in[t]++;
                if(in[t]>1)
                {
                    flag=0;
                    return;
                }
                t=fa[t];
            }
        }
    }
    if(dfn[u]==low[u])
    {
        cnt++;
        do
        {
            v=s.top();
            s.pop();
            belong[v]=cnt;
            instack[v]=false;
        }while(u!=v);
    }
}
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        scanf("%d",&n);
        init();
        int a,b;
        flag=1;
        while(scanf("%d%d",&a,&b)&&a+b)
        {
            a++,b++;
            e[a].push_back(b);
        }
        for(int i=1;i<=n;i++)
        {
            if(!dfn[i])
                targan(i);
        }
       
        if(flag==0||cnt!=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、付费专栏及课程。

余额充值