Strange Country II
2013-11-19 19:27
274 查看
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=37035#problem/K
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
Sample Output
代码:
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
3 1 2 1 2 3 1 2 1 3 2 3
Sample Output
1 1 2 1 2 3
代码:
#include <iostream> #include<stdio.h> #include<string.h> #define M 107 using namespace std; int vis[M],path[M]; int map[M][M]; int n; int flag=0; void dijkstra(int x,int step) { path[step]=x; if(step==n) { flag=1; return; } if(flag==1) return; for(int i=1;i<=n;i++) { if(map[x][i]==1&&vis[i]==0) { vis[i]=1; dijkstra(i,step+1); if(flag==1) return; vis[i]=0; } } } int main() { int t; while(cin>>t) { while(t--) { cin>>n; memset(vis,0,sizeof(vis)); memset(map,0,sizeof(map)); int m=n*(n-1)/2; flag=0; while(m--) { int a,b; scanf("%d%d",&a,&b); if(map[a][b]==0) map[a][b]=1; } for(int i=1;i<=n ;i++) { vis[i]=1; dijkstra(i,1); if(flag==1) { for(int j=1;j<n;j++) { cout<<path[j]<<" "; } cout<<path <<endl; break; } vis[i]=0; } if(flag==0) cout<<"Impossible"<<endl; } } return 0; }
相关文章推荐
- zoj 3332 Strange Country II(DFS或者网上哈密尔顿图解决)
- ZOJ 3322 Strange Country II(DFS)
- ZOJ 3332 Strange Country II暴力DFS
- Strange Country II 暴力dfs
- ZOJ 3332 Strange Country II(竞赛图中的哈密顿路)
- Strange Country II(暴力DFS判有向图的哈密顿通路)
- ZOJ 3332 Strange Country II
- ZOJ 3332 Strange Country II
- ZOJ 3332 Strange Country II
- Strange Country II
- zoj3332——Strange Country II
- ZOJ 3332 Strange Country II (竞赛图构造哈密顿通路)
- ZOJ 3332 Strange Country II【DFS】
- ZOJ-3332-Strange Country II【7th浙江省赛】【dfs】
- zoj 3332 Strange Country II (dfs)
- zoj 3332-Strange Country II-dfs
- ZOJ3332-Strange Country II
- zoj 3332 Strange Country II (双塔dp)
- Strange Country II ( 简单的dfs,但是要注意细节)
- zoj 3332 Strange Country II