您的位置:首页 > 其它

SGU 101 输出欧拉路径

2014-03-26 17:21 411 查看
Domino

Time Limit:250MS Memory Limit:4096KB 64bit IO Format:%I64d & %I64u
[Submit]  [Go Back]  [Status]  

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 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 -


先并查集判连通,然后dfs搜索。

代码:

/* ***********************************************
Author :rabbit
Created Time :2014/3/26 16:50:39
File Name :10.cpp
************************************************ */
#pragma comment(linker, "/STACK:102400000,102400000")
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <string>
#include <time.h>
#include <math.h>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-8
#define pi acos(-1.0)
typedef long long ll;
const int maxn=300;
int fa[maxn],head[maxn],tol,du[maxn];
struct Edge{
int next,to,id,vis,dir;
Edge(){};
Edge(int _next,int _to,int _id,int _vis,int _dir){
next=_next;to=_to;id=_id;vis=_vis;dir=_dir;
}
}edge[maxn];
void addedge(int u,int v,int id){
edge[tol]=Edge(head[u],v,id,0,0);
head[u]=tol++;
edge[tol]=Edge(head[v],u,id,0,1);
head[v]=tol++;
}
vector<int> ans;
int find(int x){
if(fa[x]!=x)fa[x]=find(fa[x]);
return fa[x];
}
void bin(int u,int v){
int t1=find(u);
int t2=find(v);
if(t1!=t2)fa[t1]=t2;
}
void dfs(int u){
for(int i=head[u];i!=-1;i=edge[i].next)
if(!edge[i].vis){
edge[i].vis=edge[i^1].vis=1;
dfs(edge[i].to);
ans.push_back(i);
}
}
int main()
{
//freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
int n;
while(~scanf("%d",&n)){
memset(head,-1,sizeof(head));tol=0;
memset(du,0,sizeof(du));
for(int i=0;i<=10;i++)fa[i]=i;
for(int i=1;i<=n;i++){
int u,v;
scanf("%d%d",&u,&v);
addedge(u,v,i);
du[u]++;du[v]++;
bin(u,v);
}
//cout<<"haha"<<endl;
int s=-1,cnt=0,flag=1;
for(int i=0;i<=6;i++){
if(du[i]&1)cnt++;
if(du[i]>0&&s==-1)s=i;
}
if(cnt&&cnt!=2){
puts("No solution");
continue;
}
for(int i=0;i<=6;i++)
if(du[i]>0&&find(i)!=find(s))flag=0;
if(!flag){
puts("No solution");
continue;
}
//cout<<"haha"<<endl;
ans.clear();
if(cnt==0)dfs(s);
else{
for(int i=0;i<6;i++)
if(du[i]&1){
dfs(i);break;
}
}
for(int i=0;i<ans.size();i++){
printf("%d ",edge[ans[i]].id);
if(edge[ans[i]].dir==0)puts("-");else puts("+");
}
// cout<<ans.size()<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: