HDOJ 题目3594 Cactus(强连通,判仙人掌图)

本文介绍了一种用于判断给定图是否为仙人掌图的算法,并提供了详细的实现代码。仙人掌图是一种强连通图,其特点在于每条边恰好属于一个环。文章通过示例阐述了仙人掌图的概念,并给出了输入输出样例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Cactus

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


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 
 ac代码
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<queue>
#include<string>
#define INF 0xfffffff
#define min(a,b) (a>b?b:a)
using namespace std;
int dfn[20005*3],low[20005*3],stack[20005*3],head[20005*3],ins[20005*3],belong[20005*3];
int cnt,top,time,n,taj,flag;
struct s
{
	int u,v,next;
}edge[50005*3];
void add(int u,int v)
{
	edge[cnt].u=u;
	edge[cnt].v=v;
	edge[cnt].next=head[u];
	head[u]=cnt++;
}
void init()
{
	memset(dfn,-1,sizeof(dfn));
	memset(stack,0,sizeof(stack));
	memset(low,-1,sizeof(low));
	memset(head,-1,sizeof(head));
	memset(ins,0,sizeof(ins));
	memset(belong,-1,sizeof(belong));
	cnt=top=time=taj=0;
}
void tarjan(int u)
{
	dfn[u]=low[u]=time++;
	ins[u]=1;
	stack[top++]=u;
	for(int i=head[u];i!=-1;i=edge[i].next)
	{
		int v=edge[i].v;
		if(dfn[v]==-1)
		{
			tarjan(v);
			low[u]=min(low[v],low[u]);
		}
		else
		{	
			if(ins[v])
			{
				low[u]=min(dfn[v],low[u]);
				if(dfn[v]!=low[v])
				{
					flag=1;
				}
			}
		}
	}
	if(low[u]==dfn[u])
	{
		int now;
		taj++;
		do
		{
			now=stack[--top];
			ins[now]=0;
			if(belong[now]!=-1)
				flag=1;
			belong[now]=taj;
		}while(now!=u);
	}
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int a,b;
		scanf("%d",&n);
		init();
		while(scanf("%d%d",&a,&b),a||b)
		{
			add(a,b);
		}
		flag=0;
		for(int i=0;i<n;i++)
		{
			if(dfn[i]==-1)
				tarjan(i);
		}
		if(taj==1&&flag==0)
			printf("YES\n");
		else
			printf("NO\n");
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值