字典树 hash(代替map的映射) PKU2513
来源:互联网 发布:少年三国志转盘数据图 编辑:程序博客网 时间:2024/05/12 13:07
题意就是看能不能形成欧拉图,有没有一条欧拉道路,
图为无向图,那么需要具备以下几点才能满足,联通且(最多)有2个奇数点
Colored Sticks
Time Limit: 5000MS Memory Limit: 128000KTotal Submissions: 34526 Accepted: 9009
Description
You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some color. Is it possible to align the sticks in a straight line such that the colors of the endpoints that touch are of the same color?
Input
Input is a sequence of lines, each line contains two words, separated by spaces, giving the colors of the endpoints of one stick. A word is a sequence of lowercase letters no longer than 10 characters. There is no more than 250000 sticks.
Output
If the sticks can be aligned in the desired way, output a single line saying Possible, otherwise output Impossible.
Sample Input
blue redred violetcyan blueblue magentamagenta cyan
Sample Output
Possible
代码
:
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int countn;int countz;int du[500100];int fa[500100];struct node{ int num; struct node *next[30];};node tire[250005];node *creat(){ for(int i=0; i<26; i++) { tire[countn].next[i]=NULL; } tire[countn].num=0; return &tire[countn++];}int hash(node *&root,char s[]){ if(!root) root=creat(); int la=strlen(s); node *p; p=root; for(int i=0; i<la; i++) { if(p->next[s[i]-'a']==NULL) p->next[s[i]-'a']=creat(); p=p->next[s[i]-'a']; } if(!p->num) p->num=countz++; return p->num;}int find(int x){ if(x==fa[x]) { return fa[x]; } else return fa[x]=find(fa[x]);}void Union(int x,int y){ int fx=find(x); int fy=find(y); if(fx!=fy) { fa[fx]=fy; }}int main(){ char s1[30]; char s2[30]; node *root=NULL; memset(du,0,sizeof(du)); countn=0; countz=1; for(int i=0; i<100000; i++) fa[i]=i; while(scanf("%s %s",s1,s2)!=EOF) { int x=hash(root,s1); int y=hash(root,s2); du[x]++; du[y]++; Union(x,y); } //printf("%d \n",countz); int flag=1; int old=0;//奇数的度数大于2个,则不是欧拉图 for(int i=1; i<countz; i++) { //printf("%d ",du[i]); if(du[i]%2) { old++; } if(old>2) { flag=0; break; } if(find(i)!=find(1)) { flag=0; break; } } if(flag) printf("Possible\n"); else printf("Impossible\n");}
0 0
- 字典树 hash(代替map的映射) PKU2513
- Babelfish(字典树-map映射功能)
- 字典映射代替switch
- PKU2513 - Colored Sticks --一道很不错的题(结合了并查集的应用,字典树的应用,欧拉通路的原理)
- PKU2513 - Colored Sticks --一道很不错的题(结合了并查集的应用,字典树的应用,欧拉通路的原理)
- 魔咒词典 字典树 hash map
- HDU1075-字符串映射-字典树&map
- 字典(dictionary)与映射(map)
- 【留坑】 POJ2503 注意输入输出&&几种数据结构的复习、比较(线性表建立的字典 || 树建立的字典_即Trie || hash || map)
- POJ 2503 Babelfish(map,字典树,快排+二分,hash)
- hdu 1800 Flying to the Mars(字典树||map||hash)
- Python数据结构:映射(map)——字典
- poj_2503_Babelfish(字典树&&map)
- hash map的使用
- hdu-1247 简单map的应用。(字典树)
- HDU 1800 Flying to the Mars(贪心 MAP 字典树 HASH)
- HDU-1004-提供四种解法--map解法/自己写二叉排序树/hash/字典树
- Python 字典(Map)的使用
- 3670: [Noi2014]动物园
- Java常量池详解--比较蛋疼的面试题(一)
- java字符串处理
- 机器学习之单变量线性回归(Linear Regression with One Variable)
- 机器学习之单变量线性回归(Linear Regression with One Variable)
- 字典树 hash(代替map的映射) PKU2513
- opencv基础学习笔记
- Introduction to Machine Learning
- 软考二进宫-增强的ER模型
- IntelliJ IDEA 缓存和索引介绍和清理方法
- 整型和字符串相互转换
- 【NOIP practice】BSOJ 3132 卡扎菲 并查集
- Android开发本地及网络Mp3音乐播放器(十一)使用Jsoup组件请求网络,并解析音乐数据
- 机器学习之正则化(Regularization)