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>

using namespace std;

#include <string.h>

int map[][];

int vis[];

int re[];

int n,flag;

int lu;

void dfs(int a)

{

    if(lu==n)

    {

        flag=;

        return;

    }

    for(int i=;i<=n;i++)

    {

        if(!vis[i]&&map[a][i])

        {

            re[lu]=i;

            lu++;

            vis[i]=;

            dfs(i);

            if(flag)return;

            vis[i]=;

            lu--;

        }

    }

    return;

}

int main()

{

    int t;

    cin>>t;

    while (t--) {

        cin>>n;

        if(n==)

            cout<<<<endl;

        else

        {

            memset(map, , sizeof(map));

            memset(re, , sizeof(re));

            memset(vis, , sizeof(vis));

            flag=;

            for(int i=;i<n*(n-)/;i++)

            {

                int s,d;

                cin>>s>>d;

                map[s][d]=;

            }

            for(int i=;i<=n;i++)

            {

                lu=;

                re[lu]=i;

                lu++;

                vis[i]=;

                dfs(i);

                lu--;

                if(flag)break;

                vis[i]=;

            }

            if(flag)

            {

                for(int i=;i<n;i++)

                {

                    if(i)

                        cout<<" "<<re[i];

                    else

                        cout<<re[i];

                }

                cout<<endl;

            }

            else

                cout<<"Impossible"<<endl;

        }

    }

    return ;

}

K - Strange Country II 暴力dfs判断有向图是否连通//lxm的更多相关文章

  1. Strange Country II 暴力dfs

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

  2. ZOJ 3332 Strange Country II

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

  3. ZOJ 2475 Benny's Compiler(dfs判断有向图给定点有没有参与构成环)

    B - Benny's Compiler Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu ...

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

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

  5. CSU 1660 K-Cycle(dfs判断无向图中是否存在长度为K的环)

    题意:给你一个无向图,判断是否存在长度为K的环. 思路:dfs遍历以每一个点为起点是否存在长度为k的环.dfs(now,last,step)中的now表示当前点,last表示上一个访问的 点,step ...

  6. DFS应用——遍历有向图+判断有向图是否有圈

    [0]README 0.1) 本文总结于 数据结构与算法分析, 源代码均为原创, 旨在 理解 "DFS应用--遍历有向图+判断有向图是否有圈" 的idea 并用源代码加以实现 : ...

  7. UVA129 暴力dfs,有许多值得学习的代码

    紫书195 题目大意:给一个困难的串,困难的串的定义就是里面没有重复的串. 思路:不需要重新对之前的串进行判重,只需要对当前的加入的字符进行改变即可. 因为是判断字典序第k个的字符串,所以要多一个全局 ...

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

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

  9. hdu 1325 判断有向图是否为树

    题意:判断有向图是否为树 链接:点我 这题用并查集判断连通,连通后有且仅有1个入度为0,其余入度为1,就是树了 #include<cstdio> #include<iostream& ...

随机推荐

  1. Java中的垃圾回收机制

    1. 垃圾回收的意义 在C++中,对象所占的内存在程序结束运行之前一直被占用,在明确释放之前不能分配给其它对象:而在Java中,当没有对象引用指向原先分配给某个对象的内存时,该内存便成为垃圾.JVM的 ...

  2. 怎么彻底删除2345的各种顽固Process

    清晨打开电脑,都是2345的不良新闻,心情不美美哒 2345如何卸载? “C:\Windows\System32\drivers”目录删除Mslmedia.sys 开始-运行-cmd输入“sc del ...

  3. 使用 CSS 选择器从网页中提取数据

    在 R 中,关于网络爬虫最简单易用的扩展包是 rvest.运行以下代码从 CRAN 上安装:install.packages("rvest")首先,加载包并用 read_html( ...

  4. [ios]sqlite轻量级数据库学习连接

    SQLLite (一)基本介绍 http://blog.csdn.net/lyrebing/article/details/8224431 SQLLite (二) :sqlite3_open, sql ...

  5. php 四种基础排序

    1. 冒泡排序算法 *     思路分析:法如其名,就是像冒泡一样,每次从数组当中 冒一个最大的数出来.  *     比如:2,4,1    // 第一次 冒出的泡是4  *             ...

  6. PHP消息队列之Beanstalk

    Beanstalk,一个高性能.轻量级的分布式内存队列

  7. CSS实现和选择器

    CSS实现和选择器 本课内容: 一.实现CSS四种方式 1,每个html标签中都有一个style样式属性,该属性的值就是css代码.(针对一个标签)2,使用style标签的方式. 一般都定义在head ...

  8. 表达谱(DGE)测序与转录组测序的差别

    DGE-seq和普通的transcriptomic profiling相比较有什么不同,有什么特点? DGE就是用酶将mRNA切断,只使用靠近poly A的一小段RNA去测序. #1 由于不是测定mR ...

  9. codeforces 576c// Points on Plane// Codeforces Round #319(Div. 1)

    题意:有n个点,找到一个顺序走遍这n个点,并且曼哈顿距离不超过25e8. 由于给的点坐标都在0-1e6之间.将x轴分成1000*1000,即1000长度为1块.将落在同一块的按y排序,编号为奇的块和偶 ...

  10. 『Sklearn』框架自带数据集接口

    自带数据集类型如下: # 自带小型数据集# sklearn.datasets.load_<name># 在线下载数据集# sklearn.datasets.fetch_<name&g ...