题目:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=11158
Time Limit: 5000MS | | Memory Limit: 128000KB | | 64bit IO Format: %I64d & %I64u |
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
Hint
Huge input,scanf is recommended.
开始想用map<string,int>写的,但是string是cin输入啊,而hint的提示要用scanf。这可怎么办?后来发现了神奇的字典树,轻便高效值得拥有!映射的问题解决了,然后就是欧拉路径/回路的判断了。
相关知识点:
欧拉回路和欧拉路径的判断
欧拉回路:
无向图:每个顶点的度数都是偶数,则存在欧拉回路。
有向图:每个顶点的入度都等于出度,则存在欧拉回路。
欧拉路径:
无向图:当且仅当该图所有顶点的度数为偶数 或者 除了两个度数为奇数外其余的全是偶数。
有向图:当且仅当该图所有顶点 出度=入度 或者 一个顶点 出度=入度+1,另一个顶点 入度=出度+1,其他顶点 出度=入度。
本题和hdu 1116(上一篇博客)稍有不同,这是无向图,那个是有向图(单词不可能倒着写)。判定条件不要混淆。