POJ 2513Colored Sticks(????)

Colored Sticks

Time Limit: 5000MS Memory Limit: 128000K
Total Submissions: 36450 Accepted: 9546

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

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
#include <map>

using namespace std;

struct node
{
    int x;
    node *next[26];
};

int deg[510005], father[510005], cnt, ant;
node *root, memory[5100050];

void init()///初始化并查集的根
{
    int i;
    for(i = 1; i <= 510000; i++)
    {
        father[i] = i;
    }
    memset(deg, 0, sizeof(deg));
    cnt = 0;
}

node *create()///初始化tire树
{
    node *p = &memory[ant++];
    int i;
    for(i = 0; i < 26; i++)
    {
        p->next[i] = NULL;
    }
    return p;
};

int search(char *s)///
{
    node *p = root;
    int i, k;
    for(i = 0; s[i] != '\0'; i++)
    {
        k = s[i] - 'a';
        if(p->next[k] == NULL)
            return 0;
        p = p->next[k];
    }
    return p->x;
}

void insert(char *s, int x)
{
    node *p = root;
    int i, k;
    for(i = 0; s[i]; i++)
    {
        k = s[i] - 'a';
        if(p->next[k] == NULL)
        {
            p->next[k] = create();
        }
        p = p->next[k];
    }
    p->x = x;
}

int find(int x)
{
    if(x != father[x])
    {
        father[x] = find(father[x]);
    }
    return father[x];
//    return father[x]==x?x:father[x];
}

void Union(int x, int y)
{
    father[x] = y;
}

bool judge()    ///判断是否满足欧拉
{
    int i, k, odd = 0;
    for(i = 1; i <= cnt; i++)
    {
        if(deg[i]%2 == 1)
            odd++;
    }
    if(odd != 0 && odd != 2)
        return false;
    k = find(1);
    for(i = 2; i <= cnt; i++)
    {
        if(k != find(i))
            return false;
    }
    return true;
}

int main()
{
    int x, y, fx, fy;
    char s1[15], s2[15];
    init();
    root = create();
    while(scanf("%s %s", s1, s2) != EOF)
    {
        x = search(s1);
        y = search(s2);
        if(x == 0)
            insert(s1, x = ++cnt);
        if(y == 0)
            insert(s2, y = ++cnt);
        deg[x]++;
        deg[y]++;
        fx = find(x);
        fy = find(y);
        if(fx != fy)///统一根
            father[fx] = fy;
    }
    if(judge())
        printf("Possible\n");
    else
        printf("Impossible\n");

    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值