Colored Sticks
Time Limit: 5000MS   Memory Limit: 128000K
Total Submissions: 35612   Accepted: 9324

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
【分析】欧拉通路的判定。一开始用map给字符串编号,超时了,后来在网上烤来一份字典树hash.
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#include<functional>
#define mod 1000000007
#define inf 0x3f3f3f3f
#define pi acos(-1.0)
using namespace std;
typedef long long ll;
const int N=;
const int M=;
ll power(ll a,int b,ll c){ll ans=;while(b){if(b%==){ans=(ans*a)%c;b--;}b/=;a=a*a%c;}return ans;}
int parent[N];
const int MAX=;
int degree[N];//度数
int color;//颜色编号,从0开始,最后就是颜色总数
int Find(int x) {
if(parent[x] != x) parent[x] = Find(parent[x]);
return parent[x];
}//查找并返回节点x所属集合的根节点
void Union(int x,int y) {
x = Find(x);
y = Find(y);
if(x == y) return;
parent[y] = x;
}//将两个不同集合的元素进行合并
struct Trie
{
bool isWord;
struct Trie *next[MAX];
int id;
};
int insert(Trie *root,char *word)//返回颜色编号
{
Trie *p=root;
int i=;
while(word[i]!='\0')
{
if(p->next[word[i]-'a']==NULL)
{
Trie *temp=new Trie;
temp->isWord=false;
for(int j=;j<MAX;j++)
temp->next[j]=NULL;
temp->isWord=false;
temp->id=;
p->next[word[i]-'a']=temp;
}
p=p->next[word[i]-'a'];
i++;
}
if(p->isWord)
{
return p->id;
}
else
{
p->isWord=true;
p->id=color++;
return p->id;
}
}
void del(Trie *root)
{
for(int i=;i<MAX;i++)
{
if(root->next[i]!=NULL)
del(root->next[i]);
}
free(root);
}
int main()
{
char str1[],str2[];
Trie *root=new Trie;
for(int i=;i<MAX;i++)
root->next[i]=NULL;
root->isWord=false;
root->id=;//初始化
color=;
for(int i=;i<N;i++)parent[i]=i;
memset(degree,,sizeof(degree));
while(scanf("%s%s",&str1,&str2)!=EOF)
{
int t1=insert(root,str1);
int t2=insert(root,str2);
// printf("%d %d\n",t1,t2);
degree[t1]++;
degree[t2]++;
Union(t1,t2);
}
int cnt1=,cnt2=;
for(int i=;i<color;i++)
{
if(parent[i]==i)cnt1++;
if(degree[i]%==)cnt2++;
if(cnt1>)break;
if(cnt2>)break;
}
//数据比较坑人,存在0根木棒的情况,此时cnt1==0
if((cnt1==||cnt1==)&&(cnt2==||cnt2==))
printf("Possible\n");
else printf("Impossible\n");
//del(root);//单组输入可以不释放空间,可以节省时间
return ; }

POJ2513Colored Sticks(欧拉通路)(字典树)(并查集)的更多相关文章

  1. POJ 2513 无向欧拉通路+字典树+并查集

    题目大意: 有一堆头尾均有颜色的木条,要让它们拼接在一起,拼接处颜色要保证相同,问是否能够实现 这道题我一开始利用map<string,int>来对颜色进行赋值,好进行后面的并查操作以及欧 ...

  2. Colored Sticks POJ - 2513 并查集+欧拉通路+字典树hash

    题意:给出很多很多很多很多个棒子 左右各有颜色(给出的是单词) 相同颜色的可以接在一起,问是否存在一种 方法可以使得所以棒子连在一起 思路:就是一个判欧拉通路的题目,欧拉通路存在:没奇度顶点   或者 ...

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

    http://poj.org/problem?id=2513 题意: 给定一些木棒,木棒两端都涂上颜色,求是否能将木棒首尾相接,连成一条直线,要求不同木棒相接的一边必须是相同颜色的. 思路: 题目很明 ...

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

    Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 27097   Accepted: 7175 ...

  5. poj2513 Colored Sticks —— 字典树 + 并查集 + 欧拉回路

    题目链接:http://poj.org/problem?id=2513 题解:通过这题了解了字典树.用字典树存储颜色,并给颜色编上序号.这题为典型的欧拉回路问题:将每种颜色当成一个点.首先通过并查集判 ...

  6. POJ 2513 字典树+并查集+欧拉路径

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

  7. poj2513- Colored Sticks 字典树+欧拉通路判断

    题目链接:http://poj.org/problem?id=2513 思路很容易想到就是判断欧拉通路 预处理时用字典树将每个单词和数字对应即可 刚开始在并查集处理的时候出错了 代码: #includ ...

  8. poj 2513 连接火柴 字典树+欧拉通路 好题

    Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 27134   Accepted: 7186 ...

  9. ACM/ICPC 之 DFS求解欧拉通路路径(POJ2337)

    判断是欧拉通路后,DFS简单剪枝求解字典序最小的欧拉通路路径 //Time:16Ms Memory:228K #include<iostream> #include<cstring& ...

随机推荐

  1. [NOIP2016]愤怒的小鸟 DP

    ---题面--- 题解: 首先观察数据范围,n <= 18,很明显是状压DP.所以设f[i]表示状态为i时的最小代价.然后考虑转移. 注意到出发点(0, 0)已经被固定,因此只需要2点就可以确定 ...

  2. hive subprocess failed with code X 的错误码对应信息

    PipeMapRed.waitOutputThreads(): subprocess failed with code X ,这里code X对应的信息如下:error code 1: Operati ...

  3. php 计算两个日期的间隔天数

    使用php内部自带函数实现 1.使用DateTime::diff 实现计算 参考阅读>>PHP DateTime::diff() 上代码: <?php $start = " ...

  4. (转)如何用python抓取网页并提取数据

    最近一直在学这部分,今日发现一篇好文,虽然不详细,但是轮廓是出来了: 来自crifan:http://www.crifan.com/crawl_website_html_and_extract_inf ...

  5. MyBatis对象关联关系---- association与collection

    Mybatis处理“一对多”的关系时,需要用到associasion元素.处理”多对一“用collection元素来实现(这两个元素在之前mapper文件中提到过). 本例子中,假设一名User可以有 ...

  6. HDU 1395

    2^x mod n = 1 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  7. 【hdu2222-Keywords Search】AC自动机基础裸题

    http://acm.hust.edu.cn/vjudge/problem/16403 题意:给定n个单词,一个字符串,问字符串中出现了多少个单词.(若单词her,he,字符串aher中出现了两个单词 ...

  8. 【CF1023E】Down or Right(交互,贪心)

    题意: n<=500 思路:From https://blog.csdn.net/csdnjiangshan/article/details/81813227 #include<cstdi ...

  9. [bzoj4518][Sdoi2016]征途-斜率优化

    Brief Description Pine开始了从S地到T地的征途. 从S地到T地的路可以划分成n段,相邻两段路的分界点设有休息站. Pine计划用m天到达T地.除第m天外,每一天晚上Pine都必须 ...

  10. React第三次入门

    传统HTML开发在处理越来越多的服务器数据和用户交互数据反应到复杂界面的时候,代码量越来越大,难以维护. Angular是基于MVVM的开发框架,重量级..不适用于移动端的web栈, 其UI组件的封装 ...