链接:http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=3332

本文链接:http://www.cnblogs.com/Ash-ly/p/5454611.html

题意:

  给你一个N,代表含有N个点的竞赛图,接着的N * (N- 1) / 2行两个点u, v,代表存在有向边<u,v>,问是否能构造出来一个哈密顿通路.

思路:

  竞赛图一定含有哈密顿通路,不一定含有哈密顿回路.则不可能出现不存在的情况,直接构造就可以,至于方法可看我的另外一篇文章:http://www.cnblogs.com/Ash-ly/p/5452580.html.

注意:

  构造的时候从后往前寻找1或者0的时候一定是按照当前序列的顺序查找到,而不是按照点本身的顺序查找,在这里WA了几次.

代码:

 #include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector> using namespace std;
typedef long long LL;
const int maxN = ; //The arv[] length is len, insert key befor arv[index]
inline void Insert(int arv[], int &len, int index, int key){
if(index > len) index = len;
len++;
for(int i = len - ; i >= ; --i){
if(i != index && i)arv[i] = arv[i - ];
else{arv[i] = key; return;}
}
} void Hamilton(int ans[maxN + ], int map[maxN + ][maxN + ], int n){
int ansi = ;
ans[ansi++] = ;
for(int i = ; i <= n; i++){
if(map[i][ans[ansi - ]] == )
ans[ansi++] = i;
else{
int flag = ;
for(int j = ansi - ; j > ; --j){//在当前序列中查找0/1
if(map[i][ans[j]] == ){
flag = ;
Insert(ans, ansi, j + , i);
break;
}
}
if(!flag)Insert(ans, ansi, , i);
}
}
} int main()
{
//freopen("input.txt", "r", stdin);
int t;
scanf("%d", &t);
while(t--){
int N;
scanf("%d", &N);
int M = N * (N - ) / ;
int map[maxN + ][maxN + ] = {};
for(int i = ; i < M; i++){
int u, v;
scanf("%d%d", &u, &v);
if(u < v)map[v][u] = ;//建图,map[v][u] = 1,代表存在边<u, v>,且 u < v.
}
int ans[maxN + ] = {};
Hamilton(ans, map, N);
for(int i = ; i <= N; i++)
printf(i == ? "%d":" %d", ans[i]);
printf("\n");
}
return ;
}

代码2:

 #include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector> using namespace std;
typedef long long LL;
const int maxN = ; inline void read(int&a){char c;while(!(((c=getchar())>='')&&(c<='')));a=c-'';while(((c=getchar())>='')&&(c<=''))(a*=)+=c-'';} void Hamilton(int ans[maxN + ], int map[maxN + ][maxN + ], int n){
int nxt[maxN + ];//用数组模拟链表
memset(nxt, -, sizeof(nxt));
int head = ;
for(int i = ; i <= n; i++){
if(map[i][head]){
nxt[i] = head;
head = i;
}else{
int pre = head, pos = nxt[head];
while(pos != - && !map[i][pos]){
pre = pos;
pos = nxt[pre];
}
nxt[pre] = i;
nxt[i] = pos;
}
}
int cnt = ;
for(int i = head; i != -; i = nxt[i])
ans[++cnt] = i;
} int main()
{
freopen("input.txt", "r", stdin);
int N;
while(~scanf("%d", &N)){
int map[maxN + ][maxN + ] = {};
for(int i = ; i <= N; i++){
for(int j = ; j <= N; j++){
int u;
read(u);
map[i][j] = u;
}
}
printf("%d\n%d\n", , N);
int ans[maxN + ] = {};
Hamilton(ans, map, N);
for(int i = ; i <= N; i++)
printf(i == ? "%d":" %d", ans[i]);
printf("\n");
}
return ;
}

ZOJ 3332 Strange Country II (竞赛图构造哈密顿通路)的更多相关文章

  1. ZOJ 3332 Strange Country II

    Strange Country II Time Limit: 1 Second      Memory Limit: 32768 KB      Special Judge You want to v ...

  2. POJ 1776 Task Sequences(竞赛图构造哈密顿通路)

    链接:http://poj.org/problem?id=1776 本文链接:http://www.cnblogs.com/Ash-ly/p/5458635.html 题意: 有一个机器要完成一个作业 ...

  3. K - Strange Country II 暴力dfs判断有向图是否连通//lxm

    You want to visit a strange country. There are n cities in the country. Cities are numbered from 1 t ...

  4. Strange Country II 暴力dfs

    这题点的个数(<=50)有限, 所以可以纯暴力DFS去搜索 //#pragma comment(linker, "/STACK:16777216") //for c++ Co ...

  5. 哈密顿图 哈密顿回路 哈密顿通路(Hamilton)

    本文链接:http://www.cnblogs.com/Ash-ly/p/5452580.html 概念: 哈密顿图:图G的一个回路,若它通过图的每一个节点一次,且仅一次,就是哈密顿回路.存在哈密顿回 ...

  6. Zoj3332-Strange Country II(有向竞赛图)

    You want to visit a strange country. There are n cities in the country. Cities are numbered from 1 t ...

  7. hdu 5424 Rikka with Graph II (BestCoder Round #53 (div.2))(哈密顿通路判断)

    http://acm.hdu.edu.cn/showproblem.php?pid=5424 哈密顿通路:联通的图,访问每个顶点的路径且只访问一次 n个点n条边 n个顶点有n - 1条边,最后一条边的 ...

  8. zoj 3620 Escape Time II dfs

    题目链接: 题目 Escape Time II Time Limit: 20 Sec Memory Limit: 256 MB 问题描述 There is a fire in LTR ' s home ...

  9. zoj 3356 Football Gambling II【枚举+精度问题】

    题目: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3356 http://acm.hust.edu.cn/vjudge/ ...

随机推荐

  1. 【bzoj1951】[Sdoi2010]古代猪文 费马小定理+Lucas定理+中国剩余定理

    题目描述 求  $g^{\sum\limits_{k|n}C_{n}^{\frac nk}}\mod 999911659$ 输入 有且仅有一行:两个数N.G,用一个空格分开. 输出 有且仅有一行:一个 ...

  2. 一道前端面试题:定义一个方法将string的每个字符串间加个空格返回,调用的方式'hello world'.spacify();

    偶然在群里看到了这道题:定义一个方法将string的每个字符串间加个空格返回,调用的方式'hello world'.spacify(); 这道题主要是对JavaScript对象原型的考察.

  3. Luogu 3435 POI2006OKR-Periods of Words(kmp)

    显然答案应该是Σi-next[next[……next[i]]] (next[next[……next[i]]]>0).递推即可. #include<iostream> #include ...

  4. P1196 [NOI2002]银河英雄传说

    题目描述 公元五八○一年,地球居民迁至金牛座α第二行星,在那里发表银河联邦创立宣言,同年改元为宇宙历元年,并开始向银河系深处拓展. 宇宙历七九九年,银河系的两大军事集团在巴米利恩星域爆发战争.泰山压顶 ...

  5. luogu P2764 最小路径覆盖问题

    题目描述 给定有向图G=(V,E).设P 是G 的一个简单路(顶点不相交)的集合.如果V 中每个顶点恰好在P 的一条路上,则称P是G 的一个路径覆盖.P 中路径可以从V 的任何一个顶点开始,长度也是任 ...

  6. [洛谷P4015]运输问题

    题目大意:有m个仓库和n个商店.第i个仓库有 $a_{i}$ 货物,第j个商店需要$b_{j}$个货物.从第i个仓库运送每单位货物到第j个商店的费用为$c_{i,j}$​​.求出最小费用和最大费用 题 ...

  7. Backup and Restore MySQL Database using mysqlhotcopy

    mysqlhotcopy is a perl script that comes with MySQL installation. This locks the table, flush the ta ...

  8. 杭电hdu 2089 数位dp

    杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer). 杭州交通管理局经常会扩充一些的士车牌照,新近出来一个好消息,以后上牌照,不再含有不吉利的数字了,这样一来,就可以消除个别的士司机和乘客的心理障碍 ...

  9. Error in deleting blocks.

    2014-08-24 22:15:21,714 WARN org.apache.hadoop.hdfs.server.datanode.DataNode: Error processing datan ...

  10. maven项目在eclipse tomcat正常运行

    转摘自:http://binary.duapp.com/2013/10/1577.html 配置下部署路径即可.