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 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;
}

ZOJ 3332 Strange Country II的更多相关文章

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

    链接:http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=3332 本文链接:http://www.cnblogs.com/Ash-l ...

  2. K - Strange Country II 暴力dfs判断有向图是否连通//lxm

    You want to visit a strange country. There are n cities in the country. Cities are numbered from 1 t ...

  3. Strange Country II 暴力dfs

    这题点的个数(<=50)有限, 所以可以纯暴力DFS去搜索 //#pragma comment(linker, "/STACK:16777216") //for c++ Co ...

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

    You want to visit a strange country. There are n cities in the country. Cities are numbered from 1 t ...

  5. zoj 3620 Escape Time II dfs

    题目链接: 题目 Escape Time II Time Limit: 20 Sec Memory Limit: 256 MB 问题描述 There is a fire in LTR ' s home ...

  6. zoj 3356 Football Gambling II【枚举+精度问题】

    题目: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3356 http://acm.hust.edu.cn/vjudge/ ...

  7. zoj 3620 Escape Time II

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4744 Escape Time II Time Limit: 2 Seconds ...

  8. ZOj 3466 The Hive II

    There is a hive in the village. Like this. There are 8 columns(from A to H) in this hive. Different ...

  9. ZOJ 2674 Strange Limit

    欧拉函数. #include<iostream> #include<stdio.h> #include<string.h> #include<algorith ...

随机推荐

  1. C# 异或校验算法

    C# 的异或校验算法 直接上代码 public partial class FormCRC : Form { public FormCRC() { InitializeComponent(); } p ...

  2. 【VBA研究】利用DateAdd函数取上月或上年同期的日期

    作者:iamlaosong DateAdd函数返回一个日期.这一日期加上了一个时间间隔.通过这个函数能够计算非常多我们须要的日期,比方上月上年同期日期等. 语法 DateAdd(interval, n ...

  3. python3操作mysql教程

    一.下载\安装\配置 1. python3 Python3下载网址:http://www.python.org/getit/ 当前最新版本是python3.2,下载地址是 http://www.pyt ...

  4. Android 中查看内存的使用情况集经常使用adb命令

    1. 在IDE中查看Log信息 当程序执行垃圾回收的时候,会打印一条Log信息.其格式例如以下: D/dalvikvm: <GC_Reason> <Amount_freed>, ...

  5. [docker]docker压力测试

    内存测试 -m --memory-swap 内存+swap docker run -it --rm -m 200M --memory-swap=300M progrium/stress --vm 1 ...

  6. 大型站点技术架构PDF阅读笔记(一):

    1.数据库读写分离: 2.系统吞吐量和系统并发数以及系统响应时间之间的关系: 3.系统负载的概念: 4.反向代理的概念: 5.使用缓存来读取数据: 6.利用cookie来记录session: 利用co ...

  7. Android Service演义

    摘要: 本文基于Android 5.1代码,介绍了Android Service的运作机理.按理说,网上此类文章已经很多了,本不需我再赘述.但每个人理解技术的方式多少会有所不同,我多写一篇自己理解的s ...

  8. 标志位 last break

    last-完成rewrite指令的处理,之后搜索对应的URI和location; break-完成rewrite指令的外理 [root@web01 app]# cat /app/server/ngin ...

  9. 171. Anagrams【medium】

    Given an array of strings, return all groups of strings that are anagrams. Notice All inputs will be ...

  10. 0049 MyBatis关联映射--一对一关系

    世上的事务总不是孤立存在的,表现在Java类里面,则是类与类之间的关系,比如继承is-a.依赖use-a.关联has-a,反映在数据库中,则是表与表之间的关系,比如外键 关联关系存在着以下几种类型:一 ...