Strange Country II 暴力dfs
这题点的个数(<=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的更多相关文章
- 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
Strange Country II Time Limit: 1 Second Memory Limit: 32768 KB Special Judge You want to v ...
- ZOJ 3332 Strange Country II (竞赛图构造哈密顿通路)
链接:http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=3332 本文链接:http://www.cnblogs.com/Ash-l ...
- hihoCoder 1185 连通性·三(Tarjan缩点+暴力DFS)
#1185 : 连通性·三 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 暑假到了!!小Hi和小Ho为了体验生活,来到了住在大草原的约翰家.今天一大早,约翰因为有事要出 ...
- Zoj3332-Strange Country II(有向竞赛图)
You want to visit a strange country. There are n cities in the country. Cities are numbered from 1 t ...
- UVA129 暴力dfs,有许多值得学习的代码
紫书195 题目大意:给一个困难的串,困难的串的定义就是里面没有重复的串. 思路:不需要重新对之前的串进行判重,只需要对当前的加入的字符进行改变即可. 因为是判断字典序第k个的字符串,所以要多一个全局 ...
- 2018杭电多校第五场1002(暴力DFS【数位】,剪枝)
//never use translation#include<bits/stdc++.h>using namespace std;int k;char a[20];//储存每个数的数值i ...
- A. The Fault in Our Cubes 暴力dfs
http://codeforces.com/gym/101257/problem/A 把它固定在(0,0, 0)到(2, 2, 2)上,每次都暴力dfs检查,不会超时的,因为规定在这个空间上,一不行, ...
- HDU 5723 Abandoned country (最小生成树 + dfs)
Abandoned country 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5723 Description An abandoned coun ...
随机推荐
- 11.PHP 教程_PHP Switch 语句
switch 语句用于根据多个不同条件执行不同动作. PHP Switch 语句 如果您希望有选择地执行若干代码块之一,请使用 switch 语句. 语法 switch (n) { case labe ...
- ruby2.0(rails)以后版本的debug
很喜欢RUBY(RAILS),认识也好久好久了,但是说实话,从来没用ROR写过什么东西,都是小打小闹,做些自娱自乐的东西,碰到什么问题,基本仔细看看,加上几个LOG就找到原因了,从来没想过要DEBUG ...
- Java解析器
http://www.infoq.com/cn/articles/HIgh-Performance-Parsers-in-Java-V2?utm_source=infoq&utm_medium ...
- 使用Protel99 SE 拼板的详细图解(新加队列粘贴方法)
很多网友跟我沟通,提到我上次博文中的protel99se中做拼板图解过于简略,应大家的有求,重新修改了操作图示. 首先打开PCB文档.如图所示:电路板的原点并没有在边上,为了操作方便和规范,先把有点设 ...
- Json.Net系列教程 3.Json.Net序列化和反序列化设置
原文 Json.Net系列教程 3.Json.Net序列化和反序列化设置 上节补充 首先补充一点,Json.Net是支持序列化和反序列化DataTable,DataSet,Entity Framewo ...
- Servlet、SPringMVC、Struts等防止表单反复提交的多种处理方法
第一种处理方法(非拦截器): 眼下这样的方法不建议,由于JSP规范不建议写JAVA代码.这样的能够方便另外一种处理方法的理解,另外一种方法引入拦截器的思想,原理基本一样,模仿Struts的Token机 ...
- 【在网页中获取截图数据】Chrome和Firefox下的实战经验
[转载自我在segmentfault的专栏:https://segmentfault.com/a/1190000004584071] 最近在实现一个功能,需求如下: 前提:当前页面无弹窗 页面任意位置 ...
- 简单的前端js+ajax 购物车框架(入门篇)
其实,一直想把自己写的一些js给总结下,也许是能力有限不能把它完美结合起来.只能自己默默的看着哪些代码,无能为力. 今天在公司实在没有事做,突然就想到写下商城的购物车的前端框架,当然我这里只有购物车的 ...
- 数据库基础(子查询练习、链接查询(join on 、union)及其练习)
子查询练习一:查询销售部里的年龄大于35岁的人的所有信息 练习二:将haha表中部门的所有数字代码转换为bumen表中的字符串显示 练习三:将haha表中部门的所有数字代码转换为bumen表中的字符串 ...
- 【Linux命令】mysql数据库常用操作命令
#数据库操作: #建立数据库 create database data_name #删除数据库 drop database data_name #查看: show databases; #表操作: # ...