/*
欧拉路径 之 poj 2513 Colored Sticks欧拉路径: 若图G中存在这样一条路径,使得它恰通过G中每条边一次,则称该路径为欧拉路径。无向图存在欧拉路径 充要条件:1) 图是连通的;2) 所有节点的度为偶数,或者有且只有两个度为奇数的节点。题目要求:align the sticks in a straight line such that the colors of the endpoints that touch are of the same color.模型转换:木棒看作边,木棒两端看作节点(相同的颜色即为同一个节点),这样便可构成无向图G。align the sticks in a straight line 即:在无向图G中,通过每条边一次。即:判定无向图G是否存在欧拉路径实现:字典树 + 并查集*/
  1 #include <crtdbg.h>
  2 #include <iostream>
  3 #include <fstream>
  4 #include <sstream>
  5 #include <cstdlib>
  6 #include <cstdio>
  7 #include <cstddef>
  8 #include <iterator>
  9 #include <algorithm>
 10 #include <string>
 11 #include <locale>
 12 #include <cmath>
 13 #include <vector>
 14 #include <cstring>
 15 #include <map>
 16 #include <utility>
 17 #include <queue>
 18 #include <stack>
 19 #include <set>
 20 #include <functional>
 21 using namespace std;
 22 typedef pair<int, int> PII;
 23 typedef long long int64;
 24 const int INF = 0x3f3f3f3f;
 25 const int modPrime = 3046721;
 26 const double eps = 1e-9;
 27 const int MaxN = 500010;
 28 const int MaxM = 10010;
 29
 30
 31 int degree[MaxN];
 32 int ftr[MaxN];
 33 int rnk[MaxN];
 34
 35 //Union-Find Sets
 36 void ufsIni()
 37 {
 38     for (int i = 0; i < MaxN; ++i)
 39     {
 40         ftr[i] = i;
 41         rnk[i] = 0;
 42     }
 43 }
 44 int ufsFind(int x)
 45 {
 46     if (x == ftr[x]) return x;
 47     return ftr[x] = ufsFind(ftr[x]);
 48 }
 49 void ufsUnite(int x, int y)
 50 {
 51     x = ufsFind(x);
 52     y = ufsFind(y);
 53     if (x == y) return;
 54
 55     if (rnk[x] > rnk[y])
 56     {
 57         ftr[y] = x;
 58     }
 59     else
 60     {
 61         ftr[x] = y;
 62         if (rnk[x] == rnk[y])
 63         {
 64             ++rnk[y];
 65         }
 66     }
 67 }
 68
 69 // Tree
 70 struct Tree
 71 {
 72     Tree *next[26];
 73     bool flag;
 74     int num;
 75     Tree()
 76     {
 77         flag = false;
 78         memset(next, NULL, sizeof(next));
 79     }
 80 };
 81
 82 Tree *Root = new Tree;
 83
 84 int insertNode(string str, int num)
 85 {
 86     Tree *p = Root;
 87     for (int i = 0; i < str.size(); ++i)
 88     {
 89         int pos = str[i] - 'a';
 90         if (!(p->next[pos]))
 91         {
 92             p->next[pos] = new Tree;
 93         }
 94         p = p->next[pos];
 95     }
 96     if (!(p->flag))
 97     {
 98         p->flag = true;
 99         p->num = num;
100     }
101     return p->num;
102 }
103
104 void destroyTree(Tree *root)
105 {
106     for (int i = 0; i < 26; ++i)
107     {
108         if (root->next[i])
109         {
110             destroyTree(root->next[i]);
111         }
112     }
113     delete[] root;
114 }
115
116
117 int main()
118 {
119 #ifdef HOME
120     freopen("in", "r", stdin);
121     //freopen("out", "w", stdout);
122 #endif
123
124     char str1[11], str2[11];
125     int num = 0;
126     fill(degree, degree + MaxN, 0);
127     ufsIni();
128     while (~scanf("%s %s", str1, str2))
129     {
130         int numTmp1 = insertNode(str1, num);
131         if (numTmp1 == num)
132         {
133             ++num;
134         }
135         int numTmp2 = insertNode(str2, num);
136         if (numTmp2 == num)
137         {
138             ++num;
139         }
140
141         ++degree[numTmp1];
142         ++degree[numTmp2];
143         ufsUnite(numTmp1, numTmp2);
144     }
145     destroyTree(Root);
146     for (int i = 1; i < num; ++i)
147     {
148         if (ftr[i] != ftr[0])
149         {
150             printf("Impossible\n");
151             return 0;
152         }
153     }
154     int oddCnt = 0;
155     for (int i = 0; i < num; ++i)
156     {
157         if (degree[i] & 1)
158         {
159             ++oddCnt;
160             if (oddCnt >= 3)
161             {
162                 break;
163             }
164         }
165     }
166     if (oddCnt == 0 || oddCnt == 2)
167     {
168         printf("Possible\n");
169     }
170     else
171     {
172         printf("Impossible\n");
173     }
174
175
176 #ifdef HOME
177     cerr << "Time elapsed: " << clock() / CLOCKS_PER_SEC << " ms" << endl;
178     _CrtDumpMemoryLeaks();
179 #endif
180     return 0;
181 }

 

转载于:https://www.cnblogs.com/shijianming/p/5099437.html

欧拉路径 之 poj 2513 Colored Sticks相关推荐

  1. poj 2513 Colored Sticks( 字典树哈希+ 欧拉回路 + 并查集)

    题目:http://poj.org/problem?id=2513 参考博客:http://blog.csdn.net/lyy289065406/article/details/6647445 htt ...

  2. POJ - 2513 Colored Sticks(字典树+并查集+欧拉回路)

    题目链接:点击查看 题目大意:给出n个木棍,问若两两相连,最终能否构成一根长直木棍,相连的规则是两个木棍的相接端点的颜色需要保持相同 题目分析:关于这个题目,我们可以将每个木棍视为一条边,每个木棍的两 ...

  3. POJ - 2513 Colored Sticks 欧拉通路+并查集+静态树

    一开始想用map来搞,但是感觉好复杂,然后想了一下看大佬们用trie做的,感觉十分合理就敲了一发. 一开始re,数组要开到550000 只会静态的字典树,在每个根节点看是否出现过改颜色,如果没有就把该 ...

  4. POJ 2513 Colored Sticks

    POJ_2513 这个题目实际上要求木棍能够组成一个欧拉道路即可.为了练字典树所以用字典树写的,当然,这个题目用哈希做也可以. 需要注意的是由于木棍有25W,因此不同的顶点最多有50W,所以存储图顶点 ...

  5. poj 2513 Colored Sticks

    // 判断图是否联通 在连通的基础上还要判断是否存在欧拉通路// 判断连通就并查集了 判断是否存在欧拉通路: 点度数为数的点 ==1 >=3就是不存在的 其它是存在的// 我开始用 map 判重 ...

  6. poj 2513(欧拉路径+字典树映射)

    题目链接:http://poj.org/problem?id=2513 思路:题目还是很简单的,就是判断是否存在欧拉路径,我们给每个单词的头和尾映射序号,统计度数.对于给定的无向图,当且仅当图连通并且 ...

  7. 判断线段相交 + vector. erase迭代指针 的使用 poj 2653 Pick-up sticks

    题目来源:http://poj.org/problem?id=2653 分析: 题意:按顺序给出一些木棍,输出在最上面的木棍标号. 用vector 存储木棍标号, 当前木棍与 vector 中的木棍 ...

  8. 【代码超详解】ZOJ 2551 / POJ 2653 Pick-up Sticks(快速排斥实验 + 跨立实验判断线段是否相交 · 模板)

    一.传送门 http://poj.org/problem?id=2653 https://zoj.pintia.cn/problem-sets/91827364500/problems/9182736 ...

  9. POJ 2653 Pick-up sticks (线段相交)

    题意:给你n条线段依次放到二维平面上,问最后有哪些没与前面的线段相交,即它是顶上的线段 题解:数据弱,正向纯模拟可过 但是有一个陷阱:如果我们从后面向前枚举,找与前面哪些相交,再删除前面那些相交的线段 ...

最新文章

  1. c语言logout_C++ 格式化日志输出实现代码
  2. linux独立应用程序开发,Linux应用程序开发(一)
  3. 全球首个机器人抓取云竞赛落幕,华科夺冠,中国团队包揽前三
  4. 驰骋工作流引擎设计系列04 流程引擎表结构的设计
  5. winpython使用教程-使用Python开发windows桌面程序【超简单】
  6. 城市规划理论1 选址理论
  7. CSS添加多个背景图片
  8. 【问链-Eos公开课】第四课 EOS 的钱包创建、导入私钥
  9. 运行时异常和检查性异常区别
  10. Azure系列1.1.2 —— 用于 IntelliJ 的 Azure 工具包的登录说明
  11. 关于ajax入门案例
  12. 启动tomcat报错,Failed to start component
  13. 利尔达NB-IOT的PSM和eDRX低功耗模式笔记
  14. Python狼人之夜--文字冒险游戏
  15. Wireshark实战分析之IP协议(二)
  16. 抖音上显示内部服务器错误,抖音被限流了怎么办?这里分析了原因和解决方法...
  17. app提交到iTunes失败
  18. 【人物志】美团前端通道主席洪磊:一位产品出身、爱焊电路板的工程师
  19. Inspector检视视图
  20. 即席查询(Ad Hoc)如何做到又快又稳?

热门文章

  1. 斯坦福大学Andrew Ng - 机器学习笔记(3) -- 神经网络模型
  2. Fedora 23如何安装LAMP服务器
  3. HDU 1712 裸分组dp
  4. 滴水穿石--Pydoop 架构和模块包介绍
  5. (转)资料收集,新手必备的sql数据导入导出知识
  6. 初学flex时候搞得一个大头贴工具(开源)
  7. leetcode算法题--二叉搜索树的后序遍历序列
  8. 哪个学校计算机系学大物,计算机系各专业专业及名校介绍
  9. 安卓linux定时执行脚本,Android开机自动执行shell脚本
  10. python import变量_Python import模块调用