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. Strace--系统调用分析问题集锦

    ---------------------------------------------------------------------------------------------------- ...

  2. epoll反应堆模型

    ================================ 下面代码实现的思想:epoll反应堆模型:( libevent 网络编程开源库 核心思想) 1. 普通多路IO转接服务器: 红黑树 ― ...

  3. Wpf 抽屉效果

    在android开发中有抽屉效果,就是在页面的边上有一个按钮,可以通过点击或者拖拽这个按钮,让页面显示.Wpf也可以实现相同的效果. 主要是通过一个DoubleAnimation和RectAnimat ...

  4. Atitit.获取某个服务 网络邻居列表 解决方案

    Atitit.获取某个服务 网络邻居列表 解决方案 原理,带入某个ip扫描从0---255 很快,多线程几秒就可以出来. 使用CountDownLatch来join线程.. 返回  [{ " ...

  5. hdu 3304 Interesting Yang Yui Triangle

    hdu 3304 Interesting Yang Yui Triangle 题意: 给出P,N,问第N行的斐波那契数模P不等于0的有多少个? 限制: P < 1000,N <= 10^9 ...

  6. Git-查看远程分支、本地分支、创建分支

    1.查看本地分支 $ git branch * br-2.1.2.2 master 2.查看远程分支 $ git branch -r origin/HEAD -> origin/master o ...

  7. redis命令_ZRANGE

    ZRANGE key start stop [WITHSCORES] 返回有序集 key 中,指定区间内的成员. 其中成员的位置按 score 值递增(从小到大)来排序. 具有相同 score 值的成 ...

  8. 有效提升大数据量写入excel的效率

    在开发过程中经常会有需要将数据导出到 excel 的需求,当数据量很大,达到几万甚至几十万.几百万级别的时候,如何加快生成 excel 的速度呢?首先普及一下知识背景:Excel2003 及以下版本一 ...

  9. zoj 1109 Language of FatMouse(字典树)

    Language of FatMouse Time Limit: 10 Seconds      Memory Limit: 32768 KB We all know that FatMouse do ...

  10. confirg

     如果用户点击确定按钮,则 confirm() 返回 true.如果点击取消按钮,则 confirm() 返回 false.在用户点击确定按钮或取消按钮把对话框关闭之前,它将阻止用户对浏览器的所有输入 ...