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(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 - 
时间: 2024-08-25 23:08:34

SGU 101.Domino( 欧拉路径 )的相关文章

Domino - SGU 101 (欧拉路径)

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

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,并输出. 下面给出找一笔画路径

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

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

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