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

  1. 5
  2. 1 2
  3. 2 4
  4. 2 4
  5. 6 4
  6. 2 1

Sample Output

  1. 2 -
  2. 5 +
  3. 1 +
  4. 3 +
  5. 4 -

题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=101

把0-6当成点,输入的n个当成边。这样就形成了一个无向图。

答案就是求一个欧拉路径。

相关知识可以参考:http://blog.chinaunix.net/uid-26380419-id-3164913.html

一个是判断连通,然后度为奇数的点为0个或者2个,才有欧拉路径。

欧拉路径的求法dfs就可以了,很奇妙!

  1. /* ***********************************************
  2. Author :kuangbin
  3. Created Time :2014-2-1 0:46:43
  4. File Name :E:\2014ACM\SGU\SGU101.cpp
  5. ************************************************ */
  6.  
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <iostream>
  10. #include <algorithm>
  11. #include <vector>
  12. #include <queue>
  13. #include <set>
  14. #include <map>
  15. #include <string>
  16. #include <math.h>
  17. #include <stdlib.h>
  18. #include <time.h>
  19. using namespace std;
  20.  
  21. struct Edge
  22. {
  23. int to,next;
  24. int index;
  25. int dir;
  26. bool flag;
  27. }edge[];
  28. int head[],tot;
  29. void init()
  30. {
  31. memset(head,-,sizeof(head));
  32. tot = ;
  33. }
  34. void addedge(int u,int v,int index)
  35. {
  36. edge[tot].to = v;
  37. edge[tot].next = head[u];
  38. edge[tot].index = index;
  39. edge[tot].dir = ;
  40. edge[tot].flag = false;
  41. head[u] = tot++;
  42. edge[tot].to = u;
  43. edge[tot].next = head[v];
  44. edge[tot].index = index;
  45. edge[tot].dir = ;
  46. edge[tot].flag = false;
  47. head[v] = tot++;
  48. }
  49. int du[];
  50. int F[];
  51. int find(int x)
  52. {
  53. if(F[x] == -)return x;
  54. else return F[x] = find(F[x]);
  55. }
  56. void bing(int u,int v)
  57. {
  58. int t1 = find(u);
  59. int t2 = find(v);
  60. if(t1 != t2)
  61. F[t1] = t2;
  62. }
  63. vector<int>ans;
  64. void dfs(int u)
  65. {
  66. for(int i = head[u]; i != -;i = edge[i].next)
  67. if(!edge[i].flag )
  68. {
  69. edge[i].flag = true;
  70. edge[i^].flag = true;
  71. dfs(edge[i].to);
  72. ans.push_back(i);
  73. }
  74. }
  75.  
  76. int main()
  77. {
  78. //freopen("in.txt","r",stdin);
  79. //freopen("out.txt","w",stdout);
  80. int n;
  81. while(scanf("%d",&n) == )
  82. {
  83. init();
  84. int u,v;
  85. memset(du,,sizeof(du));
  86. memset(F,-,sizeof(F));
  87. for(int i = ;i <= n;i++)
  88. {
  89. scanf("%d%d",&u,&v);
  90. addedge(u,v,i);
  91. du[u]++;
  92. du[v]++;
  93. bing(u,v);
  94. }
  95. int s = -;
  96. int cnt = ;
  97. for(int i = ;i <= ;i++)
  98. {
  99. if(du[i]&) cnt++;
  100. if(du[i] > && s == -)
  101. s = i;
  102. }
  103. bool ff = true;
  104. if(cnt != && cnt != )
  105. {
  106. printf("No solution\n");
  107. continue;
  108. }
  109. for(int i = ; i <= ;i++)
  110. if(du[i] > && find(i) != find(s))
  111. ff = false;
  112. if(!ff)
  113. {
  114. printf("No solution\n");
  115. continue;
  116. }
  117. ans.clear();
  118. if(cnt == )dfs(s);
  119. else
  120. {
  121. for(int i = ;i <= ;i++)
  122. if(du[i] & )
  123. {
  124. dfs(i);
  125. break;
  126. }
  127. }
  128. for(int i = ;i < ans.size();i++)
  129. {
  130. printf("%d ",edge[ans[i]].index);
  131. if(edge[ans[i]].dir == )printf("-\n");
  132. else printf("+\n");
  133. }
  134. }
  135. return ;
  136. }

SGU 101 Domino (输出欧拉路径)的更多相关文章

  1. SGU 101 Domino【欧拉路径】

    题目链接: http://acm.sgu.ru/problem.php?contest=0&problem=101 题意: N个多米诺骨牌,每个骨牌左右两侧分别有一个0~6的整数(骨牌可以旋转 ...

  2. SGU 101.Domino( 欧拉路径 )

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

  3. sgu 101 Domino 解题报告及测试数据

    101. Domino time limit per test: 0.25 sec. memory limit per test: 4096 KB 题解: 求多米诺骨牌按照一定方式放置能否使相邻的位置 ...

  4. SGU 101.Domino (欧拉路)

    时间限制: 0.5 sec 空间限制: 4096 KB 描述 多米诺骨牌,一种用小的方的木块或其他材料,每个都被一些点在面上标记,这些木块通常被称为骨牌.每个骨牌的面都被一条线分成两个   方形,两边 ...

  5. SGU 101 Domino 题解

    鉴于SGU题目难度较大,AC后便给出算法并发布博文,代码则写得较满意后再补上.——icedream61 题目简述:暂略 AC人数:3609(2015年7月20日) 算法: 这题就是一笔画,最多只有7个 ...

  6. sgu 101 domino

    题意还算简洁明了,加上有道翻译凑过着读完了题.题意大体上是 给你 n 个多米诺骨牌, 给出每个骨牌两端的数字, 只有数字相同才可以推到, 比如 2-3和3-2.你可以旋转这些多米诺骨牌, 输出一个可以 ...

  7. ACM: SGU 101 Domino- 欧拉回路-并查集

    sgu 101 - Domino Time Limit:250MS     Memory Limit:4096KB     64bit IO Format:%I64d & %I64u Desc ...

  8. poj 2337 有向图输出欧拉路径

    Catenyms Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10186   Accepted: 2650 Descrip ...

  9. SGU 101

    SGU 101,郁闷,想出来算法,但是不知道是哪个地方的问题,wa在第四个test上. #include <iostream> #include <vector> #inclu ...

随机推荐

  1. 【问题收集·知识储备】Xcode只能选择My Mac,不能选择模拟器如何解决?

      网友问题:请问打开一个应用,只能选择My Mac,不能选择模拟器如何解决? 答案:             下面将问答过程记录如下:     CHENYILONG Blog 请问打开一个应用,只能 ...

  2. Android手机间无线互传功能探索及实现

    年前研究了一下Android如何实现无线互传的功能,写了个小demo,最近无事,遂整理一下,与各位共享.前期调研发现,Android想要实现无线互传有以下几种技术:1,Bluetooth通行已久,简单 ...

  3. SQLSTATE[42000]

    SQLSTATE[42000]: Syntax error or access violation: 1140 Mixing of GROUP columns (MIN(),MAX(),COUNT() ...

  4. Codeforces 238 div2 B. Domino Effect

    题目链接:http://codeforces.com/contest/405/problem/B 解题报告:一排n个的多米诺骨牌,规定,若从一边推的话多米诺骨牌会一直倒,但是如果从两个方向同时往中间推 ...

  5. CF293B 方格(带技巧的搜索)

    solution: 首先我们根据一条路径上不能有两个相同颜色的格子可以得出: 对于两个格子 \((x_1 , y_1 )\) 和 \((x_2 , y_2 )\) 必须满足: \(x_1<x_2 ...

  6. Chrome插件:gitlab activity dashboard background-color

    背景 我一般都是在activity dashboard页看同事的提交记录,这样只要我有权限的项目有人提交了我就能够知道,虽然提交的具体代码压根不看.......但至少能够了解各个项目的开发情况(如果大 ...

  7. Ubuntu 14.04 + gnome session back (metacity) 任务栏右上角图标消失问题解决

    没错, 就是说右上角的所有图标 (时间啊, 系统啊所有的)都消失了. 通过下列命令可以恢复 dconf reset -f /org/gnome/gnome-panel/ 参考这篇帖子: Upgrade ...

  8. 阿里云OSS 中文名称地址不对

    oss中将该中文名称重命名.再输入一次

  9. CRT/LCD/VGA Information and Timing【转】

    转自:http://www.cnblogs.com/shangdawei/p/4760933.html 彩色阴极射线管的剖面图: 1. 电子QIANG Three Electron guns (for ...

  10. linux内存管理-内核用户空间 【转】

    转自:http://blog.chinaunix.net/uid-25909619-id-4491362.html 1,linux内存管理中几个重要的结构体和数组 page unsigned long ...