SGU 101. Domino 欧拉回路

无向图欧拉回路 欧拉通路

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

struct edge
{
	int v, next, b, id;
}e[210];
int vis[210];
int first[10], cnt;
int ans[210], len;

int f[10];

int find(int x)
{
	if(x != f[x])
		return f[x] = find(f[x]);
	return f[x];
}
void AddEdge(int u, int v, int id)
{
	e[cnt].id = id;
	e[cnt].b = 0;
	e[cnt].v = v;
	e[cnt].next = first[u];
	first[u] = cnt++;

	e[cnt].id = id;
	e[cnt].b = 1;
	e[cnt].v = u;
	e[cnt].next = first[v];
	first[v] = cnt++;
}

void dfs(int u)
{
	for(int i = first[u]; i != -1; i = e[i].next)
	{
		if(!vis[i])
		{
			vis[i] = 1;
			vis[i^1] = 1;
			dfs(e[i].v);
			ans[len++] = i;
		}
	}
}
int main()
{
	int n;
	scanf("%d", &n);
	memset(first, -1, sizeof(first));
	cnt = 0;
	int b[10] = {0}, s1 = 0, s2 = 0;
	for(int i = 0; i <= 6; i++)
		f[i] = i;
	for(int i = 1; i <= n; i++)
	{
		int u, v;
		scanf("%d %d", &u, &v);
		AddEdge(u, v, i);
		if(!b[u])
		{
			s1++;
			b[u] = 1;
		}
		else
			b[u]++;
		if(!b[v])
		{
			s1++;
			b[v] = 1;
		}
		else
			b[v]++;
	 	int x = find(u), y = find(v);
	 	if(x != y)
 		{
		 	f[x] = y;
		 	s2++;
 		}
	}

	int rt = -1, sum = 0;
	len = 0;
	for(int i = 0; i <= 6; i++)
	{
		if(b[i]&1)
		{
			sum++;
			rt = i;
		}
	}
	if(s1 - 1 != s2)
	{
		puts("No solution");
	}
	else if(sum == 2)
	{
		dfs(rt);
		for(int i = len-1; i >= 0; i--)
		{
			int x = ans[i];
			if(e[x].b)
				printf("%d -\n", e[x].id);
			else
				printf("%d +\n", e[x].id);
		}
	}
	else if(!sum)
	{
		for(int i = 0; i <= 6; i++)
		{
			if(b[i])
			{
				dfs(i);
				break;
			}
		}
		for(int i = len-1; i >= 0; i--)
		{
			int x = ans[i];
			if(e[x].b)
				printf("%d -\n", e[x].id);
			else
				printf("%d +\n", e[x].id);
		}
	}
	else
		puts("No solution");
	return 0;
}
时间: 2024-12-15 18:30:17

SGU 101. Domino 欧拉回路的相关文章

SGU 101.Domino( 欧拉路径 )

求欧拉路径...直接dfs即可,时间复杂度O(N) --------------------------------------------------------------------------- #include<cstdio> #include<cstring> #include<algorithm> #include<stack> using namespace std; #define X(i) Edge[i].first #define Y(

SGU[101] Domino

Description 描述 Dominoes – game played with small, rectangular blocks of wood or other material, each identified by a number of dots, or pips, on its face. The blocks usually are called bones, dominoes, or pieces and sometimes men, stones, or even car

SGU 101 Domino 题解

鉴于SGU题目难度较大,AC后便给出算法并发布博文,代码则写得较满意后再补上.——icedream61 题目简述:暂略 AC人数:3609(2015年7月20日) 算法: 这题就是一笔画,最多只有7个点(0~6),每个骨牌就是一条双向边. 要做的,就是找出一条一笔画的路径.步骤如下: 1. 读入数据,建成图. 2. 判断可否一笔画.一笔画条件:奇点数为0或2 + 联通分量数为1. 3. 如果不可一笔画,则输出No solution,结束:如果可以,则找到一条路径R,并输出. 下面给出找一笔画路径

ACM: SGU 101 Domino- 欧拉回路-并查集

sgu 101 - Domino Time Limit:250MS     Memory Limit:4096KB     64bit IO Format:%I64d & %I64u Description Dominoes – game played with small, rectangular blocks of wood or other material, each identified by a number of dots, or pips, on its face. The bl

SGU 101 题解

#include<iostream> #include<algorithm> using namespace std; struct sticktype { int l; int r; bool vis; }stick[101]; struct pointtype { int amount; int next[7]; bool vis; }point[7]; int tail=0; int queue[201]; void dfs(int p); int main() { int

Domino - SGU 101 (欧拉路径)

题目大意:这是一个多米诺骨游戏,这个游戏的规则就是一个连着一个,现在给出 N 个多米诺,每个多米诺两边都有一个编号,相邻的多米诺的编号要一致,当然多米诺是可以翻转的(翻转就加‘-’,不翻转是‘+’),输出一个多米诺的顺序,要从左往右. 分析:开始的是有以为是二分匹配,然后发现并不能匹配,无法分成两个集合,网络流也不能搞,最后百度才知道是欧拉路径(从一点出发经过所有的路径,每条路只走一次),这个算法倒也不难理解,感觉很巧妙,如果点的入度都是偶数的话,那么就是欧拉回路(可以从一个点出发然后,最后还可

sgu 101 无向图有双重边的欧拉路径

#include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <cmath> #include <queue> #include <stack> #include <algorithm> using namespace std; const int MAXN = 400 + 10; struct Edge

SGU 乱搞日志

SGU 100 A+B :太神不会 SGU 101 Domino: 题目大意:有N张骨牌,两张骨牌有两面有0到6的数字,能相连当且仅当前后数字相同,问能否有将N张骨牌连接的方案?思路:裸的欧拉回路,注意自环,连通 1 //sgu101 2 #include<iostream> 3 #include<cstdio> 4 #include <math.h> 5 #include<algorithm> 6 #include<string.h> 7 #i

今日SGU

SGU 100 题意: 普通的a+b #include<bits/stdc++.h> #define de(x) cout<<#x<<"="<<x<<endl; #define dd(x) cout<<#x<<"="<<x<<" "; #define rep(i,a,b) for(int i=a;i<(b);++i) #define