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 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> 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的更多相关文章
- Strange Country II 暴力dfs
这题点的个数(<=50)有限, 所以可以纯暴力DFS去搜索 //#pragma comment(linker, "/STACK:16777216") //for c++ Co ...
- ZOJ 3332 Strange Country II
Strange Country II Time Limit: 1 Second Memory Limit: 32768 KB Special Judge You want to v ...
- ZOJ 2475 Benny's Compiler(dfs判断有向图给定点有没有参与构成环)
B - Benny's Compiler Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu ...
- ZOJ 3332 Strange Country II (竞赛图构造哈密顿通路)
链接:http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=3332 本文链接:http://www.cnblogs.com/Ash-l ...
- CSU 1660 K-Cycle(dfs判断无向图中是否存在长度为K的环)
题意:给你一个无向图,判断是否存在长度为K的环. 思路:dfs遍历以每一个点为起点是否存在长度为k的环.dfs(now,last,step)中的now表示当前点,last表示上一个访问的 点,step ...
- DFS应用——遍历有向图+判断有向图是否有圈
[0]README 0.1) 本文总结于 数据结构与算法分析, 源代码均为原创, 旨在 理解 "DFS应用--遍历有向图+判断有向图是否有圈" 的idea 并用源代码加以实现 : ...
- UVA129 暴力dfs,有许多值得学习的代码
紫书195 题目大意:给一个困难的串,困难的串的定义就是里面没有重复的串. 思路:不需要重新对之前的串进行判重,只需要对当前的加入的字符进行改变即可. 因为是判断字典序第k个的字符串,所以要多一个全局 ...
- Zoj3332-Strange Country II(有向竞赛图)
You want to visit a strange country. There are n cities in the country. Cities are numbered from 1 t ...
- hdu 1325 判断有向图是否为树
题意:判断有向图是否为树 链接:点我 这题用并查集判断连通,连通后有且仅有1个入度为0,其余入度为1,就是树了 #include<cstdio> #include<iostream& ...
随机推荐
- 【Python】【正则】
1. 正则表达式基础 1.1. 简单介绍 正则表达式并不是Python的一部分.正则表达式是用于处理字符串的强大工具,拥有自己独特的语法以及一个独立的处理引擎,效率上可能不如str自带的方法,但功能十 ...
- 智能边缘计算,让IoT有大智慧
丹棱君有话说:什么是智能边缘计算(Edge Computing)?别着急,它可是与你与我都有着千丝万缕的联系.物联网(IoT)的概念早已飞入寻常百姓家,在日常生活中的许多场景发挥着“智能”作用.比如, ...
- 《剑指offer》第三十二题(之字形打印二叉树)
// 面试题32(三):之字形打印二叉树 // 题目:请实现一个函数按照之字形顺序打印二叉树,即第一行按照从左到右的顺 // 序打印,第二层按照从右到左的顺序打印,第三行再按照从左到右的顺序打印, / ...
- HTTP URL 字符转义 字符编码 、 RFC 3986编码规范
一.为什么要编码转义 通常如果一样东西需要编码,说明这样东西并不适合传输.原因多种多样,如Size过大,包含隐私数据,对于Url来说,之所以要进行编码,是因为Url中有些字符会引起歧义. 例如Url参 ...
- Java 本地环境设置
如果你希望在你的本地环境中设置 Java 程序环境,下面的这部分将会指导你在你的本地计算机上下载和设置 Java 环境.你可以按照下面的步骤进行. Java SE 目前是免费下载的,你可以通过单击下面 ...
- 『cs231n』绪论
笔记链接 cs231n系列所有图片笔记均拷贝自网络,链接如上,特此声明,后篇不再重复. 计算机视觉历史 总结出视觉两个重要结论:1.基础的视觉神经识别的是简单的边缘&轮廓2.视觉是分层的 数据 ...
- Docker+Hadoop+Hive+Presto 使用Docker部署Hadoop环境和Presto
Background 一. 什么是Presto Presto通过使用分布式查询,可以快速高效的完成海量数据的查询.如果你需要处理TB或者PB级别的数据,那么你可能更希望借助于Hadoop和HDFS来完 ...
- Ionic构建打包apk出现的问题集合
当我们写完 ionic 项目准备打包成 apk 时(比如执行 ionic cordova platform add android 或者 ionic cordova build android 等命令 ...
- 0001——初涉MySQL
MySQL是一个开源的关系型数据库管理系统. MySQL分为社区版本和企业版 MySQL安装方式: 1.MSI安装(Windows Installer) 2.ZIP安装 选择安装类型: 1.T ...
- 微信小程序绑定数据(微信小程序交流群:604788754)
在小程序开发中,用js绑定数据时,明明没报错,但页面没有显示绑定的数据内容.这有可能是相对应的js绑定数据的一些变量名字的问题.遇到这种情况.可以从新建立一个新的demo页面.把问题页面相对应的js. ...