SGU 101 && POJ 1041 欧拉路径/欧拉回路

101. Domino

time limit per test: 0.25 sec. 
memory limit per test: 4096 KB

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 cards.
The face of each piece is divided, by a line or ridge, into two squares, each of which is marked as would be a pair of dice...

The principle in nearly all modern dominoes games is to match one end of a piece to another that is identically or reciprocally numbered.

ENCYCLOPÆDIA BRITANNICA

Given a set of domino pieces where each side is marked with two digits from 0 to 6. Your task is to arrange pieces in a line such way, that they touch through equal marked sides. It is possible to rotate pieces changing left and right side.

Input

The first line of the input contains a single integer N (1 ≤ N ≤ 100) representing the total number of pieces in the domino set. The following N lines describe pieces. Each piece is represented on a separate line in a form of two digits from 0 to 6 separated by a space.

Output

Write “No solution” if it is impossible to arrange them described way. If it is possible, write any of way. Pieces must be written in left-to-right order. Every of N lines must contains number of current domino piece and sign “+” or “-“ (first means that you not rotate that piece, and second if you rotate it).

Sample Input

5
1 2
2 4
2 4
6 4
2 1

Sample Output

2 -
5 +
1 +
3 +
4 -

SGU101题意是给出两个牌,正面反面都有一个0到6的数,对着的面数字相同的两张牌可以连在一起,牌有正面和反面两种放法,然后问这些牌能不能连在一起,输出的是欧拉路径。欧拉路径要求的是度为奇数的点要么0个,要么2个。

一开始也深搜,然后正着记录的。一直WA。后来自己搞了一个反例,因为优先从奇数点开始搜,那因为一个点连接的点可能会很多,万一那一条是一个单一路径而不是回路,正着记录就不行了。所以需要逆序记录。

代码:

#pragma warning(disable:4996)
#include <iostream>
#include <functional>
#include <algorithm>
#include <cstring>
#include <vector>
#include <string>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <deque>
#include <set>
#include <map>
using namespace std;
typedef long long ll;

#define INF 0x333f3f3f
#define repp(i, n, m) for (int i = n; i <= m; i++)
#define rep(i, n, m) for (int i = n; i < m; i++)
#define sa(n) scanf("%d", &(n))

const ll mod = 1e9 + 7;
const int maxn = 3e2 + 5;
const double PI = acos(-1.0);

struct ed
{
	int to;
	int next;
	int flag;
	int dir;
	int ind;
}edge[maxn];

int n, edgen;
int head[maxn], out[8], fa[8];

void init()
{
	edgen = 0;
	memset(head, -1, sizeof(head));
	memset(edge, -1, sizeof(edge));
	memset(out, 0, sizeof(out));
	repp(i, 0, 6)
		fa[i] = i;
}

void addedge(int u, int v, int dir,int ind)
{
	edge[edgen].to = v;
	edge[edgen].flag = 0;
	edge[edgen].dir = dir;
	edge[edgen].ind = ind;
	edge[edgen].next = head[u];
	head[u] = edgen;
	edgen++;
}

int getfa(int x)
{
	return fa[x] == x ? x : fa[x] = getfa(fa[x]);
}

vector<int>res;

void uni(int u, int v)
{
	u = getfa(u), v = getfa(v);
	if (u == v)return;
	fa[u] = v;
}

void dfs(int x)
{
	int i, j, k;
	for (i = head[x]; i != -1; i = edge[i].next)
	{
		if (!edge[i].flag)
		{
			edge[i].flag = 1;
			edge[i ^ 1].flag = 1;
			k = edge[i].to;
			dfs(k);
			res.push_back(i);
		}
	}
}

void solve()
{
	int i, j, k;
	int u, v;
	repp(i, 1, n)
	{
		scanf("%d%d", &u, &v);
		addedge(u, v, 1, i);
		addedge(v, u, -1, i);
		out[u]++;
		out[v]++;
		uni(u, v);
	}
	int cnt = 0, st = -1;
	for (i = 0; i <= 6; i++)
	{
		if (out[i] & 1)
		{
			cnt++;
			st = i;
		}
		if (out[i] > 0)
		{
			st = i;
		}
	}
	if (st == -1)
	{
		printf("No solution\n");
		return;
	}
	if (cnt != 0 && cnt != 2)
	{
		printf("No solution\n");
		return;
	}
	for (i = 0; i <= 6; i++)
	{
		if (out[i] > 0 && getfa(i) != getfa(st))
		{
			printf("No solution\n");
			return;
		}
	}
	for (i = 0; i <= 6; i++)
	{
		if (out[i] & 1)
		{
			st = i;
		}
	}
	res.clear();
	dfs(st);
	for (i = 0; i < res.size(); i++)
	{
		printf("%d ", edge[res[i]].ind);
		if (edge[res[i]].dir == 1)
		{
			printf("-\n");
		}
		else
		{
			printf("+\n");
		}
	}
}

int main()
{
#ifndef ONLINE_JUDGE  
	freopen("i.txt", "r", stdin);
	freopen("o.txt", "w", stdout);
#endif
	while (scanf("%d", &n) != EOF)
	{
		init();
		solve();
	}
	return 0;
}


John's trip
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 8197 Accepted: 2767 Special Judge

Description

Little Johnny has got a new car. He decided to drive around the town to visit his friends. Johnny wanted to visit all his friends, but there was many of them. In each street he had one friend. He started thinking how to make his trip as short as possible. Very soon he realized that the best way to do it was to travel through each street of town only once. Naturally, he wanted to finish his trip at the same place he started, at his parents' house. 

The streets in Johnny's town were named by integer numbers from 1 to n, n < 1995. The junctions were independently named by integer numbers from 1 to m, m <= 44. No junction connects more than 44 streets. All junctions in the town had different numbers. Each street was connecting exactly two junctions. No two streets in the town had the same number. He immediately started to plan his round trip. If there was more than one such round trip, he would have chosen the one which, when written down as a sequence of street numbers is lexicographically the smallest. But Johnny was not able to find even one such round trip. 

Help Johnny and write a program which finds the desired shortest round trip. If the round trip does not exist the program should write a message. Assume that Johnny lives at the junction ending the street appears first in the input with smaller number. All streets in the town are two way. There exists a way from each street to another street in the town. The streets in the town are very narrow and there is no possibility to turn back the car once he is in the street 

Input

Input file consists of several blocks. Each block describes one town. Each line in the block contains three integers x; y; z, where x > 0 and y > 0 are the numbers of junctions which are connected by the street number z. The end of the block is marked by the line containing x = y = 0. At the end of the input file there is an empty block, x = y = 0.

Output

Output one line of each block contains the sequence of street numbers (single members of the sequence are separated by space) describing Johnny's round trip. If the round trip cannot be found the corresponding output block contains the message "Round trip does not exist."

Sample Input

1 2 1
2 3 2
3 1 6
1 2 5
2 3 3
3 1 4
0 0
1 2 1
2 3 2
1 3 3
2 4 4
0 0
0 0

Sample Output

1 2 3 5 4 6 
Round trip does not exist.


POJ1041输出的是字典序最小的欧拉回路,然后发现这个路径的标号最多是44,所以直接暴力搜,从最小的开始搜,然后逆序记录,再逆序输出。欧拉回路要求点的度数全部是偶数。

代码:

#pragma warning(disable:4996)
#include <iostream>
#include <functional>
#include <algorithm>
#include <cstring>
#include <vector>
#include <string>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <deque>
#include <set>
#include <map>
using namespace std;
typedef long long ll;

#define INF 0x333f3f3f
#define repp(i, n, m) for (int i = n; i <= m; i++)
#define rep(i, n, m) for (int i = n; i < m; i++)
#define sa(n) scanf("%d", &(n))

const ll mod = 1e9 + 7;
const int maxn = 1e6 + 5;
const double PI = acos(-1.0);

struct ed
{
	int u;
	int v;
}edge[maxn];

int out[maxn], vis[maxn];

void init()
{
	memset(vis, 0, sizeof(vis));
	memset(out, 0, sizeof(out));
	memset(edge, 0, sizeof(edge));
}

vector<int>res;

int n, m;
int x, y, z;

void dfs(int x)
{
	int i, j, k;
	for (i = 1; i <= m; i++)
	{
		if (vis[i] == 0 && (edge[i].u == x || edge[i].v == x))
		{
			vis[i] = 1;
			if (edge[i].u == x)
			{
				dfs(edge[i].v);
			}
			else
			{
				dfs(edge[i].u);
			}
			res.push_back(i);
		}
	}
}

void solve()
{
	int i, j, k;
	int u, v;
	int st = min(x, y);
	n = 0, m = 0;

	do
	{
		scanf("%d", &z);
		n = max(n, max(x, y));
		m++;
		edge[z].u = x;
		edge[z].v = y;
		out[x]++;
		out[y]++;
	} while (scanf("%d%d", &x, &y) == 2 && x != 0 && y != 0);

	for (i = 1; i <= n; i++)
	{
		if (out[i] & 1)
		{
			printf("Round trip does not exist.\n");
			return;
		}
	}
	res.clear();
	dfs(st);
	for (i = res.size() - 1; i >= 0; i--)
	{
		if (i == 0)
		{
			printf("%d", res[i]);
		}
		else
		{
			printf("%d ", res[i]);
		}
	}
	printf("\n");
}

int main()
{

	while (scanf("%d%d", &x,&y) != EOF)
	{
		if (x == 0 && y == 0)
			break;
		init();
		solve();
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值