ZOJ 3332Strange Country II(图论)
来源:互联网 发布:淘宝售假进货凭证截图 编辑:程序博客网 时间:2024/05/17 04:04
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3332
给出一个有向图,要求输出一条路可以一次经过每个点,每个点只能经过一次。乍一看以为是欧拉回路,仔细想想用dfs的递归层数来判断这条路上最长的线路,用ans保存路径
#include<iostream>#include<cstdio>#include<set>#include<string>#include<string.h>#include<cstring>#include<vector>#include<map>#include<queue>#include<stack>#include<cctype>#include<algorithm>#include<sstream>#include<utility>#include<cmath>#define mt(a) memset(a,0,sizeof (a))#define fl(a,b,c) fill(a,b,c)#define SWAP(a,b,t) (t=a,a=b,b=t)#define inf 1000000000+7using namespace std;typedef long long ll;vector<int>G[120];int vis[120];int n;int dis[120];int cot = 0;void dfs(int node, int k){cot = max(cot, k);dis[k] = node;if (cot == n)return;vector<int>::iterator po;for (po = G[node].begin(); po != G[node].end(); po++){if (!vis[*po]){vis[*po] = 1;dfs(*po, k + 1);if (cot == n)return;vis[*po] = 0;}}}int main(){int T;cin >> T;while (T--){scanf("%d", &n);for (int i = 0; i < 120; i++)G[i].clear();mt(dis);int m = (n - 1)*n / 2;for (int i = 0; i < m; i++){int x, y;scanf("%d %d", &x, &y);G[x].push_back(y);}int flag = 0;for (int i = 1; i <= n; i++){cot = 1;memset(vis, 0, sizeof vis);vis[i] = 1;dfs(i, 1);if (cot == n){flag = 1;break;}}if (!flag)printf("Impossible\n");else{for (int i = 1; i <= n; i++){if (i != 1)printf(" ");printf("%d", dis[i]);}printf("\n");}}return 0;}
0 0
- ZOJ 3332Strange Country II(图论)
- 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 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(竞赛图中的哈密顿路)
- 我所知道的经济学
- 深入探讨javascript函数式编程
- 程序员面试金典 2.4 链表分割
- CodeForces 520E Pluses everywhere
- Activiti教程二
- ZOJ 3332Strange Country II(图论)
- 关于WaitForMultipleObjects函数监测事件状态的问题
- word目录编辑
- js 创建对象的几种方式
- 1032. 挖掘机技术哪家强(20)
- 台湾大学林轩田老师机器学习技法:内容简介
- Java Reflection(六):Getters and Setters
- confluence创建文件报错 can not create to file /tmp/xxx Errcode:28,引出的 df命令高级用法
- Ubuntu14下OpenCV3和QT5的安装及联合使用