POJ 2513 Colored Sticks
来源:互联网 发布:淘宝店铺怎么传图片 编辑:程序博客网 时间:2024/06/03 21:11
这题是求欧拉路径,给出字符串,map映射会超时,用Trie树来映射。
一个图有欧拉路径的充分必要条件是这个图是连通的且每一个顶点的度都是偶数或者且仅有两个顶点的度是奇数。联通可以用并查集来判断。
#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>using namespace std;int fa[510000],trie[510000][30],dev[510000],id[510000],sz,num;int getfa(int x){ return fa[x] == x ? x : fa[x] = getfa(fa[x]);}void init(){ for(int i = 0; i < 510000; i++) fa[i] = i; memset(trie,0,sizeof(trie)); memset(dev,0,sizeof(dev)); memset(id,0,sizeof(id)); sz = 1; num = 0;}int Insert(char *s){ int cur = 1; for(int i = 0; s[i]; i++) { int c = s[i] - 'a'; if(!trie[cur][c]) { trie[cur][c] = ++sz; } cur = trie[cur][c]; } dev[cur]++; if(dev[cur] == 1) id[cur] = ++num; return id[cur];}int main(){ char s1[15],s2[15]; init(); while(scanf("%s%s",s1,s2)!=EOF) { int a = Insert(s1); int b = Insert(s2); //printf("%s %s\n",s1,s2); int x = getfa(a); int y = getfa(b); if(x != y) fa[y] = x; } if(num == 0) { printf("Possible\n"); return 0; } int count1 = 0; for(int i = 1; i <= sz; i++) { if(dev[i]&1) count1 ++;//计算度为奇的点的个数。 if(count1 > 2) break; } int count2 = 0; for(int i = 1; i <= num; i++) if(fa[i] == i) count2++;//count2 == 1说明联通。 if(count2 == 1 && (count1 == 0 || count1 == 2)) 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
- 书山有路勤为径
- 编译原理——引论
- 【Jason's_ACM_解题报告】Fill
- Modern计算器—提前体验Windows10的计算器
- Make 命令教程
- POJ 2513 Colored Sticks
- bzoj 2962 序列操作(线段树)
- Ubuntu14.04安装OpenCV2.9
- windows 不能在本地计算机中起动Tomcat参考特定错误代码4
- POJ 2531-Network Saboteur(dfs+剪枝)
- 南阳理工OJ 迷宫寻宝(一)DFS
- 最小二乘法2个变量
- 黑马程序员——Java概述
- 字符串翻转