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 red red violet cyan blue blue magenta magenta cyan
Sample Output
Possible
题意:
有一些木棒,两头有对应的颜色,把这些木棒头上相同的颜色拼接起来,问是否能够连成一条直线,就像例子中给的输入一样,换成对应的图为:
解题思路:
有问题出发,寻找解题的办法,既然是问是否能够组成一个欧拉路,这就涉及到了欧拉路的判断方法,另外,要构成图,就要知道有多少种颜色,每种颜色有多少个,这就涉及到了字典树的创建和查询(用别的方法时间复杂度会比较高),判断图是否能够联通,需要用到并查集。
(1).字典树
每输入一种颜色,就在字典树里查询,如果已经存在这种颜色,则让记录颜色个数的相应数组加1即可,如果没有这种颜色,则创建这种颜色的树枝,并生成这种颜色的对应编号(可以从1开始)。
(2).并查集
初始化所输入的n个结点为n棵树,那么就有一个n棵树的森林,此时每棵树的有唯一的结点(根),该结点的祖先就是它本身。再通过不断地输入边,得到某两个结点(集合)之间的关系,进而合并这两个结点(集合),那么这两个集合就构成一个新的集合,集合内的所有结点都有一个共同的新祖先,就是这个集合(树)的根。最后只要枚举任意一个结点,他们都具有相同的祖先,那么就能证明图时连通的了。
需要注意的是:使用并查集时必须压缩路径,前几次搜索某个结点k的祖先时,在不断通过父亲结点寻找祖先结点时,顺便把从k到最终祖先结点S中经过的所有结点的祖先都指向S,那么以后的搜索就能把时间降低到O(1)。
另外,并查集用到的是数组的下标,而这里颜色是字符串的形式,所以就用到了字典树返回的颜色的编号作为下标。
(3).欧拉路:
1.无向连通图G是欧拉图,当且仅当G不含奇数度结点(G的所有结点度数为偶数);
2.无向连通图G含有欧拉通路,当且仅当G有零个或两个奇数度的结点;3.有向连通图D是欧拉图,当且仅当该图为连通图且D中每个结点的入度=出度4.有向连通图D含有欧拉通路,当且仅当该图为连通图且D中除两个结点外,其余每个结点的 入度=出度,且此两点满足deg-(u)-deg+(v)=±1。(起始点s的 入度=出度-1,结束点t的出度=入度-1 或两个点的入度=出度)5.一个非平凡连通图是欧拉图当且仅当它的每条边属于奇数个环。 6.如果图G是欧拉图且 H = G - uv,则H有奇数个u,v-迹仅在最后访问v;同时,在这一序列的u,v-迹中,不是路径的迹的条数是偶数。奇度数结点个数==0 或 ==2 且 图连通 Possible;奇度数结点个数==1 或 >=3 或 图不连通 Impossible;#include <stdio.h> #include <string.h> #include <stdlib.h> #define LARGE 500000 struct node { int js;//判断字符串是否结束 int bh;//记录该颜色所对应的编号 struct node * next[27];//字典树 }*head,*p; int c_num = 0;//记录颜色的总数 int shendu[LARGE+10];//记录每种颜色的个数 int zuxian[LARGE+10];//并查集中判断是否连通 int jsbh(char *s) { int l,i,j; struct node *now; p = head; l = strlen(s); for(i = 0;i < l;i++)//字典树创建和查询 { j = s[i] - 'a'; if(p->next[j] == NULL) { now = (struct node *) malloc (sizeof(struct node)); memset(now,0,sizeof(*now)); p->next[j] = now; } p = p->next[j]; } if(p->js) return p->bh; else { p->js = 1; c_num++; p->bh = c_num; return p->bh; } } int fin(int a) { if(zuxian[a] != a) zuxian[a] = fin(zuxian[a]); return zuxian[a]; } void bing(int i,int j) { int x,y; x = fin(i); y = fin(j); zuxian[y] = x; return; } int main() { int i,j,k,z,num = 0; char s1[11],s2[11]; head = (struct node *)malloc(sizeof(struct node)); memset(head,0,sizeof(*head)); for(i = 0;i <= LARGE;i++) { zuxian[i] = i;//每个点自成一棵树 } while(scanf("%s%s",&s1,&s2) != EOF) { i = jsbh(s1); j = jsbh(s2); shendu[i]++; shendu[j]++; bing(i,j); } z = fin(1); for(i=1;i<=c_num;i++) { if(shendu[i] % 2 == 1) num++; if(num>2) { printf("Impossible\n"); return 0; } if(fin(i)!=z) { printf("Impossible\n"); return 0; } } if(num==1) printf("Impossible\n"); else printf("Possible\n"); return 0; }