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");
}

最新文章

  1. Makefile 如何轻松搞定
  2. OC 实例变量(instance var)与属性(@property)的关系 isa指针
  3. ruby 字符串学习2
  4. PHP用Array模拟枚举
  5. iOS网络传输Delegate不被触发的本质原因
  6. new Date() iso不支持兼容性问题
  7. LintCode 38. Search a 2D Matrix II
  8. win64位 apache2.4 php5.4 mysql5.6
  9. CodeForces 707A Brain&#39;s Photos
  10. Mysql主从复制(基于Log)
  11. lua向文件中写入数据,进行记录
  12. python构造一个freebuf新闻发送脚本
  13. 细说Android事件传递
  14. JS中如何获取JSON有多少个字段,JSON子项的个数或叫length
  15. 如何打通CMDB,实现就近访问
  16. Linux配置SSH免登录
  17. 压缩和解压缩(I)
  18. VMware里的linux系统里的命令行里会有bee的声音,要如何关掉
  19. Alpha 冲刺 (1/10)
  20. 20145332 拓展:注入shellcode实验

热门文章

  1. Django-CKeditor使用笔记
  2. [Swift通天遁地]五、高级扩展-(4)快速生成Invert、Mix、Tint、Shade颜色及调整饱和度阶
  3. Akka源码分析-Actor创建(续)
  4. redis-数据结构以及使用场景分析
  5. url参数为数组
  6. JavaScript入门二
  7. Java 开源博客 Solo 1.3.0 发布 - Docker 支持
  8. Raspberry Pi开发之旅-空气温湿度检测(DHT11)
  9. 图解TCP/IP笔记(2)——数据链路
  10. Linux 信息查询