poj 2513 Colored Sticks 并查集+欧拉路

原创 2018年04月17日 14:59:46

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
【题解】
感谢xu0_zyhttps://blog.csdn.net/xu0_zy/article/details/79975154
~(≧▽≦)/~啦啦啦

#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[1].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;
}

poj 2513 Colored Sticks 欧拉路

水欧拉路,直接颜色离散化然后建边,或者并查集。 #include #include #include #include using namespace std; const int maxn=250...
  • u013665921
  • u013665921
  • 2015-03-25 21:26:10
  • 309

POJ 2513 Colored Sticks 欧拉路+字典树

点击打开链接 题意;给定一些木棒,木棒两端都涂上颜色,求是否能将木棒首尾相接,连成一条直线,要求不同木棒相接的一边必须是相同颜色的。 思路 注意 1 此题需要构建无向图; 2 通过字典树得到每种颜...
  • became_a_wolf
  • became_a_wolf
  • 2016-02-22 01:17:01
  • 221

[poj 2513] Colored Sticks (trie+欧拉路)

题目大意:给定一些木棒,木棒两端都涂上颜色,求是否能将木棒首尾相接,连成一条直线,要求不同木棒相接的一边必须是相同颜色的。 我们把颜色看做点,木棒看做边,这道题就转为了求是否存在欧拉路。 而判断欧...
  • qq_33208960
  • qq_33208960
  • 2017-02-16 13:51:26
  • 215

poj 2513 -- Colored Sticks ( Trie + 并查集 + 欧拉 )

WA了很久的一道题了,今天终于过了 判断条件有两个 1 、出现次数为奇数的颜色 2 、判断图是否连通 用Trie处理效率较高,判断连通可以将颜色映射成数字用并查集 # include...
  • dlut_ju
  • dlut_ju
  • 2013-04-02 16:59:58
  • 467

POJ 2513 Colored Sticks (排序+并查集判断欧拉路)

题意: 给一堆棍子,棍子由头尾两个字符串组成,棍子两两相连的条件是相接的部分字符串必须相同,问能否将这些棍子组成一条直线. 解题过程: 这题其实并不需要用字典树处理,用排序统计度数一样. 一开始我...
  • johsnows
  • johsnows
  • 2016-08-22 10:13:09
  • 323

POJ 2513 Colored Sticks (Tire+欧拉路+并查集)

Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 32788   Accepted:...
  • Grit_ICPC
  • Grit_ICPC
  • 2015-08-11 21:35:11
  • 373

POJ 2513 Colored Sticks 好题 字典树+并查集+欧拉路

原文 優YoU  http://user.qzone.qq.com/289065406/blog/1304742541 大致题意: 给定一些木棒,木棒两端都涂上颜色,求是否能将木棒首尾相接,连成一...
  • qq_36312912
  • qq_36312912
  • 2017-08-05 20:24:19
  • 66

POJ 2513 Colored Sticks【tire tree+并查集+欧拉路】

Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissi...
  • mengxiang000000
  • mengxiang000000
  • 2016-05-13 13:31:36
  • 437

poj 2513 Colored Sticks (欧拉路+并查集+字典树)

题目链接:http://poj.org/problem?id=2513 思路:
  • u013649253
  • u013649253
  • 2014-10-04 19:26:02
  • 285
收藏助手
不良信息举报
您举报文章:poj 2513 Colored Sticks 并查集+欧拉路
举报原因:
原因补充:

(最多只允许输入30个字)