SGU 101.Domino( 欧拉路径 )

本文介绍了一种求解欧拉路径的算法实现,并通过一个具体的多米诺骨牌问题来展示该算法的应用。该算法使用深度优先搜索(DFS),时间复杂度为O(N),适用于寻找图中的一条特殊路径,即欧拉路径。文章提供了完整的C++代码实现。

求欧拉路径...直接dfs即可,时间复杂度O(N)

---------------------------------------------------------------------------

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
 
using namespace std;
 
#define X(i) Edge[i].first
#define Y(i) Edge[i].second
typedef pair<int, int> pii;
 
const int maxn = 10;
const int maxm = 109;
const int n = 7;
 
struct edge {
int to, F, Id;
edge* next;
} E[maxm << 1], *pt = E, *head[maxn];
 
void AddEdge(int u, int v, int Id, int f) {
pt->to = v;
pt->F = f;
pt->Id = Id;
pt->next = head[u];
head[u] = pt++;
}
 
inline edge* Rev(edge* e) {
return E + ((e - E) ^ 1);
}
 
pii Edge[maxm];
int N, cnt[maxn], par[maxn];
stack<pii> S;
 
int Find(int x) {
return x == par[x] ? x : par[x] = Find(par[x]);
}
 
bool chk() {
int c = 0;
for(int i = 0; i < n; i++)
if((cnt[i] & 1) && ++c > 2) return false;
for(int i = 0; i < n; i++) par[i] = i;
for(int i = 0; i < N; i++)
par[Find(X(i))] = Find(Y(i));
c = -1;
for(int i = 0; i < n; i++) if(cnt[i]) {
if(!~c) 
c = par[i];
else if(par[i] != c)
return false;
}
return true;
}
 
void Euler(int x) {
for(edge*&e = head[x]; e; ) if(e->F) {
int F = e->F, t = e->to, Id = e->Id;
e->F = Rev(e)->F = 0;
e = e->next;
Euler(t);
S.push(make_pair(Id, F));
} else
e = e->next;
}
 
int main() {
scanf("%d", &N);
memset(cnt, 0, sizeof cnt);
for(int i = 0; i < N; i++) {
scanf("%d%d", &X(i), &Y(i));
cnt[X(i)]++;
cnt[Y(i)]++;
AddEdge(X(i), Y(i), i, 1);
AddEdge(Y(i), X(i), i, -1);
}
if(!chk()) {
puts("No solution");
return 0;
}
for(int i = 0; i < n; i++) if(cnt[i] & 1) {
Euler(i);
while(!S.empty()) {
pii p = S.top(); S.pop();
printf("%d %c\n", ++p.first, p.second != 1 ? '-' : '+');
}
return 0;
}
for(int i = 0; i < n; i++) if(cnt[i]) {
Euler(i);
while(!S.empty()) {
pii p = S.top(); S.pop();
printf("%d %c\n", ++p.first, p.second != 1 ? '-' : '+');
}
return 0;
}
return 0;
}

--------------------------------------------------------------------------- 

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 - 

 

转载于:https://www.cnblogs.com/JSZX11556/p/5052205.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值