洛谷.1333.瑞瑞的木棍(欧拉路径 Hash)
2018-03-03 09:46
344 查看
题目链接
#include <cstdio> #include <cstring> const int N=2e6+5,M=5e5+5,mod=2e6; const int seed[5]={31,37,131,41}; int dgr ,fa[M]; char s[15]; namespace Hash { int cnt,val ,pos ; int Get_Hash(char *s) { int x=0,l=strlen(s); for(int i=0; i<l; ++i) x=(x+s[i]*seed[i%4])%mod; return x; } int Insert(char *s) { int p=Get_Hash(s); while(val[p]&&val[p]!=p){ ++p; if(p>=mod) p-=mod; } if(val[p]) return pos[p]; val[p]=p; return pos[p]=++cnt; } } int Find(int x){ return x==fa[x]?x:fa[x]=Find(fa[x]); } int main() { int p1,p2,r1,r2,t=0; for(int i=1; i<M; ++i) fa[i]=i; while(~scanf("%s",s)) { p1=Hash::Insert(s); scanf("%s",s); p2=Hash::Insert(s); ++dgr[p1], ++dgr[p2]; r1=Find(p1), r2=Find(p2); if(r1!=r2) ++t,fa[r1]=r2; } if(t<Hash::cnt-1) {printf("Impossible"); return 0;} int tot=Hash::cnt; t=0; for(int i=1; i<=tot; ++i) if(dgr[i]&1) ++t; printf(t>2?"Impossible":"Possible"); return 0; }
相关文章推荐
- POJ 2513 Colored Sticks 字典树hash 欧拉路径
- 欧拉回路——洛谷 P1333 瑞瑞的木棍
- 洛谷P1333 瑞瑞的木棍(欧拉回路)
- 欧拉路径问题 (附POJ 1041 POJ 2337)
- 洛谷P1730 最小密度路径(floyd)
- 混合边的欧拉路径——poj1637 最大流
- 欧拉路径, 欧拉回路 USACO Riding the Fences
- 欧拉路径&&欧拉回路
- Riding the Fences USACO 3.3 欧拉路径(dfs+floodfill)
- 寻找最小生成树的欧拉路径,即一笔画问题
- AC日记——最小路径覆盖问题 洛谷 P2764
- hiho第五十周——欧拉路·二(Fleury算法求欧拉路径)
- 【最短路径】洛谷 P1529 回家 Bessie Come Home
- 【欧拉回路】【欧拉路径】【Fleury算法】CDOJ1634 记得小苹初见,两重心字罗衣
- 算法录 之 拓扑排序和欧拉路径。
- 图论欧拉路径问题(单词接龙)
- Fleury(弗罗莱)算法求欧拉路径
- poj 2337 之 有向图 欧拉路径输出
- poj 2513 Colored Sticks(字典树+欧拉路径+并查集)
- hash+dp打印路径 Codeforces615C Running Track