字典树 hash(代替map的映射) PKU2513
2016-04-30 08:36
495 查看
题意就是看能不能形成欧拉图,有没有一条欧拉道路,
图为无向图,那么需要具备以下几点才能满足,联通且(最多)有2个奇数点
Colored Sticks
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
Sample Output
代码
:
图为无向图,那么需要具备以下几点才能满足,联通且(最多)有2个奇数点
Colored Sticks
Time Limit: 5000MS | Memory Limit: 128000K | |
Total Submissions: 34526 | Accepted: 9009 |
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 red red violet cyan blue blue magenta magenta 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"); }
相关文章推荐
- opencv基础学习笔记
- 机器学习之单变量线性回归(Linear Regression with One Variable)
- Codeforces Round #349 (Div. 1) A. Reberland Linguistics 动态规划
- 机器学习之单变量线性回归(Linear Regression with One Variable)
- java字符串处理
- 3670: [Noi2014]动物园
- 自定义控件
- Elasticsearch 聚合作用域(Scoping Aggregations)
- 五、JAVA数组
- bzoj4459 丢番图
- 【笔试/面试】—— 数学找规律题
- 开源新闻速递(160430):GNOME 3.22 开始开发
- matplotlib作图中文显示问题
- curl 模拟登陆
- NoSQL数据库技术
- 5-48 银行排队问题之单窗口“夹塞”版 (30分)
- C语言:关于程序运行时的内存管理
- Elasticsearch 聚合 - 时间数据处理(Looking at Time)
- Jetbrains IntelliJ IDEA破解方法
- Java类和对象及实例