博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
zoj 3332 Strange Country II(DFS或者网上哈密尔顿图解决)
阅读量:4036 次
发布时间:2019-05-24

本文共 2002 字,大约阅读时间需要 6 分钟。

1、

2、题目大意:

给定一张图,求出从一个点出发,经过所有的点有且仅一次,输出路径,如果不存在这样的一条路径,输出“Impossible”;

3、题目:

Strange Country II

           


           

               
Time Limit: 1 Second                                   
Memory Limit: 32768 KB                                                   
Special Judge                           

           


           

You want to visit a strange country. There are n cities in the country. Cities are numbered from 1 to n. The unique way to travel in the country is taking planes.Strangely, in this strange country, for every two cities A and B, there is a flight from A to B or from B to A, but not both.You can start at any city and you can finish your visit in any city you want. You want to visit each city exactly once. Is it possible?

Input

There are multiple test cases. The first line of input is an integer T (0 < T <= 100) indicating the number of test cases.Then T test cases follow. Each test case starts with a line containing an integer n (0 < n <= 100), which is the number of cities.Each of the next n * (n - 1) / 2 lines contains 2 numbers A, B (0 < A, B <= n, A != B), meaning that there is a flight from city A to city B.

Output

For each test case:

  • If you can visit each city exactly once, output the possible visiting order in a single line please. Separate the city numbers by spaces. If there are more than one orders, you can output any one.
  • Otherwise, output "Impossible" (without quotes) in a single line.

Sample Input

3121 231 21 32 3

Sample Output

11 21 2 3

 

4、AC代码:

#include
#include
int map[105][105];int visit[105];int path[105];int n,flag;void dfs(int x,int step){ path[step]=x; if(step==n) { flag=1; return; } if(flag) return ; for(int i=1;i<=n;i++) { if(map[x][i]==1 && visit[i]==0) { visit[i]=1; dfs(i,step+1); if(flag) return ; visit[i]=0; } }}int main(){ int t,a,b; scanf("%d",&t); while(t--) { flag=0; memset(path,0,sizeof(path)); scanf("%d",&n); int m=n*(n-1)/2; memset(map,0,sizeof(map)); memset(visit,0,sizeof(visit)); for(int i=0;i

 

转载地址:http://leddi.baihongyu.com/

你可能感兴趣的文章
cppcheck c++静态代码检查
查看>>
在C++中使用Lua
查看>>
C++中使用Mongo执行count和distinct运算
查看>>
一些socket的编程经验
查看>>
socket编程中select的使用
查看>>
关于AIS编码解码的两个小问题
查看>>
GitHub 万星推荐:黑客成长技术清单
查看>>
可以在线C++编译的工具站点
查看>>
关于无人驾驶的过去、现在以及未来,看这篇文章就够了!
查看>>
所谓的进步和提升,就是完成认知升级
查看>>
昨夜今晨最大八卦终于坐实——人类首次直接探测到了引力波
查看>>
如何优雅、机智地和新公司谈薪水?
查看>>
为什么读了很多书,却学不到什么东西?
查看>>
长文干货:如何轻松应对工作中最棘手的13种场景?
查看>>
如何用好碎片化时间,让思维更有效率?
查看>>
No.147 - LeetCode1108
查看>>
No.174 - LeetCode1305 - 合并两个搜索树
查看>>
No.175 - LeetCode1306
查看>>
No.176 - LeetCode1309
查看>>
No.182 - LeetCode1325 - C指针的魅力
查看>>