Zoj3332-Strange Country II(有向竞赛图)
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
解析: 裸的有向竞赛图,有向竞赛图要满足的条件是有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(有向竞赛图)的更多相关文章
- ZOJ 3332 Strange Country II
Strange Country II Time Limit: 1 Second Memory Limit: 32768 KB Special Judge You want to v ...
- 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 ...
- ZOJ 3332 Strange Country II (竞赛图构造哈密顿通路)
链接:http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=3332 本文链接:http://www.cnblogs.com/Ash-l ...
- Strange Country II 暴力dfs
这题点的个数(<=50)有限, 所以可以纯暴力DFS去搜索 //#pragma comment(linker, "/STACK:16777216") //for c++ Co ...
- 【转】欧拉回路&特殊图下的哈密顿回路题集
转自:http://blog.csdn.net/shahdza/article/details/7779385 欧拉回路[HDU]1878 欧拉回路 判断3018 Ant Trip 一笔画问题1116 ...
- 【转载】图论 500题——主要为hdu/poj/zoj
转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...
- hdu图论题目分类
=============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many ...
- HDU图论题单
=============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many ...
- 【HDOJ图论题集】【转】
=============================以下是最小生成树+并查集====================================== [HDU] How Many Table ...
随机推荐
- xm学习笔记
1关于静态网页的制作 html主要负责页面的结构+css页面的美观+js与用户的交互. 2html 有标签体的标签: <p></p> <span></spa ...
- 关于bootstrap--表单控件(disabled表单禁用、显示表单验证的样式)
1.disabled: (1)在input中加入disabled可使表单禁用,如图: <input class="form-control input-lg" id=&quo ...
- 基于PCA和SVM的人脸识别
程序中采用的数据集是ORL人脸库,该人脸库共有400副人脸图像,40人,每人10幅,大小为112*92像素,同一个人的表情,姿势有少许变化. 程序的流程主要分为三部分,数据的预处理(PCA降维和规格化 ...
- qt tablewidget中单个和批量删除代码如下(部分)截图如下
def coltable(self):#行删除 row=self.downwidget.currentRow() select=self.downwidget.isItemSelected ...
- python高级编程:有用的设计模式3
# -*- coding: utf-8 -*-__author__ = 'Administrator'#python高级编程:有用的设计模式#访问者:有助于将算法从数据结构中分离出来"&qu ...
- python高级编程:有用的设计模式1
# -*- coding: utf-8 -*-__author__ = 'Administrator'#python高级编程:有用的设计模式#设计械是可复用的,某种程序上它对软件设计中觉问题提供的语言 ...
- gzcompress, gzencode, gzdeflate三个压缩函数的对比
PHP的自带的函数中,有三个压缩相关的函数:gzcompress.gzencode.gzdeflate,下面我们通过一段程序,来比较一下这三个函数的压缩比.代码:$string = "8ae ...
- Performance Tuning guide 翻译 || 前言
CSDN 对格式支持比較弱,能够到http://user.qzone.qq.com/88285879/blog/1399382878 看一致的内容. 前言Preface 包含例如以下几个小节 l Au ...
- [转] unix/linux下线程私有数据实现原理及使用方法
在维护每个线程的私有数据的时候,我们可能会想到分配一个保存线程数据的数组,用线程的ID作为数组的索引来实现访问,但是有一个问题是系统生成的线程 ID不能保证是一个小而连续的整数,并且用数组实现的时候 ...
- 主运行循环main run loop的一些理解
应用主运行循环负责处理所有用户相关的事件.UIApplication对象在应用启动时安装主运行循环并且使用此循环去处理事件和处理基于视图的界面更新.正如名字所表明的,该主运行循环是在应用的主线程app ...