ZOJ 3332 Strange Country II
来源:互联网 发布:mac还原hosts文件 编辑:程序博客网 时间:2024/05/16 19:34
Description
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
3121 231 21 32 3
Sample Output
11 21 2 3
边这么多,随便跑跑就能找到路的,所以直接dfs就好了,为了输出路径所以倒着比较好。
#include<cmath>#include<queue>#include<stack>#include<vector>#include<string>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<functional>using namespace std;typedef long long LL;const int maxn=1e2+10;int T,n,x,y,vis[maxn];vector<int> t[maxn];bool dfs(int x,int y){if (y==n){ printf("%d",x); return true;}vis[x]=1;for (int i=0;i<t[x].size();i++){if (!vis[t[x][i]]){if (dfs(t[x][i],y+1)){printf(" %d",x); return true;}}}vis[x]=0;return false;}int main(){scanf("%d",&T);while (T--){scanf("%d",&n);for (int i=1;i<=n;i++) t[i].clear();for (int i=1;i+i<=n*(n-1);i++){scanf("%d%d",&x,&y);t[y].push_back(x);}int flag=0;memset(vis,0,sizeof(vis));for (int i=1;i<=n;i++) {if (dfs(i,1)) {flag=1; break;}}if (!flag) printf("Impossible");printf("\n");}return 0;}
温故知新#include<map>#include<cmath> #include<queue> #include<vector>#include<cstdio> #include<cstring> #include<algorithm> using namespace std;#define ms(x,y) memset(x,y,sizeof(x)) #define rep(i,j,k) for(int i=j;i<=k;i++) #define per(i,j,k) for(int i=j;i>=k;i--) #define loop(i,j,k) for (int i=j;i!=-1;i=k[i]) #define inone(x) scanf("%d",&x) #define intwo(x,y) scanf("%d%d",&x,&y) #define inthr(x,y,z) scanf("%d%d%d",&x,&y,&z) typedef long long LL;const int low(int x) { return x&-x; }const int mod = 1e9 + 7;const int N = 1e2 + 10;const int M = 1 << 14;int T, n, m, x, y, d[N][N], o[N], v[N];bool dfs(int x, int y){o[y] = x; v[x] = 1;if (y == n) return true;rep(i, 1, n){if (v[i] || !d[x][i]) continue;if (dfs(i, y + 1)) return true;}v[x] = 0;return false;}int main(){for (inone(T); T--;){inone(n); m = n*(n - 1) / 2;ms(d, 0);ms(v, 0);rep(i, 1, m){intwo(x, y);d[x][y] = 1;}bool flag = false;rep(i, 1, n) if (flag |= dfs(i, 1)) break;if (flag) rep(i, 1, n) printf("%d%s", o[i], i == n ? "\n" : " ");else puts("Impossible");}return 0;}
- ZOJ 3332 Strange Country II
- zoj Strange Country II 3332
- ZOJ 3332 Strange Country II
- ZOJ 3332 Strange Country II
- zoj 3332 Strange Country II
- ZOJ 3332 Strange Country II
- ZOJ 3332 Strange Country II
- ZOJ 3332 Strange Country II暴力DFS
- zoj 3332-Strange Country II-dfs
- zoj 3332 Strange Country II (dfs)
- zoj 3332 Strange Country II DFS+回溯
- zoj 3332 Strange Country II (双塔dp)
- ZOJ 3332Strange Country II(图论)
- ZOJ 3332 Strange Country II【DFS】
- ZOJ 3332 Strange Country II 判断有向图联通
- ZOJ-3332-Strange Country II【7th浙江省赛】【dfs】
- ZOJ 3322 Strange Country II(DFS)
- ZOJ 3332 Strange Country II(竞赛图中的哈密顿路)
- Android利用dimens.xml进行适配,使用代码生成不同的dimens.xml文件
- TBL自定义函数EMA
- iOS9项目打包上线
- linux面试之--堆、栈、自由存储区、全局/静态存储区和常量存储区
- Unity3D操作技巧
- ZOJ 3332 Strange Country II
- 学习数据结构和算法动态可视化工具
- AFHTTPRequestOperation做文件下载的断点续传
- CSS各浏览器兼容方法
- ECMAScript和JavaScript的关系
- spring+springMvc+mybatis 记录备忘
- Windows操作系统中常见的可执行映像(.exe)和dll
- NSURLErrorDomain Code=-999
- 管理系统