码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-02-15 09:31:12      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:

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");
	}
}


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

标签:

原文地址:http://blog.csdn.net/yu_ch_sh/article/details/43818559

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!