ZOJ 3332 Strange Country II
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 A, B (0 < A, B <= n, A != 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的更多相关文章
- ZOJ 3332 Strange Country II (竞赛图构造哈密顿通路)
链接:http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=3332 本文链接:http://www.cnblogs.com/Ash-l ...
- 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 ...
- Strange Country II 暴力dfs
这题点的个数(<=50)有限, 所以可以纯暴力DFS去搜索 //#pragma comment(linker, "/STACK:16777216") //for c++ Co ...
- Zoj3332-Strange Country II(有向竞赛图)
You want to visit a strange country. There are n cities in the country. Cities are numbered from 1 t ...
- 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 ...
- zoj 3356 Football Gambling II【枚举+精度问题】
题目: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3356 http://acm.hust.edu.cn/vjudge/ ...
- zoj 3620 Escape Time II
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4744 Escape Time II Time Limit: 2 Seconds ...
- 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 ...
- ZOJ 2674 Strange Limit
欧拉函数. #include<iostream> #include<stdio.h> #include<string.h> #include<algorith ...
随机推荐
- jsp版本的环境变量集合
System.out.println("Protocol: " + request.getProtocol());System.out.println("Scheme: ...
- [svc][jk]gpu温度监测
在使用TensorFlow跑深度学习的时候,经常出现显存不足的情况,所以我们希望能够随时查看GPU时使用率.如果你是Nvidia的GPU,那么在命令行下,只需要一行命令就可以实现. 1. 显示当前GP ...
- 第一个EJB示例
FirstEJB2.0.zip Eclipse + JBoss 5.1 Ejb3Example.zip Eclipse + JBoss 7.1 注意点: 1. jboss 增加用户: D:\DevPr ...
- Unity 扩展编辑器
扩展Inspector界面 继承自Editor,添加CustomEditorAttribute,传入定制的类型 定制显示的类型要求: 类型中所有的public 字段都会自动暴露给Inspector编辑 ...
- 【Android】利用Fiddler进行抓包详解教程。抓取接口以及数据,可以抓真实安卓手机或者模拟器。
大家都知道抓包的方法很多.我这里给大家介绍介绍一种,利用fiddler进行抓包,当然比如Wireshark也可以抓包,我们这里不做介绍.我这里演示的是fiddler+天天模拟器,当然真实安卓手机也是一 ...
- CentOS6.2下Qt5.1.0无法输入中文
因为在程序中需要在界面上输入中文,但是系统是英文系统,没有预装中文输入法,于是从网上搜了一下输入法的安装,但是输入法安装好之后,可以再系统中输入中文,但是却无法再Qt中输入中文,只能继续找解决办法 安 ...
- Action方法调用
一.Action访问路径 Action的访问路径是由struts.xml文件中配置的Action所在包的命名空间,Action的名字和常struts.action.extension共同决定的 例如: ...
- Zookeeper的结构和命令
1. Zookeeper的特性 1.Zookeeper:一个leader,多个follower组成的集群. 2.全局数据一致:每个server保存一份相同的数据副本,client无论连接到哪个serv ...
- vSphereClient向ESXi主机分配许可证
ESXi服务器需要使用VMwarevSphereClient进行管理(7.0+版本可以通过浏览器进行管理)在VMware vSphere client可以方便的创建.管理虚拟机,并分配相应的资源.要能 ...
- Eqs - poj 1840(hash)
题意:对于方程:a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0 ,有xi∈[-50,50], xi != 0, any i∈{1,2,3,4,5}. 现在给出a1,a2,a3, ...