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

解析: 裸的有向竞赛图,有向竞赛图要满足的条件是有N*(N-1)/2条边,也就是每个点与其他点都有边相连,求哈密顿通路。

代码

#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
#include<sstream>
#include<algorithm>
#include<utility>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<cmath>
#include<iterator>
#include<stack>
using namespace std;
const int maxn=;
int N,ans[maxn];
bool link[maxn][maxn];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&N);
memset(link,false,sizeof(link));
int tot=N*(N-)/;
int u,v;
for(int i=;i<=tot;i++)
{
scanf("%d%d",&u,&v);
link[u][v]=true;
}
ans[]=;
int i,j,k;
for(k=;k<=N;k++)
{
for(j=;j<k;j++) if(link[k][ans[j]]) break;
for(i=k;i>j;i--) ans[i]=ans[i-];
ans[j]=k;
}
for(int i=;i<=N;i++) printf("%d%c",ans[i],i==N?'\n':' ');
}
return ;
}

Zoj3332-Strange Country II(有向竞赛图)的更多相关文章

  1. ZOJ 3332 Strange Country II

    Strange Country II Time Limit: 1 Second      Memory Limit: 32768 KB      Special Judge You want to v ...

  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. ZOJ 3332 Strange Country II (竞赛图构造哈密顿通路)

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

  4. Strange Country II 暴力dfs

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

  5. 【转】欧拉回路&特殊图下的哈密顿回路题集

    转自:http://blog.csdn.net/shahdza/article/details/7779385 欧拉回路[HDU]1878 欧拉回路 判断3018 Ant Trip 一笔画问题1116 ...

  6. 【转载】图论 500题——主要为hdu/poj/zoj

    转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...

  7. hdu图论题目分类

    =============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many ...

  8. HDU图论题单

    =============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many ...

  9. 【HDOJ图论题集】【转】

    =============================以下是最小生成树+并查集====================================== [HDU] How Many Table ...

随机推荐

  1. URAL 2038 Minimum Vertex Cover

    2038. Minimum Vertex Cover Time limit: 1.0 secondMemory limit: 64 MB A vertex cover of a graph is a ...

  2. js基础例子购物车升级版(未优化版)

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  3. C++11多线程教学II

    从我最近发布的C++11线程教学文章里,我们已经知道C++11线程写法与POSIX的pthreads写法相比,更为简洁.只需很少几个简单概念,我们就能搭建相当复杂的处理图片程序,但是我们回避了线程同步 ...

  4. Centos7安装Oracle JDK

    查看Linux是否自带的JDK,如有openJDK,则卸载 java -version

  5. CSS基础知识笔记(一)

    css 样式由选择符和声明组成,而声明又由属性和值组成: 选择符: 又称选择器,指明网页中要应用样式规则的元素,如本例中是网页中所有的段(p)的文字将变成蓝色,而其他的元素(如ol)不会受到影响. 声 ...

  6. node-sqlite3-API-归纳总结

    SQLITE3-API-LIST:API1. new sqlite3.Database(filename,[mode],[callback]) 返回数据库对象并且自动打开和连接数据库 它没有独立打开数 ...

  7. arraylist与List<>

    arraylist 不用规定 类型 list<>用规定类型 推荐list 这个编译检查类型  出错率低 代码质量高

  8. Windows命令行(DOS命令)教程-8 (转载)http://arch.pconline.com.cn//pcedu/rookie/basic/10111/15325_7.html

    15. pass [功能] 设定DOS寻找.COM..EXE..BAT文件的所在目录 [格式] path=[[drive:]path[;-]]或path [说明] 只打path没有参数时,只显示环境变 ...

  9. PL/SQL中字符串变量的分割转化

    在编写PL/SQL时,有时候我们需要处理这样一个输入的变量,它的格式是由多个值通过分隔符组成的字符串,如“1,2,3”,我们需要将这个变量加入到我们的SQL中,形成诸如in('1','2','3')的 ...

  10. C#传值

    C#若不加限制传值时自带的类型为值传递,自创的类型为引用传递 using System; using System.Collections.Generic; using System.Linq; us ...