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 blue red red violet cyan blue blue magenta magenta cyan Sample Output Possible Hint Huge input,scanf is recommended.
Source |
详细解释:http://blog.csdn.net/lyy289065406/article/details/6647445
#include<iostream> #include<cstdio> #include<cstring> #include<vector> using namespace std; struct Trie{ int x; Trie *next[26]; }*root,memory[5100000]; int deg[5100000],father[510000],cnt,tot; Trie *create(){ Trie *p=&memory[cnt++]; for(int i=0;i<26;i++) p->next[i]=NULL; return p; } void InsertTrie(char *str,int x){ Trie *loc=root; for(int i=0;str[i]!='