[欧拉] poj 2513 Colored Sticks

主题链接:

http://poj.org/problem?

id=2513


Colored Sticks

Time Limit: 5000MS   Memory Limit: 128000K
Total Submissions: 30955   Accepted: 8159

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

[

problem_id=2513" style="text-decoration:none">Submit]   [Go Back]   [

problem_id=2513" style="text-decoration:none">Status]  
[Discuss]

题目意思:

有n条木棒。每条木棒两端有两种颜色,求这些木棒是否能能在一起,使得前一个木棒的后端颜色和前一个木棒的前端颜色一样。

解题思路:

欧拉通路+字典树+并查集

注意是无向边。注意要推断连通性。直接用map映射会超时,自己写字典树。

代码:

//#include<CSpreadSheet.h>

#include<iostream>
#include<cmath>
#include<cstdio>
#include<sstream>
#include<cstdlib>
#include<string>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<ctime>
#include<bitset>
#include<cmath>
#define eps 1e-6
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define ll __int64
#define LL long long
#define lson l,m,(rt<<1)
#define rson m+1,r,(rt<<1)|1
#define M 1000000007
//#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;

#define Maxn 550000

int dei[Maxn],a[Maxn],cnt,la;

struct Node
{
    struct Node * next[30];
    int nu;
}node[Maxn*15],root;

int Insert(char *a)
{
    Node * p=&root;

    while(*a)
    {
        if(p->next[*a-'a'+1]==NULL)
        {
            node[++la].nu=0;
            memset(node[la].next,NULL,sizeof(node[la].next));
            p->next[*a-'a'+1]=&node[la];
        }
        p=p->next[*a-'a'+1];
        a++;
    }
    if(!p->nu)
        p->nu=++cnt;
    return p->nu;

}

int Find(int x)
{
    int temp=x;
    while(x!=fa[x])
        x=fa[x];
    while(fa[temp]!=x)
    {
        int cur=fa[temp];
        fa[temp]=x;
        temp=cur;
    }
    return x;
}
void Unio(int a,int b)
{
    a=Find(a),b=Find(b);
    if(a!=b)
        fa[a]=b;
}
int main()
{

   memset(dei,0,sizeof(dei));
   string a,b;
   char sa[15],sb[15];
   cnt=la=0;
   for(int i=1;i<=Maxn-5000;i++)
        fa[i]=i;

   memset(root.next,NULL,sizeof(root.next));
   root.nu=0;

   while(~scanf("%s%s",sa+1,sb+1))
   {
       int a=Insert(sa+1);
       int b=Insert(sb+1);

       dei[a]++,dei[b]++;
       Unio(a,b);
   }
   bool ans=true;
   int nui=0,nuo=0,la=-1;

   for(int i=1;i<=cnt;i++)
   {
       if(la==-1)
            la=Find(i);
       else if(la!=Find(i))
       {
           ans=false;
           break;
       }
       if(dei[i]&1)
            nui++;
   }
   if(!ans||nui>2||nui==1)
        printf("Impossible\n");
   else
        printf("Possible\n");
   //system("pause");

    return 0;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

时间: 2024-08-02 15:25:26

[欧拉] poj 2513 Colored Sticks的相关文章

[ACM] POJ 2513 Colored Sticks (Trie树,欧拉通路,并查集)

Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 29736   Accepted: 7843 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 st

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

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

poj 2513 Colored Sticks(欧拉路径+并检查集合+特里)

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

poj 2513 Colored Sticks 彩色棒

poj  2513  Colored Sticks http://poj.org/problem?id=2513 题意:现在有几个木棒,每个木棒端点都着色,问:能否将它们排成一排,同时要满足相邻的的两端点颜色是一样的. trie+并查集+欧拉通路 方法:要想排成一排,可以变向的理解为从一个图里找到一个欧拉通路(一定要想到):如果只是孤零零的一个个小棒,这道题很难实现. 首先:要先要对所有颜色进行编号,由于事先又不知道有几种颜色,有哪几种颜色.故有一个笨方法,用map<int,string>容器

POJ 2513 Colored Sticks(欧拉回路,字典树,并查集)

题意:给定一些木棒,木棒两端都涂上颜色,求是否能将木棒首尾相接,连成一条直线,要求不同木棒相接的一边必须是相同颜色的. 转:kuangbing 无向图存在欧拉路的充要条件为: ①     图是连通的: ②     所有节点的度为偶数,或者有且只有两个度为奇数的节点. 图的连通可以利用并查集去判断. 度数的统计比较容易. view code//第一次用指针写trie,本来是用二维数组,发现数组开不下,只好删删改改,改成指针 //做这道题,知道了欧拉回路判定,还有用指针写trie #include

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 2513 Colored Sticks(字典树+并查集连通性+欧拉回路)

题目地址:POJ 2513 刚开始没想到字典树,用的map函数一直TLE,由于上一次的签到题由于没想到字典树而卡了好长时间的深刻教训,于是过了不久就想起来用字典树了,(为什么是在TLE了5次之后..T^T)然后把map改成了字典树,然后就过了. 这题居然不知不觉的用上了欧拉回路..其实当时我是这样想的..因为相互接触的必须要相同,所以除了两端外,其他的都是两两相同的,所以除了两端的颜色外其他的的个数必须为偶数.然后两端的可能相同可能不相同,相同的话,说明所有的都是偶数个数了,不相同的话那就只有这

[欧拉回路] poj 2513 Colored Sticks

题目链接: http://poj.org/problem?id=2513 Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 30955   Accepted: 8159 Description You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some color. Is it

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

http://poj.org/problem?id=2513 题意: 给定一些木棒,木棒两端都涂上颜色,求是否能将木棒首尾相接,连成一条直线,要求不同木棒相接的一边必须是相同颜色的. 思路: 题目很明显的是欧拉道路的问题. 欧拉道路的关键是: ①图是连通的. ②最多只能有两个奇点.(不能只存在一个奇点) 本来是想用map映射的,但是太多了,比较费时,这里用字典树的话会比较省时,判断图是否连通可以用并查集来完成. 1 #include<iostream> 2 #include<algori