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.
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
- HDOJ 题目3964 Find The Simple Circle(DFS)
- HDU 3964 Find The Simple Circle
- HDOJ 题目1010Tempter of the Bone(搜索,dfs)
- HDOJ 题目1597 find the nth digit(数学)
- POJ 题目1426 Find The Multiple(DFS)
- HDOJ 题目2266 How Many Equations Can You Find(DFS水)
- HDOJ 1374 The Circumference of the Circle (求外接圆)
- HDOJ 题目1595 find the longest of the shortest(枚举,最短路记录路径)
- Find The Multiple(dfs)
- HDOJ 题目1142 A Walk Through the Forest(spfa dfs)
- HDOJ 题目1181变形课(dfs)
- HDOJ 题目1072 Nightmare(dfs)
- HDOJ 题目1518 Square(dfs)
- HDOJ 题目1045 Fire Net(dfs)
- HDOJ 题目1015 Safecracker(dfs)
- HDOJ题目2952Counting Sheep (DFS)
- HDOJ 题目1342Lotto(DFS)
- HDOJ 题目4707 Pet(DFS)
- Linux下配置Nginx和Tomcat
- Android-读取联系人练习
- 挑战1:使用jQuery生成水平图片幻灯效果
- body div 和 body>div 的区别
- 使用OpenCV的GPU模块(Win7x64+VS2010+OpenCV2.4.5+CUDA5.0)配置详解
- HDOJ 题目3964 Find The Simple Circle(DFS)
- android客户端性能测试 (一)
- poj3461
- APK反编译基础知识
- Android之流媒体播放
- There is a problem with this website’s security certificate in IE
- wamp初始配置
- android 网络通信框架volly
- UITableView优化