Strange Country II(暴力DFS判有向图的哈密顿通路)

173 篇文章 3 订阅
99 篇文章 0 订阅

Link:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3757


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 AB (0 < AB <= nA != 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

3
1
2
1 2
3
1 2
1 3
2 3

Sample Output

1
1 2
1 2 3

Author:  CAO, Peng
Source:  The 7th Zhejiang Provincial Collegiate Programming Contest

题意:给定一个有向图,判断是否存在一条哈密顿通路。


AC code:
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int n;
bool vis[1010];
int deg[1010];
int Map[1010][1010];
int fg;
int path[1010];
void dfs(int u,int cnt)
{
	vis[u]=true;
	path[cnt]=u;
	if(cnt==n)
	{
		fg=1;
		return;	
	}
	for(int i=1;i<=n&&!fg;i++)
	{
		if(!vis[i]&&Map[u][i])
		{
			dfs(i,cnt+1);
			vis[i]=false;
		}
	}
}
int main(){
   //freopen("in.txt","r",stdin);
   int i,u,v;
   int cas;
   scanf("%d",&cas);
   while(cas--)
   {
   		scanf("%d",&n);
   		int m=n*(n-1)/2;
		memset(Map,0,sizeof(Map));
   		for(i=1;i<=m;i++)
   		{
   			scanf("%d%d",&u,&v);
   			if(u!=v&&!Map[u][v])//排除自环和重边 
   			{
				Map[u][v]=1;
			}
		}
		for(i=1;i<=n;i++)
		{
			memset(vis,0,sizeof(vis));  
			fg=0;
			dfs(i,1);//从度数为1的点开始深搜 
			if(fg)
			{
				printf("%d",path[1]);
				for(i=2;i<=n;i++)
				{
					printf(" %d",path[i]);
				}
				puts("");
				break;
			}
		}
		if(!fg)
		{
			puts("Impossible");
		}
   }
   return 0;
}



  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林下的码路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值