HDU 4337 King Arthur's Knights 它输出一个哈密顿电路

n积分m文章无向边

它输出一个哈密顿电路

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

const int N = 155;

int n, m;
bool mp[N][N];

int S, T, top, Stack[N];
bool vis[N];
void _reverse(int l,int r) {
	while (l<r)
		swap(Stack[l++],Stack[r--]);
}
void expand() {
	while(1) {
		bool flag = 0;
		for (int i=1; i<=n && false == flag; i++)
			if (!vis[i] && mp[T][i])
			{
				Stack[top++]=i;
				T=i;
				flag = vis[i] = 1;
			}
		if (!flag) return;
	}
}
void hamiltun(int Start){
	memset(vis, 0, sizeof vis);

	S = Start;
	for(T=2; T<=n; T++) //随意找两个相邻的节点S和T
		if (mp[S][T]) break;
	top = 0;
	Stack[top++]=S;
	Stack[top++]=T;
	vis[S] = vis[T] = true;
	while (1)
	{
		expand(); //在它们基础上扩展出一条尽量长的没有反复节点的路径:步骤1
		_reverse(0,top-1);
		swap(S,T);
		expand(); //在它们基础上扩展出一条尽量长的没有反复节点的路径
		int mid=0;
		if (!mp[S][T]) //若S与T不相邻,能够构造出一个回路使新的S和T相邻
		{
			//设路径S→T上有k+2个节点,依次为S,v1,v2…… vk和T.
			//能够证明存在节点vi,i∈[1,k),满足vi与T相邻,且vi+1与S相邻
			for (int i=1; i<top-2; i++)
				if (mp[Stack[i]][T] && mp[Stack[i+1]][S])
				{
					mid=i+1; break;
				}
			//把原路径变成S→vi→T→vi+1→S,即形成了一个回路
			_reverse(mid,top-1);
			T=Stack[top-1];
		}
		if (top==n) break;
		//如今我们有了一个没有反复节点的回路.假设它的长度为N,则汉密尔顿回路就找到了
		//否则,因为整个图是连通的,所以在该回路上,一定存在一点与回路以外的点相邻
		//那么从该点处把回路断开,就变回了一条路径,再依照步骤1的方法尽量扩展路径
		for (int i = 1, j; i <= n; i++)
			if (!vis[i])
			{
				for (j=1; j<top-1; j++)
					if (mp[Stack[j]][i]) break;
				if (mp[Stack[j]][i])
				{
					T=i; mid=j;
					break;
				}
			}
		S=Stack[mid-1];
		_reverse(0,mid-1);
		_reverse(mid,top-1);
		Stack[top++]=T;
		vis[T]=true;
	}
}

int main() {
	while (cin>>n>>m) {
		memset(mp, 0, sizeof mp);
		for (int i = 1, u, v; i <= m; i++) {
			scanf("%d %d",&u, &v);
			mp[u][v] = mp[v][u] = 1;
		}
		hamiltun(1);
		for (int i = 0; i < top; i++)
			printf("%d%c", Stack[i], i==top-1?'\n':' ');
	}
	return 0;
}

版权声明:本文博主原创文章,博客,未经同意不得转载。

HDU 4337 King Arthur's Knights 它输出一个哈密顿电路

时间: 2024-11-25 00:22:12

HDU 4337 King Arthur&#39;s Knights 它输出一个哈密顿电路的相关文章

HDU 4337 King Arthur&#39;s Knights 找出一条哈密顿回路

n个点m条无向边 输出一条哈密顿回路 #include <cstdio> #include <cstring> #include <iostream> using namespace std; const int N = 155; int n, m; bool mp[N][N]; int S, T, top, Stack[N]; bool vis[N]; void _reverse(int l,int r) { while (l<r) swap(Stack[l++

hdu 5640 King&#39;s Cake(模拟)

Problem Description It is the king's birthday before the military parade . The ministers prepared a rectangle cake of size n×m(1≤n,m≤10000) . The king plans to cut the cake himself. But he has a strange habit of cutting cakes. Each time, he will cut

hdu 5641 King&#39;s Phone(暴力模拟题)

Problem Description In a military parade, the King sees lots of new things, including an Andriod Phone. He becomes interested in the pattern lock screen. The pattern interface is a 3×3 square lattice, the three points in the first line are labeled as

hdu 5642 King&#39;s Order

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5642 题意:给出一个n,问一个长度为n的只含有小写字母的序列中,满足其中不含有超过3个连续相同字母的序列的个数有多少. 思路: 设dp[i][j],表示序列第i位为字母j的合法序列的个数.sum[i]表示长度为i的序列有多少个. 假设第i位为字母'a',dp[i][j]就是sum[i-1]*1-不合法的情况. 不合法的情况就是sum[i-4] - dp[i-4][j]. sum[i-4]表示已经计算

POJ3682 King Arthur&#39;s Birthday Celebration

King Arthur is an narcissist who intends to spare no coins to celebrate his coming K-th birthday. The luxurious celebration will start on his birthday and King Arthur decides to let fate tell when to stop it. Every day he will toss a coin which has p

HDU 5640 King&#39;s Cake

King's Cake Problem Description It is the king's birthday before the military parade . The ministers prepared a rectangle cake of size n×m(1≤n,m≤10000) . The king plans to cut the cake himself. But he has a strange habit of cutting cakes. Each time,

HDU 5643 King&#39;s Game | 约瑟夫环变形

经典约瑟夫环 1 int f[N] ={ 0 }; 2 for(int i=2; i<=n; i++) 3 { 4 f[i] = (f[i-1] + k) % i; 5 } 变形:k是变化的 #include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> #include <stdlib.h> #include <queue> #in

HDU 5642 King&#39;s Order(数位DP)

题目链接:点击打开链接 题意:要求你生成一个合法的字符串, 由小写字母a~z组成, 相同字母相邻出现不能超过3个, 求有多少种组合. 思路:数位DP来做, 用d[i][j][k]表示处理完前i个字母, 第i-1个字母为j,已经连续出现了k次的方法数. 然后每次转移就很简单了, 继续选择字母j(if(k < 3)), 或者换其他的. 细节参见代码: #include<cstdio> #include<cstring> #include<algorithm> #inc

HDU 4081Qin Shi Huang&amp;#39;s National Road System(最小生成树+最小瓶颈路)

 题意:秦始皇要修路.把N个城市用N-1条边连通.且他希望花费最小,可是这时候有一个多管闲事的道士出来说他有魔法能够帮助秦始皇变成一条路.可是仅仅能变出一条. 可是.两个人对修路的法案存在歧义,道士希望修路能够给很多其它的百姓带来福利.而秦始皇希望修路要尽量使花费小.最后,秦始皇拿出了一个公式A/B.A表示两个城市的人数,B表示出了用魔法变出来的路外.最短的总距离. 如今要你求出A/B的最大值. 思路:枚举连接哪两个城市.由于这条边是免费的.我们要求算上这条边的最小生成树.假设每次都求一次最