HDOJ 题目3964 Find The Simple Circle(DFS)

Find The Simple Circle

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 453    Accepted Submission(s): 211


Problem Description
Given a directed graph, your task is to find the simple circles. The simple circle is a subgraph meeting the following conditions:

1. There are m ( m > 1 ) different vertexes and arcs in it.

2. The simple circle can be represented as a seqence "v1,v2...vm" and v1,v2...vm are different vertexes in it. There is an arc from the i-th vertex to the (i+1)-th vertex or from the m-th vertex to the 1st vertex in the simple circle.

You will be given the adjacency matrix of the directed graph, what you should do is outputing all the simple circles in it in lexicographic order. The output format of one simple circle is a string where the i-th character is the index of the i-th vertex in the simple circle seqence. To unify the format, the 1st vertex should be the vertex with smallest index. Vertexes are indexed from 0. A sequence a1,a2...ak appears in lexicographic order before a sequence b1,b2...bk if and only if the first ai, which is different from bi, is less than bi.
 

Input
There are several test cases in the input. Each case begins with a line with an integer N (2 ≤ N ≤ 9), denoting the number of vertexes in the graph. In the following N lines with N integers, the j-th number in the i-th line will be 1 or 0, representing that there is an arc from the i-th vertex to the j-th vertex or not. It is ensured that the i-th number in the i-th line is 0. Input is terminated by EOF.
 

Output
Output one simple circle sequence in one line, and end each case with a blank line.
 

Sample Input
  
  
3 0 1 1 1 0 1 1 1 0 4 0 1 0 1 1 0 1 0 1 0 0 1 1 0 1 0
 

Sample Output
  
  
01 012 02 021 12 01 012 0123 03 032 23
 

Source
 

Recommend
We have carefully selected several similar problems for you:   3965  3966  3961  3970  3969 
 
ac代码
#include<stdio.h>
#include<string.h>
int vis[1010],stack[1010],map[1010][1010];
int n,top,inx;
void dfs(int u)
{
	stack[top++]=u;
	for(int i=0;i<n;i++)
	{
		if(map[u][i])
		{
			if(vis[i]&&i==inx)
			{
				for(int j=0;j<top;j++)
					printf("%d",stack[j]);
				printf("\n");
			}
			else
				if(!vis[i])
				{
					vis[i]=1;
					dfs(i);
					vis[i]=0;
					top--;
				}
		}
		
	}
}
int main()
{
	//int n;
	while(scanf("%d",&n)!=EOF)
	{
		int i,j;
		for(i=0;i<n;i++)
		{
			for(j=0;j<n;j++)
				scanf("%d",&map[i][j]);
		}
		memset(vis,0,sizeof(vis));
		for(i=0;i<n;i++)
		{
			inx=i;
			top=0;
			vis[i]=1;
			dfs(i);
		}
		printf("\n");
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值