链接: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. hdu 1575 Tr A (二分矩阵)

    Tr A Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  2. table表头固定问题

    table表头固定问题 原生的table表头在表格滚动时候无法固定,可以使用以下的方法进行模拟 1. 双table法 表头和表体各用一个table,这样会产生表格列对不齐的问题,可以使用colgrou ...

  3. 【17.12.22.B】

    B 题面描述: 给一个长度为n的序列,a[1], a[2], ... , a[n], 选出连续的k个数,使得这k个数的最大值加这k个数的or值最大. 假设选出的数为a[l], a[l + 1], .. ...

  4. 【BZOJ 3123】 [Sdoi2013]森林 主席树启发式合并

    我们直接按父子关系建主席树,然后记录倍增方便以后求LCA,同时用并查集维护根节点,而且还要记录根节点对应的size,用来对其启发式合并,然后每当我们合并的时候我们都要暴力拆小的一部分重复以上部分,总时 ...

  5. 使用Spring AOP实现读写分离(MySql实现主从复制)

    1.  背景 我们一般应用对数据库而言都是“读多写少”,也就说对数据库读取数据的压力比较大,有一个思路就是说采用数据库集群的方案,其中一个是主库,负责写入数据,我们称之为:写库: 其它都是从库,负责读 ...

  6. JavaScript获取HTML元素样式的方法(style、currentStyle、getComputedStyle)

    一.style.currentStyle.getComputedStyle的区别 style只能获取元素的内联样式,内部样式和外部样式使用style是获取不到的. currentStyle可以弥补st ...

  7. TestRedis

    import org.junit.Before; import org.junit.Test; import redis.clients.jedis.Jedis; import java.util.H ...

  8. 动态规划:LCIS

    先给出状态转移方程: 定义状态 F[i][j]表示以a串的前i个整数与b串的前j个整数且以b[j]为结尾构成的LCIS的长度 状态转移方程: ①F[i][j] = F[i-][j] (a[i] != ...

  9. MongoDB 聚合(管道与表达式)

    MongoDB中聚合(aggregate)主要用于处理数据(诸如统计平均值,求和等),并返回计算后的数据结果.有点类似sql语句中的 count(*). aggregate() 方法 MongoDB中 ...

  10. 【mysql优化】大数据量分页优化

    limit 翻页原理 limit offset,N, 当offset非常大时, 效率极低, 原因是mysql并不是跳过offset行,然后单取N行, 而是取offset+N行,返回放弃前offset行 ...