这题点的个数(<=50)有限, 所以可以纯暴力DFS去搜索

//#pragma comment(linker, "/STACK:16777216") //for c++ Compiler
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cmath>
#include <stack>
#include <string>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <vector>
#include <algorithm>
#define Max(a,b) (((a) > (b)) ? (a) : (b))
#define Min(a,b) (((a) < (b)) ? (a) : (b))
#define Abs(x) (((x) > 0) ? (x) : (-(x)))
#define MOD 1000000007
#define pi acos(-1.0) using namespace std; typedef long long ll ;
typedef unsigned long long ull ;
typedef unsigned int uint ;
typedef unsigned char uchar ; template<class T> inline void checkmin(T &a,T b){if(a>b) a=b;}
template<class T> inline void checkmax(T &a,T b){if(a<b) a=b;} const double eps = 1e- ;
const int N = ;
const int M = * ;
const ll P = 10000000097ll ;
const int MAXN = ; int n;
int g[][], vis[], ans[]; void init(){
memset(g, , sizeof(g));
} bool dfs(int pos, int level){
ans[level] = pos;
if(level >= n){
return true;
}
for(int i = ; i <= n; ++i){
if(i == pos) continue;
if(!vis[i] && g[pos][i]){
vis[i] = ;
if(dfs(i, level + )){
return true;
}
vis[i] = ;
}
}
} int main(){
std::ios::sync_with_stdio(false);
int i, j, t, k, u, v, numCase = ;
cin >> t;
while(t--){
init();
cin >> n;
for(i = ; i <= n * (n - ) / ; ++i){
cin >> u >> v;
g[u][v] = ;
}
for(i = ; i <= n; ++i){
memset(vis, , sizeof(vis));
memset(ans, , sizeof(ans));
vis[i] = ;
if(dfs(i, )) break;
vis[i] = ;
}
if(i != n + ){
for(i = ; i < n; ++i){
cout << ans[i] << ' ';
}
cout << ans[n] << endl;
} else{
cout << "Impossible" << endl;
}
} return ;
}

Strange Country II 暴力dfs的更多相关文章

  1. 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 ...

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

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

  4. hihoCoder 1185 连通性·三(Tarjan缩点+暴力DFS)

    #1185 : 连通性·三 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 暑假到了!!小Hi和小Ho为了体验生活,来到了住在大草原的约翰家.今天一大早,约翰因为有事要出 ...

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

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

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

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

  7. 2018杭电多校第五场1002(暴力DFS【数位】,剪枝)

    //never use translation#include<bits/stdc++.h>using namespace std;int k;char a[20];//储存每个数的数值i ...

  8. A. The Fault in Our Cubes 暴力dfs

    http://codeforces.com/gym/101257/problem/A 把它固定在(0,0, 0)到(2, 2, 2)上,每次都暴力dfs检查,不会超时的,因为规定在这个空间上,一不行, ...

  9. HDU 5723 Abandoned country (最小生成树 + dfs)

    Abandoned country 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5723 Description An abandoned coun ...

随机推荐

  1. JetBrains PhpStorm 使用

    · 在左侧显示当前文件位置 在左侧显示当前文件位置 alt + F1 在选择第1个在文件夹显示文件 在文件标签上 ctrl + 鼠标左键 或者 alt + F1 在选择第8个显示当前文件的函数,变量 ...

  2. 2.PHP 教程_PHP 安装

    您需要做什么? 找一个支持PHP和MySQL的主机 在您自己的PC机上安装web服务器,然后安装PHP和MySQL 使用支持PHP的Web的主机 如果您的服务器支持PHP,那么您不需要做任何事情. 只 ...

  3. [LeetCode]题解(python):069-Sqrt(x)

    题目来源: https://leetcode.com/problems/sqrtx/ 题意分析: 实现一个整型的开根. 题目思路: 利用牛顿迭代法可以求解.首先讲数据类型转成浮点数,然后选取初始值为n ...

  4. google浏览器的安装

    很简单,命令行下安装命令:apt-get install google-chrome-stable(如安装不成功,输入apt-get -f install进行修复依赖,之后在菜单里就可以看到图标了) ...

  5. IOS 表视图(UITableVIew)的使用方法(7)表视图的编辑功能(拖拉调整排序位置)

    除了每个单元行左边的删除和新增图标,UITableView还支持在单元行的右侧显示一个供用户拖拉调整排序位置的控件. 不过如果要显示此控件,UITableView的数据源需要实现以下的方法. -(vo ...

  6. 用于展现图表的50种JavaScript库

    在很多项目中都会有在前端展现数据图表的需求,而在开发过程中,开发者往往会使用一些JavaScript库,从而更有效地达到想要的目标.最近,TechSlide上的一篇文章总结了50种用于展现图表的Jav ...

  7. CSS的float与clear

    首先要知道,div是块级元素,在页面中独占一行,自上而下排列,也就是传说中的流.如下图: 可以看出,即使div1的宽度很小,页面中一行可以容下div1和div2,div2也不会排在div1后边,因为d ...

  8. BZOJ 1806: [Ioi2007]Miners 矿工配餐( dp )

    dp... ------------------------------------------------------------------------------- #include<cs ...

  9. C# Socket select模型

    http://www.cnblogs.com/Clingingboy/archive/2011/07/04/2097806.html http://www.cnblogs.com/RascallySn ...

  10. Properties的使用

    Properties的各种相对路径,绝对路径的用法 所在包: package com.test.javaAPI.collections; import java.io.File; import jav ...