SGU 122.The book (哈密顿回路)
题目描述
有一群人从1到N标号,而且这群人中每个人的朋友个数不少于 (N+1)/2 个。
编号为1的人有一本其他人都想阅读的书。
写一个程序,找到一种传阅顺序使得书本只经过每个人手中一次,并且一个人只能将书本传给他的朋友,并且书本最后必须传回给第一个人。(注释:如果A是B的朋友,那么B一定是A的朋友)
输入
第一行包含一个数字N。
接下来的有N行,第i行表示第i-1个人的朋友
输出
如果不存在解决方案,则输出 'No solution' 。否则你将输出1行包含N+1个整数,表示传阅路径,由1开始、由1结尾。
输入样例
4
2 3
1 4
1 4
2 3
输出样例
1 3 4 2 1
Solution:
每个人至少有(n+1)/2 个朋友,一定存在哈密顿回路.
找到哈密顿路后,从1 的位置开始输出,最后再输出一个1.
code:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#define INF 1111
using namespace std;
bool edge[INF][INF];
int ans[INF], vis[INF];
int n, tol = 2, t, s = 1;
void expand() {
int i;
while (1) {
for (i = 1; i <= n; i++) {
if (edge[t][i] && !vis[i]) {
ans[tol++] = i;
t = i, vis[i] = 1;
break;
}
}
if (i > n) return;
}
}
void Hamiton() {
int i, j;
for (i = 1; i <= n; i++) if (edge[s][i]) break;
t = i;
ans[0] = s, ans[1] = t;
vis[s] = vis[t] = 1;
while (1) {
expand();
reverse (ans, ans + tol);
swap (s, t);
expand();
if (!edge[s][t]) {
for (i = 1; i < tol - 2; i++)
if (edge[ans[i]][t] && edge[ans[i + 1]][s]) break;
reverse (ans + i + 1, ans + tol);
t = ans[tol - 1];
}
if (tol == n) return;
for (j = 1; j <= n; j++) {
if (vis[j]) continue;
for (i = 1; i < tol - 1; i++)
if (edge[ans[i]][j]) break;
if (edge[ans[i]][j]) break;
}
s = ans[i - 1], t = j;
reverse (ans, ans + i);
reverse (ans + i, ans + tol);
ans[tol++] = j, vis[j] = 1;
}
}
int main() {
char ci;
scanf ("%d", &n);
for (int i = 1; i <= n; i++) {
scanf ("%d", &t);
edge[i][t] = edge[t][i] = 1;
ci = getchar();
while (ci != '\n' && ci != '\r' && ci != EOF)
scanf ("%d", &t), edge[i][t] = edge[t][i] = 1, ci = getchar();
}
Hamiton();
int i;
for (i = 0; i < n; i++)
if (ans[i] == 1) break;
for (int j = 0; j < n; j++) {
printf ("%d ", ans[i]);
i++;
if (i == n) i = 0;
}
putchar ('0' + 1);
return 0;
}
SGU 122.The book (哈密顿回路)的更多相关文章
- sgu 122. The book 满足ore性质的汉密尔顿回路 难度:2
122. The book time limit per test: 0.25 sec. memory limit per test: 4096 KB There is a group of N (2 ...
- The sum - SGU 122(斐波那契前N项和)
直接上代码....... ======================================================================================= ...
- 今日SGU 5.27
SGU 122 题意:给你n个人,每个人有大于 N / 2(向上取整)的朋友,问你1这个人有一个书,每个人都想看,只能从朋友之间传递,然后最后回到了1这个人,问你 是否有解,然后有解输出路径 收获:哈 ...
- SGU 分类
http://acm.sgu.ru/problemset.php?contest=0&volume=1 101 Domino 欧拉路 102 Coprime 枚举/数学方法 103 Traff ...
- SGU Volume 1
SGU 解题报告(持续更新中...Ctrl+A可看题目类型): SGU101.Domino(多米诺骨牌)------------★★★type:图 SGU102.Coprimes(互质的数) SGU1 ...
- SGU 156. Strange Graph(欧拉路)
时间限制:0.25s 空间限制:6M 题目描述 让我们想象一个无向图G=<V,E>.如果边(u,v)在边集E中,那么我们就说两个顶点u和v是邻接点.在这种情况下,我们也说u是v的一个邻接点 ...
- 122. Best Time to Buy and Sell Stock(二) leetcode解题笔记
122. Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price ...
- SGU 495. Kids and Prizes
水概率....SGU里难得的水题.... 495. Kids and Prizes Time limit per test: 0.5 second(s)Memory limit: 262144 kil ...
- HDU 1693 Eat the Trees(插头DP、棋盘哈密顿回路数)+ URAL 1519 Formula 1(插头DP、棋盘哈密顿单回路数)
插头DP基础题的样子...输入N,M<=11,以及N*M的01矩阵,0(1)表示有(无)障碍物.输出哈密顿回路(可以多回路)方案数... 看了个ppt,画了下图...感觉还是挺有效的... 参考 ...
随机推荐
- 网络流(最大流)CodeForces 512C:Fox And Dinner
Fox Ciel is participating in a party in Prime Kingdom. There are n foxes there (include Fox Ciel). T ...
- POI做题记录:第二届POI
Trees Memory limit: 32 MB Trees occur very often in computer science. As opposed to trees in nature, ...
- HDU 5926 Mr. Frog's Game 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)
Mr. Frog's Game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...
- Quality in the Test Automation Review Process and Design Review Template
About this document Prerequisite knowledge/experience: Software Testing, Test Automation Applicable ...
- bzoj 1096 [ZJOI2007]仓库建设(关于斜率优化问题的总结)
1096: [ZJOI2007]仓库建设 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3234 Solved: 1388[Submit][Stat ...
- winform 窗体关闭按钮禁用、不显示最大化、最小化、关闭按钮 分类: WinForm 2014-12-22 16:09 82人阅读 评论(0) 收藏
关闭按钮禁用: (1) FormClosing事件 private void Main_FormClosing(object sender, FormClosingEventArgs e) { ...
- springboot +spring security4 +thymeleaf 后台管理系统
需求:一个后台管理系统,现在用的springboot 微框架比较多, 所以这里也使用了, 后台权限用 spring security ,之前以前觉得听复杂 . 后来发现还是蛮简单的, 看了源代码之后. ...
- 使用javah生成.h文件, 出现无法访问android.app,Activity的错误的解决
在工程ndk22/bin/classes中 运行javah com.cn.ndk22.Ndk22.Activity ,出现了.h文件 我在bin/classes目录中 ,就是无法访问, : 错误:无 ...
- OC中类别、扩展、协议与托付
类别(category)--通过使用类别,我们能够动态地为现有的类加入新方法.并且能够将类定义模块化地分不到多个相关文件里.通常仅仅在类别中定义方法. 类别,接口部分的定义,通常该文件命名为已有&qu ...
- centos 网站目录权限参考
Linux下Apache网站目录读写权限的设置 网站目录文件权限的设置对网站的安全至关重要,下面简单介绍网站目录文件权限的基本设定. 我们假设http服务器运行的用户和用户组是www,网站用户为cen ...