POJ 2513

Colored Sticks

Time Limit: 5000MS   Memory Limit: 128000K
Total Submissions: 41270   Accepted: 10684

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做大规模输入超时。

此外通过并查集判断是否构成通路,通过节点的度判断是否存在欧拉通路:所有点的度均为偶数或者只有两个点的度是奇数。

 

int insert(tree *root, char *word) {
    tree *p = root;
    int i = 0;
    while(word[i] != '\0') {
        if (p->next[word[i]-'a'] == NULL) {
            tree *temp = new tree;
            temp->isWord = false;
            for (int j = 0;j < MAX;j++) {
                temp->next[j] = NULL;
            }
            temp->isWord = false;
            temp->id = 0;
            p->next[word[i]-'a'] = temp;
        }
        p = p->next[word[i]-'a'];
        i++;
    }
    if (p->isWord) {
        return p->id;
    }
    else {
        p->isWord = true;
        p->id = color++;
        return p->id;
    }
}

你可能感兴趣的:(北邮acm50题题解)