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;
}
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 8197 | Accepted: 2767 | Special Judge |
Description
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
Output
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;
}