zoj 3332 Strange Country II

转载请注明出处:http://blog.csdn.net/u012860063

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3332

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 <cstdio>
#include<cstring>
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 T,a, b, i;
	scanf("%d", &T);
	while(T--)
	{
		scanf("%d", &n);
		memset(map,0,sizeof(map));
		memset(vis,0,sizeof(vis));
		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 == 0)
					printf("%d", num[i]);
				else
					printf(" %d", num[i]);
			}
			printf("\n");
		}
		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
*/

zoj 3332 Strange Country II,布布扣,bubuko.com

时间: 2024-08-03 19:01:46

zoj 3332 Strange Country II的相关文章

ZOJ 3332 Strange Country II (竞赛图构造哈密顿通路)

链接:http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=3332 题意: 给你一个N,代表含有N个点的竞赛图,接着的N * (N- 1) / 2行两个点u, v,代表存在有向边<u,v>,问是否能构造出来一个哈密顿通路. 思路: 竞赛图一定含有哈密顿通路,不一定含有哈密顿回路.则不可能出现不存在的情况,直接构造就可以,至于方法可看我的另外一篇文章:http://www.cnblogs.com/Ash-ly/p/5452580.

Strange Country II ( 简单的dfs,但是要注意细节)

Strange Country II 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, th

Strange Country II 暴力dfs

这题点的个数(<=50)有限, 所以可以纯暴力DFS去搜索 //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler #include <stdio.h> #include <iostream> #include <fstream> #include <cstring> #include <cmath> #include <stack> #

Zoj3332-Strange Country II(有向竞赛图)

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 fro

zoj 3620 Escape Time II

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4744 Escape Time II Time Limit: 2 Seconds      Memory Limit: 65536 KB There is a fire in LTR ’ s home again. The fire can destroy all the things in t seconds, so LTR has to escape in t second

zoj 3620 Escape Time II dfs

Escape Time II Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3620 Description There is a fire in LTR ’ s home again. The fire can destroy all the things in t seconds, so LTR has to escape i

ZOJ 3557-How Many Sets II(Lucas定理+插板法求组合数)

题目地址:ZOJ 3557 题意:给一个集合,一共n个元素,从中选取m个元素,满足选出的元素中没有相邻的元素,一共有多少种选法(结果对p取模1 <= p <= 10^9) 思路:用插板法求出组合数.既然是从n个数中选择m个数,那么剩下的数为n-m,那么可以产生n-m+1个空,这道题就变成了把m个数插到这n-m+1个空中有多少种方法,即C(n-m+1,m)%p.然后就Lucas定理上去乱搞.因为这道题的p较大,所以不能预处理. #include <stdio.h> #include

ZOJ 3574 Under Attack II 归并排序求逆序对

Under Attack II Time Limit: 5 Seconds      Memory Limit: 65536 KB Because of the sucessfully calculation in Under Attack I, Doctor is awarded with Courage Cross and promoted to lieutenant. But the war seems to end in never, now Doctor has a new order

ZOJ 3466 The Hive II

插头DP.. 求用若干个回路走遍整张图的方案数..然而格子是六边形的TAT 想了好久...按照列来转移好写一点..其实和正方形格子差不多? 膜了标程才知道,如果回路数没有限制的话..可以只记录有没有插头,而不用去记录插头属于哪个联通块.. (要是去记录插头属于哪个联通块就会爆long long了TAT 代码依然很慢... 1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<a