ZOJ 3332 Strange Country II
来源:互联网 发布:专业美工电脑配置 编辑:程序博客网 时间:2024/04/29 22:13
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3757
题面:
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就可以了,加一个flag标记及时返回。因为不小心忽略了每一个只访问一次这个条件,wa了一次。
代码:
#include <iostream>#include <cstdio>#include <cstring> using namespace std;bool map[105][105];bool vis[105],flag;int n,t,x,a,b;int road[105],cnt=0;void dfs(int x){//及时返回 if(flag)return;road[cnt++]=x;vis[x]=1;bool sign=true;//检验是否找到 for(int i=1;i<=n;i++){if(vis[i]==0){sign=false;break;}}//已经找到 if(sign){flag=true;return;}else{for(int i=1;i<=n;i++){//注意该点要未被访问过 if(map[x][i]&&!vis[i]){ dfs(i); //注意还原 vis[i]=0; cnt--;}}}}int main(){scanf("%d",&t);while(t--){flag=false;memset(map,0,sizeof(map));scanf("%d",&n);x=n*(n-1)/2;//读入操作 for(int i=0;i<x;i++){ scanf("%d%d",&a,&b); map[a][b]=1;}for(int i=1;i<=n&&!flag;i++){cnt=0;memset(vis,0,sizeof(vis));dfs(i);}if(flag){printf("%d",road[0]);for(int i=1;i<n;i++)printf(" %d",road[i]);printf("\n");}else printf("Impossible\n");}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(竞赛图中的哈密顿路)
- jQuery实现tabs(标签页/选项卡)Demo
- Android studio 使用 facebook 开源图片库 Fresco
- 在U盘上安装RaspberryPI
- 畅通工程再续
- UI开发----简单通讯录的实现
- ZOJ 3332 Strange Country II
- h264解码小程序
- 【我就看看不说话】UIAppearance
- Netty4.0学习笔记系列之一:Server与Client的通讯
- SQLite 日期类型(转)
- ViewPager滑动监听的参数解释
- Git使用入门,使用原理解读及如何在GitLab、GitHub或者Stash上管理项目(一)
- java内存分配
- &&、||运算问题