poj 2513(trie树+并查集+欧拉回路条件)(记得要初始化指针数组)

来源:互联网 发布:职业摄影师知乎 编辑:程序博客网 时间:2024/05/16 17:51
Colored Sticks
Time Limit: 5000MS Memory Limit: 128000KTotal Submissions: 28302 Accepted: 7481

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 redred violetcyan blueblue magentamagenta cyan

Sample Output

Possible

Hint

Huge input,scanf is recommended.

Source

The UofA Local 2000.10.14

非常沮丧的 RE 多次。
trie树用链表来实现吧:
结构体中的 next指针用来寻找下一个字母node。
isdict变量表示这个结点是不是一个词典中的单词。(必须有!)
struct node {    node * next[26];    bool isdict;};

然后遍历输入串,在树上行走。发现NULL指针就new一个。一定要注意new后要把新结点的next指针都初始化为NULL。否则可能是野指针,导致RE。
    while(s[i]) {        int a = s[i] - 'a';        assert(a < 26);        if (cur->next[a] == NULL) {            cur->next[a] = new node;            memset(cur->next[a]->next, NULL, sizeof(cur->next[a]->next));            cur->next[a]->f = false;        }           cur = cur->next[a];        i++;    }   

首先要判断图的联通(用并查集后,所有点的root相同)
其次要判断入度为奇数的点,或者为0,或者为2。
以上两个条件都满足时,则必存在欧拉回路,否则必不存在欧拉回路。


提交记录:
1-N、RE。 记得要初始化指针。否则结果不可预知。
N+1、AC。



原创粉丝点击