Loading [MathJax]/jax/output/CommonHTML/config.js
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >ZOJ 3332 Strange Country II

ZOJ 3332 Strange Country II

作者头像
ShenduCC
发布于 2018-04-26 09:18:44
发布于 2018-04-26 09:18:44
61400
代码可运行
举报
文章被收录于专栏:算法修养算法修养
运行总次数:0
代码可运行

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

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
3
1
2
1 2
3
1 2
1 3
2 3

Sample Output

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
1
1 2
1 2 3
dfs#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <stdio.h>

using namespace std;
int a[105][105];
int ans[105];
int vis[105];
int n;
bool res;
void dfs(int x,int cnt)
{
	if(cnt>n)
		return;
	if(res)
	{
		ans[cnt]=x;
		return;
	}
    if(cnt==n)
	{
		res=true;
		ans[cnt]=x;
		return;

	}
    for(int i=1;i<=n;i++)
	{
		if(a[x][i]&&!vis[i])
		{
			vis[i]=1;
			dfs(i,cnt+1);
			vis[i]=0;
			if(res)
			{
				ans[cnt]=x;
				return;
			}

		}
	}
}
int main()
{
	int t;
	int x,y;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		memset(a,0,sizeof(a));
		for(int i=1;i<=n*(n-1)/2;i++)
		{
			scanf("%d%d",&x,&y);
			a[x][y]=1;
		}
		memset(vis,0,sizeof(vis));
		res=false;
		for(int i=1;i<=n;i++)
		{
			vis[i]=1;
			dfs(i,1);
			vis[i]=0;
			if(res)
				break;
		}
		if(!res)
		{
           printf("Impossible\n");
		   continue;
		}
		for(int i=1;i<=n;i++)
		{
			if(i!=n)
			printf("%d ",ans[i]);
			else
				printf("%d",ans[i]);
		}
		printf("\n");
	}
	return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016-05-04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
CodeForces 666B World Tour(spfa+枚举)
B. World Tour time limit per test 5 seconds memory limit per test 512 megabytes input standard input output standard output A famous sculptor Cicasso goes to a world tour! Well, it is not actually a world-wide. But not everyone should have th
ShenduCC
2018/04/26
5800
CodeForces 666B World Tour(spfa+枚举)
K - Highway Project  ZOJ - 3946 【 SPFA 求最小时间下最小距离】
Edward, the emperor of the Marjar Empire, wants to build some bidirectional highways so that he can reach other cities from the capital as fast as possible. Thus, he proposed the highway project.
Lokinli
2023/03/09
2510
ZOJ 3946 Highway Project(Dijkstra)
Highway Project ---- Time Limit: 2 Seconds      Memory Limit: 65536 KB ---- Edward, the emperor of the Marjar Empire, wants to build some bidirectional highways so that he can reach other cities from the capital as fast as possible. Thus, he proposed the h
ShenduCC
2018/04/26
5310
航班Flight(SPFA+dp)- HDU 3499
Recently, Shua Shua had a big quarrel with his GF. He is so upset that he decides to take a trip to some other city to avoid meeting her. He will travel only by air and he can go to any city if there exists a flight and it can help him reduce the total cos
ACM算法日常
2018/08/07
3970
图论--网络流--最小割 HDU 2485 Destroying the bus stations(最短路+限流建图)
Gabiluso is one of the greatest spies in his country. Now he’s trying to complete an “impossible” mission ----- to make it slow for the army of City Colugu to reach the airport. City Colugu has n bus stations and m roads. Each road connects two bus stations directly, and all roads are one way streets. In order to keep the air clean, the government bans all military vehicles. So the army must take buses to go to the airport. There may be more than one road between two bus stations. If a bus station is destroyed, all roads connecting that station will become no use. What’s Gabiluso needs to do is destroying some bus stations to make the army can’t get to the airport in k minutes. It takes exactly one minute for a bus to pass any road. All bus stations are numbered from 1 to n. The No.1 bus station is in the barrack and the No. n station is in the airport. The army always set out from the No. 1 station. No.1 station and No. n station can’t be destroyed because of the heavy guard. Of course there is no road from No.1 station to No. n station. Please help Gabiluso to calculate the minimum number of bus stations he must destroy to complete his mission.
风骨散人Chiam
2020/10/28
3730
图论-多源最短路径(Floyd算法)
分析: 给定若干双向正值边和单向负值边,问是否存在负圈(使其时光倒流回到原点)。 所以在第二重循环,求完第i个结点后判断。
唔仄lo咚锵
2020/10/16
6580
图论-多源最短路径(Floyd算法)
Friends (ZOJ - 3710)
Alice lives in the country where people like to make friends. The friendship is bidirectional and if any two person have no less than k friends in common, they will become friends in several days. Currently, there are totally n people in the country, and m friendship among them. Assume that any new friendship is made only when they have sufficient friends in common mentioned above, you are to tell how many new friendship are made after a sufficiently long time.
Lokinli
2023/03/09
1610
hdu----(2586)How far away ?(DFS/LCA/RMQ)
How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5492    Accepted Submission(s): 2090 Problem Description There are n houses in the village and some bidirectional roads connecting
Gxjun
2018/03/26
5640
逮捕罪犯(树)- HDU 3069
Country ALPC has n cities, and the cities are connected by undirected roads. Furthermore, there is exactly one route between each pair of cities. Now some criminals break the prison and the police do not know where they are. And the criminals can stay in a city or move on the roads. The police office has made a decision to send policemen to arrest the breakers. If a police and a criminal at a same point at the same time the criminal is under arrest, no matter in a city or on a road. The police office wants to minimize the number of policemen to arrest all the criminals. Now the map of Country ALPC is given, please tell the police office the least number of policemen required.
ACM算法日常
2018/08/07
2620
逮捕罪犯(树)- HDU 3069
2019 ICPC 银川网络赛 F-Moving On (卡Cache)
Firdaws and Fatinah are living in a country with nn cities, numbered from 11 to nn. Each city has a risk of kidnapping or robbery.
风骨散人Chiam
2020/10/28
4050
ZOJ 3941 Kpop Music Party(省赛, 贪心)
Kpop Music Party ---- Time Limit: 2 Seconds      Memory Limit: 65536 KB ---- Marjar University often hosts Kpop music festival. A Kpop music festival will last several days. During a Kpop festival, there will be a Kpop party every day. Kpop music is very p
ShenduCC
2018/04/26
8000
ZOJ 3710 Friends
Alice lives in the country where people like to make friends. The friendship is bidirectional and if any two person have no less than k friends in common, they will become friends in several days. Currently, there are totally n people in the country, and 
ShenduCC
2018/04/26
4160
周练19.11.03/10
它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。
AngelNH
2020/04/16
4090
HDU 4605 Magic Ball Game(可持续化线段树,树状数组,离散化)
Magic Ball Game Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 2489    Accepted Submission(s): 759 Problem Description When the magic ball game turns up, Kimi immediately falls in it. The inter
ShenduCC
2018/04/27
6770
HDUOJ----2485 Destroying the bus stations(2008北京现场赛A题)
Destroying the bus stations                                                                                     Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)                                                            
Gxjun
2018/03/22
5460
CodeForces 24A Ring road(dfs)
A. Ring road time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Nowadays the one-way traffic is introduced all over the world in order to improve driving safety and reduce traffic jams
ShenduCC
2018/04/27
6710
PAT 甲级 1003Emergency(Dijkstra最短路)
1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some
ShenduCC
2018/04/26
6490
hdu-----(2807)The Shortest Path(矩阵+Floyd)
The Shortest Path Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2440    Accepted Submission(s): 784 Problem Description There are N cities in the country. Each city is represent by a matrix siz
Gxjun
2018/03/26
9020
Codeforce 322E Ciel the Commander (点分治)
Now Fox Ciel becomes a commander of Tree Land. Tree Land, like its name said, has n cities connected by n - 1 undirected roads, and for any two cities there always exists a path between them.
风骨散人Chiam
2020/10/28
5960
Codeforce 322E Ciel the Commander (点分治)
Break Standard Weight (ZOJ 3706)
The balance was the first mass measuring instrument invented. In its traditional form, it consists of a pivoted horizontal lever of equal length arms, called the beam, with a weighing pan, also called scale, suspended from each arm (which is the origin of the originally plural term "scales" for a weighing instrument). The unknown mass is placed in one pan, and standard masses are added to this or the other pan until the beam is as close to equilibrium as possible. The standard weights used with balances are usually labeled in mass units, which are positive integers.
Lokinli
2023/03/09
1580
相关推荐
CodeForces 666B World Tour(spfa+枚举)
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档