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. [Swift通天遁地]四、网络和线程-(6)检测网络连接状态

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  2. webview加载本地页面

    main.xml中布局webview,activity中设置如下 MyWebView=(WebView)findViewById(R.id.webView1); MyWebView.requestFo ...

  3. 【BZOJ1939】[Croatian2010] Zuma(动态规划)

    题目: BZOJ1939(权限题) 分析: 这题很容易看出是DP,但是状态和转移都不是很好想-- 用\(dp[l][r][c]\)表示在\(l\)前面已经新加了\(c\)个和\(l\)一样的弹子时,使 ...

  4. 题解报告:hdu 3790 最短路径问题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3790 Problem Description 给你n个点,m条无向边,每条边都有长度d和花费p,给你起 ...

  5. html表单代码演示

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...

  6. vs2010 视图 aspx页面设计窗口创建控件时出错 未将对象引用设置到对象的实例

    第一步,首先关闭aspx页面 第二步,在单击项目右击,选择“清理” 第三步,然后在打开aspx页面,就可以看到正常的页面了. 注:一次不行的会,多做几次. 如果还是不行的话,你看看你.cs页面是否继承 ...

  7. WEB文件上传之apache common upload使用(一)

    文件上传一个经常用到的功能,它有许多中实现的方案. 页面表单 + RFC1897规范 + http协议上传 页面控件(flash/html5/activeX/applet) + RFC1897规范 + ...

  8. transactoin

    hibernate对数据的操作是封装在事务当中,并且默认是非自动提交方式.所以用session保存对象时,如果不开启事务,并且手工提交事务,对象并不会真正保存在数据库中.

  9. TCP:三次握手,URG、ACK、PSH、RST、SYN、FIN 含义

    http://blog.csdn.net/wudiyi815/article/details/8505726 TCP:SYN ACK FIN RST PSH URG简析   三次握手Three-way ...

  10. CDR软件-CorelDRAW软件下载,618活动

    618我有诚意,你呢? 不花钱的618,是残缺的618 给自己一个放肆shopping的机遇 活动力度不够大? 继续升级,终极体验 618疯狂倒计时! 同志们,如果你错过了之前的抢先购和升级活动 那么 ...