poj2513(欧拉回路)
2015-08-18 19:47
218 查看
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
Hint
Huge input,scanf is recommended.
这题其实也是求欧拉回路!只不过变成了字符串!所以怎么办?
办法只有一个:将字符串变数字!!
目前大一的话最好的办法就是字典树了!!我们应该想到字典树的查询时对应的数字是独一无二的吧!所以我们用字典树的好处:1,帮助存储。2,便于查询。3!!将每个字符串化为了一个独一无二的数字!!第3点是最利于我们求欧拉回路的!!!因为有了数就可以开数组!有了数组求个欧拉函数算个啥呀~~~~~~~~~~~~~~~~
在此再次向大牛们提出一个疑问:
这题我按照正常求欧拉回路的方法就是死活不a!!但是看了别人的题解,归纳是:
由图论知识可以知道,无向图存在欧拉路的充要条件为:
① 图是连通的;
② 所有节点的度为偶数,或者有且只有两个度为奇数的节点。
我按照归纳一写就过了
,可是!!我自己写的我觉得是一个意思!!!就hh()函数里面一点点差别~~~~~~~望大牛们看到了指点12!
下面wa代码:
下面是正确代码:
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
Hint
Huge input,scanf is recommended.
这题其实也是求欧拉回路!只不过变成了字符串!所以怎么办?
办法只有一个:将字符串变数字!!
目前大一的话最好的办法就是字典树了!!我们应该想到字典树的查询时对应的数字是独一无二的吧!所以我们用字典树的好处:1,帮助存储。2,便于查询。3!!将每个字符串化为了一个独一无二的数字!!第3点是最利于我们求欧拉回路的!!!因为有了数就可以开数组!有了数组求个欧拉函数算个啥呀~~~~~~~~~~~~~~~~
在此再次向大牛们提出一个疑问:
这题我按照正常求欧拉回路的方法就是死活不a!!但是看了别人的题解,归纳是:
由图论知识可以知道,无向图存在欧拉路的充要条件为:
① 图是连通的;
② 所有节点的度为偶数,或者有且只有两个度为奇数的节点。
我按照归纳一写就过了
,可是!!我自己写的我觉得是一个意思!!!就hh()函数里面一点点差别~~~~~~~望大牛们看到了指点12!
下面wa代码:
#include <iostream> #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; const int NODE = 10000000,CH = 26; int ch[NODE][CH],sz,val[NODE]; int idx(char c) { return c-'a'; } int node() { memset(ch[sz],0,sizeof(ch[sz])); val[sz]=0; return sz++; } void init() { sz=0; node(); } void insert(char *s,int v) { int u=0; for(;*s;s++) { int c=idx(*s); if(!ch[u][c]) ch[u][c]=node(); u=ch[u][c]; } val[u]=v; } int find(char *s) { int u=0; for(;*s;s++) { int c=idx(*s); if(!ch[u][c]) return 0; u=ch[u][c]; } return val[u]; } int f[1000000],rd[1000000],cd[1000000]; int n; int ff(int x) { if(x==f[x])return x; return ff(f[x]); } void hh(int n) { int s=0; for(int i=1;i<=n;i++) if(f[i]==i)s++; // cout<<s<<endl; if(s>1) { printf("Impossible\n"); return ; } else { int z=0,y=0; for(int i=1;i<=n;i++) if(rd[i]!=cd[i]) { if(rd[i]-cd[i]==1) y++; else if(rd[i]-cd[i]==-1) z++; else { printf("Impossible\n"); return ; } } if((y==0&&z==0)||(y==1&&z==1)) { printf("Possible\n"); return ; } else { printf("Impossible\n"); return; } } } int main() { memset(rd,0,sizeof(rd)); memset(cd,0,sizeof(cd)); for(int i=0;i<1000000;i++) f[i]=i; init(); int i=0; char c1[20],c2[20]; while(~scanf("%s%s",c1,c2)) { if(find(c1)==0) insert(c1,++i); if(find(c2)==0) insert(c2,++i); int k1=find(c1),k2=find(c2); f[k1]=f[k2]=ff(k1); rd[k1]++,cd[k2]++; } hh(i); return 0; }
下面是正确代码:
#include <iostream> #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; const int NODE = 10000000,CH = 26; int ch[NODE][CH],sz,val[NODE]; int idx(char c) { return c-'a'; } int node() { memset(ch[sz],0,sizeof(ch[sz])); val[sz]=0; return sz++; } void init() { sz=0; node(); } void insert(char *s,int v) { int u=0; for(;*s;s++) { int c=idx(*s); if(!ch[u][c]) ch[u][c]=node(); u=ch[u][c]; } val[u]=v; } int find(char *s) { int u=0; for(;*s;s++) { int c=idx(*s); if(!ch[u][c]) return 0; u=ch[u][c]; } return val[u]; } int f[1000000],d[1000000]; int n; int ff(int x) { if(x==f[x])return x; return ff(f[x]); } void hh(int n) { int s=0; for(int i=1;i<=n;i++) if(f[i]==i)s++; // cout<<s<<endl; if(s>1) { printf("Impossible\n"); return; } else { int y=0; for(int i=1;i<=n;i++) if(d[i]&1) y++; if(y==2||y==0) { printf("Possible\n"); return ; } else { printf("Impossible\n"); return; } } } int main() { memset(d,0,sizeof(d)); for(int i=0;i<1000000;i++) f[i]=i; init(); int i=0; char c1[20],c2[20]; while(~scanf("%s%s",c1,c2)) { if(find(c1)==0) insert(c1,++i); if(find(c2)==0) insert(c2,++i); int k1=find(c1),k2=find(c2); f[k1]=f[k2]=ff(k1); d[k1]++,d[k2]++; } hh(i); return 0; }
相关文章推荐
- poj - 1426-Find The Multiple-BFS
- 最大子数组和(最大子段和)
- 大龄屌丝自学笔记--Java零基础到菜鸟--004
- 数学概念——H 最美素数
- 首次开机时的默认动态壁纸
- HDOJ 2112 HDU Today(最短路,dijkstra算法)
- [TOJ 2663] Concert Hall Scheduling
- Raw-OS互斥的源代码分析的量的Mutex
- Linux下更改系统时区及时间
- 学习书籍
- HDOJ 5399.Too Simple(2015多校-9的1004)
- hdu 5402 ,Travelling Salesman Problem,2015多校联合训练赛#9
- AE开发之路(一)
- 通过网络获取图片并缓存
- SS2H项目开发历程
- 欢迎使用CSDN-markdown编辑器
- poj-1426 Find The Multiple
- 【Cocos2dx】Windows平台下Cocos2dx 2.x的下载、安装、配置,打造自己的Helloworld
- 浅谈数据结构-树和二叉树之间关系
- 自定义微信图文模板注意事项整理