# poj 2513 Colored Sticks 并查集+欧拉路

Colored Sticks
Time Limit: 5000MS Memory Limit: 128000K
Total Submissions: 38637 Accepted: 10111
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<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define maxl 15
#define maxn 500005
using namespace std;
char x[maxl],y[maxl];
int n,m,tot,sum,fa[maxn],hsh[maxn];
struct chj{
int id;
char s[maxl];
bool operator<(const chj b)const{return strcmp(s,b.s)<0;}
}a[maxn];
inline int get(int x){return fa[x]==x?x:fa[x]=get(fa[x]);}
int main(){
freopen("2513.in","r",stdin);
freopen("2513.out","w",stdout);
while (~scanf("%s%s",x,y)){
n++;
a[++m].id=n;strcpy(a[m].s,x);
a[++m].id=n;strcpy(a[m].s,y);
}
sort(a+1,a+m+1);
for (int i=1;i<=n;i++) fa[i]=i;
int i=1;
while (i<=m){
int j=i+1;
hsh[++tot]=1;
while (j<=m&&strcmp(a[i].s,a[j].s)==0){
int fx=get(a[i].id),fy=get(a[j].id);
fa[fx]=fy;
hsh[tot]++;
j++;
}
i=j;
}
for (int i=1;i<=tot;i++) sum+=hsh[i]&1;
int fath=get(a.id);
for (int i=2;i<=m;i++)
if (get(a[i].id)!=fath){printf("Impossible\n");return 0;}
if (sum!=0&&sum!=2) printf("Impossible\n");else printf("Possible\n");
return 0;
}
10-30 111
05-16 118
04-18 360
03-14 882
05-11 4368
02-19 270
02-23 27
08-10 76
11-18 51
02-11 1819
08-05 129
07-19 103
09-20 109
10-06 20
05-13 555
08-11 458
05-17 205

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

•  非常没帮助
•  没帮助
•  一般
•  有帮助
•  非常有帮助 被折叠的  条评论 为什么被折叠? 到【灌水乐园】发言 ¥2 ¥4 ¥6 ¥10 ¥20  余额支付 (余额：-- )  扫码支付 获取中  扫码支付 点击重新获取   扫码支付 1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。 余额充值