首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >ZOJ 3332 Strange Country II

ZOJ 3332 Strange Country II

作者头像
ShenduCC
发布于 2018-04-26 09:18:44
发布于 2018-04-26 09:18:44
62000
代码可运行
举报
文章被收录于专栏:算法修养算法修养
运行总次数: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 删除。

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

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
HDUOJ----(1031)Design T-Shirt
Design T-Shirt Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4370    Accepted Submission(s): 2124 Problem Description Soon after he decided to design a T-shirt for our Algorithm Board on Free-City
Gxjun
2018/03/21
5920
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
1660
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
5330
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
5540
图论--网络流--最小割 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
3920
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
5710
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
5850
CodeForces 666B World Tour(spfa+枚举)
逮捕罪犯(树)- 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
2690
逮捕罪犯(树)- HDU 3069
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
6750
hdu 4081 Qin Shi Huang's National Road System (次小生成树)
Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3843    Accepted Submission(s): 1336 Problem Description During the Warring States Period of ancient China(476
Gxjun
2018/03/26
9970
hdu  4081   Qin Shi Huang's National Road System   (次小生成树)
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
4190
2017 Multi-University Training Contest - Team 9 1002&&HDU 6162 Ch’s gift【树链部分+线段树】
Ch’s gift Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1354    Accepted Submission(s): 496 Problem Description Mr. Cui is working off-campus and he misses his girl friend very much. After a w
Angel_Kitty
2018/04/09
6090
航班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
4040
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
4100
HOJ 2133&POJ 2964 Tourist(动态规划)
Tourist Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1503 Accepted: 617 Description A lazy tourist wants to visit as many interesting locations in a city as possible without going one step further than necessary. Starting from
ShenduCC
2018/04/26
7030
HDUOJ---3371Connect the Cities
Connect the Cities Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7997    Accepted Submission(s): 2267 Problem Description In 2100, since the sea level rise, most of the cities disappear. Though
Gxjun
2018/03/22
6930
ZOJ 3715 Kindergarten Election
At the beginning of the semester in kindergarten, the n little kids (indexed from 1 to n, for convenience) in class need to elect their new leader. The ith kid will vote for his best friend fi (where 1 ≤ fi ≤ n, and it's too shame to vote for yourself, so 
ShenduCC
2018/04/26
4470
POJ 2594 Treasure Exploration
Description Have you ever read any book about treasure exploration? Have you ever see any film about
attack
2018/04/11
5910
hdu----149850 years, 50 colors(最小覆盖点)
50 years, 50 colors Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1695    Accepted Submission(s): 931 Problem Description On Octorber 21st, HDU 50-year-celebration, 50-color balloons floating
Gxjun
2018/03/22
5780
hdu----149850 years, 50 colors(最小覆盖点)
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
8070
相关推荐
HDUOJ----(1031)Design T-Shirt
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档