zoj 3332-Strange Country II-dfs

Description

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

先说说题目的意思吧:

某人到了一个陌生城市,这个城市的特点就是:每两个城市之间都有飞机直接到达,但是不一定的双向的,也就是A - > B != B - > A,

给出城市的数量n,以及n * (n - 1) / 2  条城市 x  到 城市 y 的路,(表示x 到 y 有飞机),问你是不是可以每个城市都经过并且只经过一次

可以的话,一次输出经过的城市,否则输出Impossible


分析:很显然,先建立一个邻接矩阵map[A][B], 表示A 到B之间路线的情况,if(A - > B),map[A][B] = 1;  else map[A][B] = 0;

           然后用num[N] 记录走过了的城市,关键是怎么来表示呢?  放在dfs里面,表示这之间确实有路,那么就可以记录了

          用vis[N]查询是不是访问过了,这个很关键

         t的作用是判断是不是所有的城市都去过了,没去过一个城市,t做一次减法。。

详见代码:

#include <stdio.h>
#include <string>
#include <stdlib.h>
#include <iostream>

using namespace std;

int map[105][105];
int num[105] ;
bool vis[105];
int flag ;
int t, n;
void dfs(int v) {
	num[t ++] = v;
	if(t == n) { 
		flag = 1; return;
	}
	for(int u = 1; u <= n; u ++) {
		if(!vis[u] && map[v][u] == 1) {
			vis[u] = true; dfs(u); 
			if(flag == 1) return;
			t --;
			vis[u] = false;
		}
	}
}
int main() {
	int cas;
	int a, b, i;
	scanf("%d", &cas);
	while(cas --) {
		scanf("%d", &n);
		for (i = 0; i < 105; i ++)
			for (int j = 0; j < 105; j ++)
				map[i][j] = 0;
			for (int k = 0; k < 105; k ++)
				vis[k] = 0;
			for(i = 0; i < n * (n - 1) / 2; i ++) {
				scanf("%d %d", &a, &b) ;
				map[a][b] = 1;
			}
			flag = 0;
			for(i = 1; i <= n; i ++) {
				vis[i] = true;
				t = 0;
				dfs(i);
				if(flag) break;
				vis[i] = false;
			}
			if(flag) {
				for(i = 0; i < n; i ++) {
					if(i != n - 1)
						printf("%d ", num[i]);
					else
						printf("%d\n", num[i]);
				}
			}
			else  printf("Impossible\n");
	}
	return 0;
}
/*
5
5
1 3
3 5
2 5
2 4
1 3
1 3
1 3
1 3
1 3
1 3
6
1 2
1 6
1 4
5 6
5 4
3 4
3 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值