测试赛A - Colored Sticks(并查集+字典树+欧拉回路)

A - Colored Sticks

Time Limit:5000MS     Memory Limit:128000KB     64bit IO Format:%I64d & %I64u

Submit Status

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.

使用字典树来给每个颜色定一个序号,并统计出现的次数,使用并查集判断是否全部的点联通,在用欧拉回路,看整个图是不是可以一次走完,

判断方法,如果奇数度数的点为0或2个,那么一定可以走通

#include <cstdio>
#include <cstring>
int p[520000] , top , q[520000];
struct node{
    int flag ;
    node *next[27] ;
} *head;
node *newnode()
{
    node *p = new node ;
    p->flag = 0;
    for(int i = 0 ; i < 27 ; i++)
        p->next[i] = NULL ;
    return p;
}
int gettree(node *head,char *s)
{
    int i , l = strlen(s) ;
    node *p = head ;
    for(i = 0 ; i < l ; i++)
    {
        int k = s[i] - 'a' ;
        if(p->next[k]==NULL)
            p->next[k] = newnode();
        p = p->next[k] ;
    }
    if( p->flag == 0 )
        p->flag = top++ ;
    return p->flag ;
}
int f(int x)
{
    int r , k , l ;
    r = x ;
    while( r != p[r] )
        r = p[r] ;
    k = x ;
    while( k != r )
    {
        l = p[k] ;
        p[k] = r ;
        k = l ;
    }
    return r ;
}
void add(int u,int v)
{
    u = f(u) ;
    v = f(v) ;
    if( u != v )
        p[u] = v ;

}
char s1[11] , s2[11] ;
int main()
{
    int i , n , k1 , k2 ;
    memset(q,0,sizeof(q));
    head = newnode() ;
    for(i = 0 ; i <= 520000 ; i++)
        p[i] = i ;
        top = 1 ;
    while( scanf("%s %s", s1, s2 ) !=EOF )
    {
        k1 = gettree(head,s1);
        k2 = gettree(head,s2);
        q[k1]++ ;
        q[k2]++ ;
        add(k1,k2) ;
    }
    int k = f(1) ;
    for(i = 2 ; i < top ; i++)
        if( k != f(i) )
            break;
    if( i < top )
        printf("Impossible\n");
    else
    {
        int ji = 0 ;
        for(i = 1 ; i < top ; i++)
            if( q[i]%2 )
                ji++ ;
        if(ji ==0 || ji == 2)
            printf("Possible\n");
        else
            printf("Impossible\n");
    }
    return 0;
}

测试赛A - Colored Sticks(并查集+字典树+欧拉回路)

时间: 2024-08-06 06:26:53

测试赛A - Colored Sticks(并查集+字典树+欧拉回路)的相关文章

poj 2513 Colored Sticks 并查集 字典树 欧拉回路判断

点击打开链接题目链接 Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 30273   Accepted: 8002 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 sti

POJ - Colored Sticks - 并查集+字典树

这道题主要还是要判断是不是欧拉图 说白了就是能不能这幅图能不能用一笔画下来,那么就可以知道了,如果是一个环状的,说明奇数度就不存在,否则就只能用两个奇数度(起点终点)//我的理解这是 只需要用字典树将单词变为对应的一个数字,然后并查集操作就可以,需要维护一个度变量 #include<stdio.h> #include<string.h> int du[500010],p[500010]; int tot=1; struct tree { tree *next[30]; int id

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

题目链接:poj 2513 Colored Sticks 题目大意:有N个木棍,每根木棍两端被涂上颜色,现在给定每个木棍两端的颜色,不同木棍之间拼接需要颜色相同的 端才可以,问最后能否将N个木棍拼接在一起. 解题思路:欧拉通路+并查集+字典树.欧拉通路,每个节点的统计度,度为奇数的点不能超过2个.并查集,判断节点 是否完全联通.字典树,映射颜色. #include <cstdio> #include <cstring> #include <string> #includ

测试赛F - Dragon Balls(并查集)

F - Dragon Balls Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Description Five hundred years later, the number of dragon balls will increase unexpectedly, so it's too difficult for Monkey King(WuKong) to

hdu 3172 Virtual Friends (并查集 + 字典树)

题目: 链接:点击打开链接 题意: 输入n,给出n行数据,每行有两个字符串,输出关系网络中朋友的个数,n行. 思路: 代码: #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> using namespace std; const int N = 22; const int M = 200020; struct node { int c; node *chil

POJ2513 Colored Sticks (并查集+trie)

传送门 Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K       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

POJ2513(欧拉回路+并查集+字典树)

Colored Sticks 题目链接:https://vjudge.net/problem/POJ-2513 题目大意: candidate19有好多根棍子,这些棍子的两端分都别涂了一种颜色. candidate19突然有了一个疑问,就是他手里的这些棍子能否互相拼接,从而形成一条直线呢? 两根棍子只有在颜色相同的时候才能拼接.比如有两根棍子,第一根棍子的两端的颜色分别为blue green,第二根两端的颜色为blue red,那么他们就可以拼接成green blue blue red或者red

优先队列 + 并查集 + 字典树 + 树状数组 + 线段树 + 线段树点更新 + KMP +AC自动机 + 扫描线

这里给出基本思想和实现代码 . 优先队列 : 曾经做过的一道例题       坦克大战 1 struct node 2 { 3 int x,y,step; 4 friend bool operator <(node s1,node s2) // 定义结构体 的时候 这个 就是 用于 优先队列的 基准排序 5 { 6 return s1.step>s2.step; // 步数小的 在上 为小顶堆 7 } 8 }; 9 priority_queue<node>Q; // 优先队列的结构

poj2513Colored Sticks(欧拉通路+字典树+并查集)

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