ZOJ 3332 Strange Country II 判断有向图联通
2013-11-18 09:18
239 查看
点击打开链接
Strange Country II
Time Limit: 1 Second
Memory Limit: 32768 KB Special Judge
You want to visit a strange country. There are n cities in the country. Cities are numbered from 1 ton. The unique way to travel in the country is taking planes.Strangely, in this strange country, for every two citiesA andB,
there is a flight from A to B or fromB 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.ThenT test cases follow. Each test case starts with a line containing an integern (0 <n
<= 100), which is the number of cities.Each of the nextn * (n - 1) / 2 lines contains 2 numbersA,
B (0 <A, B <= n, A != B), meaning that there is a flight from cityA 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
比赛的时候抓不住重点,搞不清此题的真正意图,以至于我先用mst求解,在用朱刘算法也不行。到头来,就是一道dfs深搜。汗!!!!!!
Strange Country II
Time Limit: 1 Second
Memory Limit: 32768 KB Special Judge
You want to visit a strange country. There are n cities in the country. Cities are numbered from 1 ton. The unique way to travel in the country is taking planes.Strangely, in this strange country, for every two citiesA andB,
there is a flight from A to B or fromB 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.ThenT test cases follow. Each test case starts with a line containing an integern (0 <n
<= 100), which is the number of cities.Each of the nextn * (n - 1) / 2 lines contains 2 numbersA,
B (0 <A, B <= n, A != B), meaning that there is a flight from cityA 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
比赛的时候抓不住重点,搞不清此题的真正意图,以至于我先用mst求解,在用朱刘算法也不行。到头来,就是一道dfs深搜。汗!!!!!!
#include<stdio.h> #include<string.h> #define M 107 int vis[M],g[M][M],path[M]; int n,flag; void dfs(int u,int step) { path[step]=u; if(step==n){flag=1;return;} for(int i=1;i<=n;i++) { if(!vis[i]&&g[u][i]) { vis[i]=1; dfs(i,step+1); if(flag)return; vis[i]=0; } } } int main() { //freopen("in.txt","r",stdin); int t; scanf("%d",&t); while(t--) { memset(g,0,sizeof(g)); memset(vis,0,sizeof(vis)); scanf("%d",&n); int m=n*(n-1)/2,a,b; while(m--) { scanf("%d%d",&a,&b); g[a][b]=1; } flag=0; for(int i=1;i<=n;i++) { vis[i]=1; dfs(i,1); if(flag) { for(int i=1;i<n;i++) printf("%d ",path[i]); printf("%d\n",path ); break; } vis[i]=0; } if(!flag)printf("Impossible\n"); } return 0; }
相关文章推荐
- 矩阵快速幂ZOJ - 3497(矩阵在图的联通
- 判断手机号码属于哪个地区 哪个运营商(移动、联通、电信、)
- delphi 域名转ip并判断ip是否可以联通
- WA 裸强联通判断 hdu Problem-1269 迷宫城堡
- (判断是否为弱联通分量) poj 2762
- ZOJ 3332 Strange Country II (竞赛图构造哈密顿通路)
- ZOJ 3332 Strange Country II
- POJ2942-Knights of the Round Table (双联通+判断奇环)
- poj2942—Knights of the Round Table(点双联通分量的求解+染色法判断奇圈)
- ZOJ 3332 Strange Country II(图论)
- ZOJ 3332 Strange Country II【DFS】
- ZOJ3332-竞赛图中的哈密顿路
- hdu3478(图的联通+二分图判断)
- poj1979 Red and Black(dfs判断联通分量)
- zoj 2587 Unique Attack (判断最小割是否唯一)
- 矩阵快速幂ZOJ - 3497(矩阵在图的联通
- zoj 2100 Seeding 【DFS 判断是否能经过每个点一次遍历完整个图】
- zoj 3332 竞赛图哈密顿路
- 一句话判断网络是否联通
- POJ 2762判断单联通(强连通缩点+拓扑排序)