poj 2513 Colored Sticks(并查集+hash+欧拉图)

3 篇文章 0 订阅
1 篇文章 0 订阅

Colored Sticks

Time Limit: 5000MS Memory Limit: 128000K
Total Submissions: 39375 Accepted: 10265

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

The UofA Local 2000.10.14

[Submit]   [Go Back]   [Status]   [Discuss]

标准做法是用并查集+欧拉图+字典树,但是我不想敲字典树

先用了map,毫无意外tle了,然后就想用hash试一下,然后直接过了。。。(不知道那些用map过的到底怎么写的。。。)

个人觉得unordered_map应该也能过,可惜poj貌似不能用c++11

其实这题最坑的地方是有空输入,这个时候输出possible

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <map>

using namespace std;
const int maxn = 250010*2;

int  sett[maxn];
int used[maxn];


unsigned int hasha(char *url)
{
	unsigned int nn=0;
	char *b=(char *)&nn;
	for(int i=0;url[i];++i)
		b[i%4]^=url[i];
	return nn%maxn;
}

int findSet(int x)
{
    if(x==sett[x])
        return x;
    int y = findSet(sett[x]);
    return sett[x] = y;
}

void unionSet(int x,int y)
{
    int x1 = findSet(x);
    int y1 = findSet(y);
    if(x1!=y1)
    {
        sett[x1] = y1;
    }
}
char str1[15],str2[15];
int ma[maxn];
int main()
{
    int t =0,flag =1;
    for(int i = 0;i<=maxn;i++)
        {
            sett[i] = i;
            used[i] = 0;
        }
    //map<string,int> ma;
    while(scanf("%s%s",str1,str2)!=EOF)
    {
        unsigned int s1 = hasha(str1);
        unsigned int s2 = hasha(str2);
        if(ma[s1]==0)
        ma[s1] = t++;
        if(ma[s2]==0)
        ma[s2] = t++;
        used[ma[s1]]++;
        used[ma[s2]]++;
        unionSet(ma[s1],ma[s2]);
    }
    int k = 0;
    int m = 0;
    for(int i =0;i<t-1;i++)
    {
        if(sett[i] == i)
            {
                m++;
                //cout<<i<<endl;
            }
        if(used[i]%2==1)
            k++;
    }
    //cout<<m<<endl;
    if((k!=0&&k!=2)||m!=1)
        flag = 0;
    if(t==0)
        flag = 1;
    //cout<<flag<<endl;
    if(flag)
      cout<<"Possible"<<endl;
    else
      cout<<"Impossible"<<endl;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值