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. 开始使用Bootstrap

    bootstrap使用到的图标字体文件格式有 .woff,IIS7下需要添加MIME映射:.woff  application/x-font-woff

  2. CentOS安装自动补全安装包

    CentOS7标准版有这个功能,但是CentOS6却没有,其实很简单: 1.安装bash-completion yum install bash-completion 2.保存一下最新的缓存 yum ...

  3. Atitit.软件开发的最终的设计 dsl化,ast化(建立ast, 解析执行ast)

    Atitit.软件开发的最终的设计 dsl化,ast化(建立ast, 解析执行ast) 1. 使用js,html 撰写dsl1 1.1. 架构图1 1.2. html2 1.3. Js2 1.4. C ...

  4. Atitit.ati dwr的原理and设计 attilax 总结 java php 版本

    Atitit.ati dwr的原理and设计 attilax 总结 java php 版本 1. dwr的优点相对于ajax来说..1 2. DWR工作原理1 3. Dwr的架构2 4. 自定义dwr ...

  5. 安装yii2高级应用模板

    composer global require "fxp/composer-asset-plugin:1.0.0" composer create-project --prefer ...

  6. Java多线程之内置锁与显示锁

    Java中具有通过Synchronized实现的内置锁,和ReentrantLock实现的显示锁,这两种锁各有各的好处,算是互有补充,今天就来做一个总结. Synchronized 内置锁获得锁和释放 ...

  7. ToStringBuilder学习(二):两种方法用法优缺点及一个问题

    研究ApacheCommon源码, 先从一个最简单的开始,即围绕Object类里的toString方法自动化实现的一系列类.         怎么来自动化地实现toString方法, 有两种:反射和手 ...

  8. iOS swift 富文本显示 富文本在iOS中使用场景和解决方案

    项目中很多地方都会用到富文本的内容:比如一般的商品详情,视频详情,资讯详情等,运营人员通过后台的富文本编辑器编辑的内容,前端拿到的就是一段富文本的字符串,这富文本大多都是图片和文字的组合.我们今天介绍 ...

  9. linux学习笔记34--命令rcp和scp

    rcp代表“remote file copy”(远程文件拷贝).该命令用于在计算机之间拷贝文件.rcp命令有两种格式.第一种格式用于文件到文件的拷贝:第二种格式用于把文件或目录拷贝到另一个目录中. 1 ...

  10. python学习笔记2---函数

    函数主要是为了代码复用. 函数分为两种:系统库预定义函数,自定义函数. 函数格式: def functionName(): statement 函数调用: funtionName() 函数的参数:形参 ...