链接: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. FAQ: SNMP on NetScaler Appliance

    FAQ: SNMP on NetScaler Appliance https://support.citrix.com/article/CTX122436 https://docs.citrix.co ...

  2. [USACO08MAR]土地征用Land Acquisition

    题面在这里 题意 约翰准备扩大他的农场,眼前他正在考虑购买N块长方形的土地. 如果约翰单买一块土地,价格就是土地的面积,但他可以选择并购一组土地, 并购的价格为这些土地中最大的长乘以最大的宽. 给定每 ...

  3. [LeetCode] decode ways 解码方式

    A message containing letters fromA-Zis being encoded to numbers using the following mapping: 'A' -&g ...

  4. [Leetcode] container with most water 最大水容器

    Given n non-negative integers a1 , a2 , ..., an , where each represents a point at coordinate (i, ai ...

  5. BZOJ day1

    十题击破  1051108811921432195119682242245624632761

  6. “CNKI 中国知网 PDF 全文下载”油猴脚本在线安装地址

    https://greasyfork.org/zh-CN/scripts/18841-cnki-%E4%B8%AD%E5%9B%BD%E7%9F%A5%E7%BD%91-pdf-%E5%85%A8%E ...

  7. 安卓下拉刷新空间SwipeRefreshLayout的基本使用

    1.先写布局文件 <android.support.v4.widget.SwipeRefreshLayout android:id="@+id/refresh" androi ...

  8. BAT定期删除N天前的文件

    1.直接看脚本在win2008测试可用 ::clean logs @echo off title clean up logs ::delete logs FORFILES /P /C "cm ...

  9. The 2017 ACM-ICPC Asia East Continent League Final记录

    首先感谢tyz学弟的麻麻-给我们弄到了名额- 然后就开始了ACM ECLFinal的玩耍,A*仙人掌可是立了flag要好好打的- 试机赛好像就全是GCJ kickstart的原题,然后AK了但是由于一 ...

  10. DotNETCore 学习笔记 异常处理

    Error Handling public void Configure(IApplicationBuilder app, IHostingEnvironment env) { app.UseIISP ...