POJ - 2513 Colored Sticks
来源:互联网 发布:淘宝商品数据包 编辑:程序博客网 时间:2024/05/21 07:08
题意:有一些木棍,木棍的两边各有一种颜色,如果两根木棍的一边颜色相同的话,那么就可以连在一起,问能不能完全连成一根
思路:不在是简单的欧拉路,如果能将颜色表达成一个数字的话就能转化为欧拉路了,
用Trie树来优化,再用并查集判断是否为欧拉路,将pre[]初始化为-1,加快速度
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <map>using namespace std;const int N = 15;const int M = 1000010;struct Trie{ int num; Trie *next[26];}*Head;Trie trie[M];char u[N],v[N];int num,n,top,total,flag;int pre[M],in[M];int Find_set(int x){ return pre[x] == -1 ? x : (pre[x] = Find_set(pre[x]));}void Make_Set(int x,int y){ int root1 = Find_set(x); int root2 = Find_set(y); if (root1 != root2) pre[root2] = root1;}int insert(char *str){ Head = &trie[0]; int len = strlen(str); for (int i = 0; i < len; i++){ int temp = str[i] - 'a'; if (Head->next[temp] == NULL) Head->next[temp] = &trie[++num]; Head = Head->next[temp]; } if (Head->num == 0) Head->num = top++; return Head->num;}void init(){ num = total = 0,top = 1,flag = 1; memset(in,0,sizeof(in)); memset(pre,-1,sizeof(pre)); for (int i = 0; i < 20; i++){ trie[i].num = 0; for (int j = 0; j < 26; j++) trie[i].next[j] = NULL; }}int main(){ int tmp,temp; init(); while (scanf("%s %s",u,v) != EOF){ tmp = insert(u),temp = insert(v); in[tmp]++,in[temp]++; Make_Set(tmp,temp); } int root = Find_set(1); for (int i = 1; i < top; i++){ if (root != Find_set(i)){ flag = 0; break; } if (in[i] & 1) total++; if (total > 2) break; } if ((total == 0 || total == 2) && flag == 1) printf("Possible\n"); else printf("Impossible\n"); return 0;}
0 0
- POJ 2513 Colored Sticks
- poj 2513 Colored Sticks
- POJ 2513 Colored Sticks
- poj 2513 colored sticks
- Poj 2513 Colored Sticks
- POJ 2513 Colored Sticks
- POJ 2513 Colored Sticks
- POJ 2513 Colored Sticks
- poj 2513 Colored Sticks
- poj 2513 Colored Sticks
- POJ 2513 Colored Sticks
- POJ 2513 Colored Sticks
- Poj 2513 Colored Sticks
- poj 2513 Colored Sticks
- POJ 2513 Colored Sticks
- POJ 2513 Colored Sticks
- poj 2513 Colored Sticks
- POJ 2513 Colored Sticks
- 那些年,我读过的技术书(Java中篇)
- Android 2.2.2到Android 4.2.2源码下载地址
- PHP数组的Hash冲突实例
- MATLAB sort函数应用——求一个矩阵前L个最大值及其在矩阵中的位置
- python Django框架的配置,以及用django搭建一个blog
- POJ - 2513 Colored Sticks
- Mongodb 的ORM框架 Morphia 之 Query接口
- Cavas绘图移动游戏背景呈现前进效果(雷电战机一)
- 工厂方法模式
- Python暴力破解的收集
- 读书笔记--盗梦工厂--2014年03月05日
- 集合中ArrayList的使用
- OpenMP学习(三)
- Android R文件无法恢复问题