poj2513

maksyuki 发表于 oj 分类,标签:
0

Colored Sticks

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.

Source

The UofA Local 2000.10.14

 

题目类型:无向图欧拉回路(路径)的判定

算法分析:将颜色看成节点,连接颜色的木棒看成一条无向边。使用并查集判断构建的图中是否连通,然后统计每一个节点的奇数度数的个数,直接使用欧拉路径和回路的判断定理判断即可。注意需要使用trie树或者是hash表进行字符串的快速检索和插入,直接使用O(n)的算法会超时。注意数据中可能出现自环,即代码中的id可能最小取值到0,所以在判断连通性时下标需要从0开始!!!下面会给出使用Trie树实现的代码