HDOJ 题目3964 Find The Simple Circle(DFS)

来源:互联网 发布:删除源码包安装的程序 编辑:程序博客网 时间:2024/05/20 18:55

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
30 1 11 0 11 1 040 1 0 11 0 1 01 0 0 11 0 1 0
 

Sample Output
0101202021120101201230303223
 

Source
2011 Multi-University Training Contest 13 - Host by HIT
 

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


0 0
原创粉丝点击