Time Limit: 5000MS   Memory Limit: 128000K
Total Submissions: 37340   Accepted: 9796

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<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<queue>
#include<deque>
#include<iomanip>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<fstream>
#include<memory>
#include<list>
#include<string>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define MAXN 500008
#define INF 1000000009
#define eps 0.00000001
/*
欧拉路 一笔画问题!
Trie树就是一种节省空间的hash
利用并查集判断是否联通
*/
char a[], b[];
int cnt = , index = , pre[MAXN], degree[MAXN];
typedef struct node
{
bool flag;
int id;
struct node* next[];
}*Tree;
node memory[MAXN];
Tree Newnode()
{
Tree T = &memory[cnt++];
T->flag = false;
T->id = -;
for (int i = ; i < ; i++)
T->next[i] = NULL;
return T;
}
int Insert(char* s, Tree T)
{
int p = ;
while (s[p] != '\0')
{
int k = s[p] - 'a';
if (!T->next[k])
T->next[k] = Newnode();
T = T->next[k];
p++;
}
if (T->flag)
return T->id;
else
{
T->flag = true;
T->id = index++;
return T->id;
}
}
int find(int x)
{
if (pre[x] == -)
return x;
else
return pre[x] = find(pre[x]);
}
void mix(int a, int b)
{
int fa = find(a), fb = find(b);
if (fa != fb)
{
pre[fa] = fb;
}
}
int main()
{
memset(pre, -, sizeof(pre));
memset(degree, , sizeof(degree));
Tree T = Newnode();
while (scanf("%s %s", a, b) != EOF)
{
//if (a[0] == '#') break;
int ia = Insert(a, T), ib = Insert(b, T);
//cout << ia << ' ' << ib << endl;
degree[ia]++;
degree[ib]++;
mix(ia, ib);
}
int s = find(), tmp = ;
for (int i = ; i < index; i++)
{
if (find(i) != s) //存在多个祖先,图为森林,不连通
{
printf("Impossible\n");
return ;
}
if (degree[i] % )
tmp++;
}
if(tmp== || tmp==)
printf("Possible\n");
else
printf("Impossible\n");
}

Colored Sticks (并查集+Trie + 欧拉路)的更多相关文章

  1. PKU 2513 Colored Sticks(并查集+Trie树+欧拉路径(回路))

    题目大意: 给定一些木棒,木棒两端都涂上颜色,求是否能将木棒首尾相接,连成一条直线,要求不同木棒相连接的一端必须是同颜色的. 解题思路: 可以用图论中欧拉路的知识来解这道题,首先可以把木棒两端看成节点 ...

  2. Colored Sticks POJ - 2513(trie树欧拉路)

    题意: 就是无向图欧拉路 解析: 不能用map..超时 在判断是否只有一个联通的时候,我比较喜欢用set,但也不能用set,会超时,反正不能用stl emm 用trie树来编号就好了 #include ...

  3. FZU 2112 并查集、欧拉通路

    原题:http://acm.fzu.edu.cn/problem.php?pid=2112 首先是,票上没有提到的点是不需要去的. 然后我们先考虑这个图有几个连通分量,我们可以用一个并查集来维护,假设 ...

  4. POJ 2513 Colored Sticks 字典树、并查集、欧拉通路

    Description You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some ...

  5. loj6198谢特 后缀数组+并查集+Trie

    先把问题放在后缀数组上考虑 已知两个数组a b,求min(a[i],...,a[j])+(b[i]^b[j])的最大值 套路题 初始每个点都是一个小连通块 把a按从大到小的顺序加入,计算当前加入边作为 ...

  6. poj 2513 Colored Sticks (trie树+并查集+欧拉路)

    Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 40043   Accepted: 10406 ...

  7. Colored Sticks (字典树哈希+并查集+欧拉路)

    Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 27704   Accepted: 7336 Description You ...

  8. POJ2513——Colored Sticks(Trie树+欧拉回路+并查集)

    Colored Sticks DescriptionYou are given a bunch of wooden sticks. Each endpoint of each stick is col ...

  9. poj 2513 Colored Sticks trie树+欧拉图+并查集

    点击打开链接 Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 27955   Accepted ...

随机推荐

  1. Flink编程练习

    目录 1.wordcount 2.双流警报EventTime 3.持续计数stateful + timer + SideOutputs 4.一定时间范围内的极值windowfunction + che ...

  2. 8.3 TCPIP协议族

    接下来我们要学习的内容是TCP/IP协议族.TCP/IP协议族在网络系统中是非常重要的.这一个协议族当中牵涉到许许多多的我们平常所用到的协议.TCP/IP呢它也有分层模型.然后我们讲到的就是三方面的内 ...

  3. P2258 子矩阵(dp)

    P2258 子矩阵 题目描述 给出如下定义: 子矩阵:从一个矩阵当中选取某些行和某些列交叉位置所组成的新矩阵(保持行与列的相对顺序)被称为原矩阵的一个子矩阵. 例如,下面左图中选取第2.4行和第2.4 ...

  4. SpringBoot入门之HelloWorld

    1.SpringBoot简介 百度百科:Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而 ...

  5. CyclibcBarrier与CountDownLatch区别

    1.CyclibcBarrier的线程运行到某个位置后即停止运行,直到所有的线程都到达这个点,所有线程才开始运行:CountDownLatch是线程运行到某个点后,计数器-1,程序继续运行即Cycli ...

  6. 用最简单的脚本完成supertab的基本功能并实现一个更加合理的功能

    supertab是vim的一个出名的插件, 相信会vim的人没几个不知道的, 我在之前的<<vim之补全1>>中首先说明的也是它, supertab实现的功能简单的说就是用ta ...

  7. CSS平滑过渡动画:transition

    <html> <head> <link href="http://cdn.bootcss.com/twitter-bootstrap/3.0.2/css/boo ...

  8. 2016.01.05 DOM笔记(一) 查找元素

    DOM节点的种类 元素和标签是一个意思,例如<body>标签或者称为<body>元素 节点DOM的节点分为三类  元素节点,文本节点,属性节点 例如 <div id=‘b ...

  9. Ubuntu 18.04 如何固定图标到任务栏

    参考 https://blog.csdn.net/u014160286/article/details/81631863

  10. AcRxClass::addX

    AcRxClass::addX函数 virtual AcRxObject * addX( AcRxClass* pProtocolClass, AcRxObject* pProtocolObject) ...