sgu 101 Domino 解题报告及测试数据
101. Domino
time limit per test: 0.25 sec.
memory limit per test: 4096 KB
题解:
求多米诺骨牌按照一定方式放置能否使相邻的位置数字相同。其实就是求无向图的欧拉通路,dfs即可。
但需要注意以下几点:
1、注意是否是连通图。
2、注意自环。
3、注意有两个奇度顶点时深搜起点。
以下是代码:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int arr[7][7];
int visited[105];
int path[105][2];
int st,end;
int n,tag;
struct node{
int x,y;
}Node[105]; int judge(){ //判断是否是欧拉图、半欧拉图
int a[7]={ 0 };
for(int i=0;i<7;i++)
for(int j=0;j<7;j++)
a[i]+=arr[i][j];
int cnt=0;
for(int i=0;i<7;i++)
if(a[i]%2){
st=i;
cnt++;//如果是半欧拉图 将深搜起点改为奇度顶点
}
if(cnt==0 || cnt==2)return st; //如果是返回深搜起点
return -1;
}
void dfs(int v,int k,int ii){//深度优先搜索
if(tag==1)return;
visited[v]=1;
char ch = k==1? '+' : '-'; //k为1代表不翻 k为2代表翻转
int t = k==1?Node[v].y : Node[v].x;
path[ii][0]=v+1;
path[ii][1]=ch;
if(ii==n-1){ //如果是连通图 输出路径
for(int i=0;i<n;i++)
printf("%d %c\n",path[i][0],path[i][1]);
tag=1;
}
for(int i=0;i<n;i++)
if(visited[i]==0){
if(Node[i].x==t)dfs(i,1,ii+1);
else if(Node[i].y == t)dfs(i,2,ii+1);
visited[i]=0;
}
}
int main(){
//freopen("1.in","r",stdin);
while(cin >> n){
for(int i=0;i<n;i++){
scanf("%d%d",&st,&end);
arr[st][end]++;
arr[end][st]++;
Node[i].x = st;
Node[i].y = end;
}
if(judge()==-1)printf("No solution\n");//不是欧拉图 半欧拉图
else{
for(int i=0;i<n;i++)
if(Node[i].x == st){
dfs(i,1,0);
break;
}
else if(Node[i].y == st){
dfs(i,2,0);
break;
}
if(tag==0)printf("No solution\n");//不是连通图
}
}
}
以下是测试数据:
sample input
17
0 3
1 2
5 1
1 4
2 1
0 2
3 4
3 5
4 3
5 0
3 4
4 0
0 4
4 4
1 4
0 4
3 1
3
5 2
0 5
3 5
4
5 2
5 2
5 4
2 5
9
2 5
2 1
2 1
5 4
5 0
4 1
2 1
3 5
3 4
14
2 5
4 1
5 4
0 0
3 0
5 4
3 0
3 2
2 4
1 2
5 3
3 1
4 1
2 4
sample output
3 +
2 +
5 +
4 +
7 -
1 -
10 -
8 -
9 -
11 -
17 +
15 +
12 +
13 +
14 +
16 -
6 +
No solution
3 -
1 +
2 -
4 -
4 -
1 -
2 +
3 -
7 +
6 -
9 -
8 +
5 +
5 +
4 +
7 -
8 +
1 +
3 +
2 +
10 +
9 +
6 -
11 +
12 +
13 -
14 -
sgu 101 Domino 解题报告及测试数据的更多相关文章
- sgu 102 Coprimes 解题报告及测试数据
102. Coprimes time limit per test: 0.25 sec. memory limit per test: 4096 KB 题解: 求一个1-10000之间的数 N 的互质 ...
- SGU 101 Domino (输出欧拉路径)
101. Domino time limit per test: 0.25 sec. memory limit per test: 4096 KB Dominoes – game played wit ...
- SGU 101.Domino( 欧拉路径 )
求欧拉路径...直接dfs即可,时间复杂度O(N) -------------------------------------------------------------------------- ...
- sgu 104 Little shop of flowers 解题报告及测试数据
104. Little shop of flowers time limit per test: 0.25 sec. memory limit per test: 4096 KB 问题: 你想要将你的 ...
- sgu 103 Traffic Lights 解题报告及测试数据
103. Traffic Lights Time limit per test: 0.25 second(s) Memory limit: 4096 kilobytes 题解: 1.其实就是求两点间的 ...
- sgu 100 A+B 解题报告及测试数据
100.A+B time limit per test: 0.25 sec. memory limit per test: 65536 KB 题解:上手题,不解释. 直接上代码: #include & ...
- SGU 101 Domino【欧拉路径】
题目链接: http://acm.sgu.ru/problem.php?contest=0&problem=101 题意: N个多米诺骨牌,每个骨牌左右两侧分别有一个0~6的整数(骨牌可以旋转 ...
- Spring-2-H Array Diversity(SPOJ AMR11H)解题报告及测试数据
Array Diversity Time Limit:404MS Memory Limit:0KB 64bit IO Format:%lld & %llu Descript ...
- SGU 101.Domino (欧拉路)
时间限制: 0.5 sec 空间限制: 4096 KB 描述 多米诺骨牌,一种用小的方的木块或其他材料,每个都被一些点在面上标记,这些木块通常被称为骨牌.每个骨牌的面都被一条线分成两个 方形,两边 ...
随机推荐
- iOS 模块化
模块化 1.公共模块 网络层 模型层(基类) 2.mvvm 3.模块化(单元模块,实现单元功能,单元测试) 4.pod 5.路由
- 链接href的多重使用
1. 拨打电话 在电话号码前面可以加上 + (加号)表示国际号码. <a href="tel:10086">10086</a> 使用wtai协议进行拨打电话 ...
- JSP页面的生命周期
JSP页面的生命周期:我们假设要访问的jsp页面是index.jsp.首先,用户发出请求index.jsp:服务器会判断是否是第一次请求:如果是的话,JSP引擎会把该JSP文件转换成为一个Servle ...
- php实现简单的权限管理
今天主要来实现一个权限管理系统,它主要是为了给不同的用户设定不同的权限,从而实现不同权限的用户登录之后使用的功能不一样,首先先看下数据库 总共有5张表,qx_user,qx_rules和qx_jues ...
- SSM框架---搭建
SSM框架简介 SSM框架,是spring + spring MVC + MyBatis的缩写,这个是继SSH之后,目前比较主流的Java EE企业级框架,适用于搭建各种大型的企业级应用系统. Spr ...
- [算法][LeetCode]Single Number——异或运算的巧妙运用
题目要求 Given an array of integers, every element appears twice except for one. Find that single one. N ...
- 二项分布。计算binomial(100,50,0.25)将会产生的递归调用次数(算法第四版1.1.27)
算法第四版35页问题1.1.27,估计用一下代码计算binomial(100,50,0.25)将会产生的递归调用次数: public static double binomial(int n,int ...
- HTML 之轮播图
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- ActiveMQ 详解
1. 如何同步索引库 方案一: 在taotao-manager中,添加商品的业务逻辑中,添加一个同步索引库的业务逻辑; 缺点:业务逻辑耦合度高,业务拆分不明确; 方案二: 业务逻辑在taotato-s ...
- Flask使用mysql数据池
helper.py import pymysql from settings import Config def connect(): conn = Config.POOL.connection() ...