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

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

详细解释:http://blog.csdn.net/lyy289065406/article/details/6647445

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector> using namespace std; struct Trie{
int x;
Trie *next[];
}*root,memory[]; int deg[],father[],cnt,tot; Trie *create(){
Trie *p=&memory[cnt++];
for(int i=;i<;i++)
p->next[i]=NULL;
return p;
} void InsertTrie(char *str,int x){
Trie *loc=root;
for(int i=;str[i]!='\0';i++){
int id=str[i]-'a';
if(loc->next[id]==NULL)
loc->next[id]=create();
loc=loc->next[id];
}
loc->x=x;
} int SearchTrie(char *str){
Trie *loc=root;
for(int i=;str[i]!='\0';i++){
int id=str[i]-'a';
if(loc->next[id]==NULL)
return ;
loc=loc->next[id];
}
return loc->x;
} void init(){
for(int i=;i<=;i++)
father[i]=i;
memset(deg,,sizeof(deg));
cnt=;
tot=;
} int findSet(int x){
if(x!=father[x]){
father[x]=findSet(father[x]);
}
return father[x];
} int judge(){ //判断是否满足欧拉
int odd=;
for(int i=;i<=tot;i++)
if(deg[i]%==)
odd++;
if(odd!= && odd!=)
return ;
int k=findSet();
for(int i=;i<=tot;i++)
if(k!=findSet(i))
return ;
return ;
} int main(){ //freopen("input.txt","r",stdin); char s1[],s2[];
init();
root=create();
while(~scanf("%s%s",s1,s2)){
int x=SearchTrie(s1); //映射求编号速度太慢
int y=SearchTrie(s2); //用字典树来求编号
if(x==)
InsertTrie(s1,x=++tot);
if(y==)
InsertTrie(s2,y=++tot);
deg[x]++;
deg[y]++;
int fx=findSet(x);
int fy=findSet(y);
if(fx!=fy)
father[fx]=fy;
}
if(judge())
printf("Possible\n");
else
printf("Impossible\n");
return ;
}

POJ 2513 Colored Sticks (欧拉回路 + 字典树 +并查集)的更多相关文章

  1. [欧拉] poj 2513 Colored Sticks

    主题链接: http://poj.org/problem? id=2513 Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Tota ...

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

    题目链接:poj 2513 Colored Sticks 题目大意:有N个木棍,每根木棍两端被涂上颜色.如今给定每一个木棍两端的颜色.不同木棍之间拼接须要颜色同样的 端才干够.问最后是否能将N个木棍拼 ...

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

    题目链接 Description You are given a bunch of wooden sticks. Each endpoint of each stick is colored with ...

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

    题意:给定一些木棒,木棒两端都涂上颜色,求是否能将木棒首尾相接,连成一条直线,要求不同木棒相接的一边必须是相同颜色的.   无向图存在欧拉路的充要条件为: ①     图是连通的: ②     所有节 ...

  5. POJ 2513 - Colored Sticks - [欧拉路][图的连通性][字典树]

    题目链接: http://poj.org/problem?id=2513 http://bailian.openjudge.cn/practice/2513?lang=en_US Time Limit ...

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

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

  7. POJ - 2513 Colored Sticks(欧拉通路+并查集+字典树)

    https://vjudge.net/problem/POJ-2513 题解转载自:優YoU  http://user.qzone.qq.com/289065406/blog/1304742541 题 ...

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

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

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

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

随机推荐

  1. 如何用Javascript检测到所有的IE版本

    如何用Javascript检测到所有的IE版本 function GetIEVersion() { var sAgent = window.navigator.userAgent; var Idx = ...

  2. ASP入门(十)-Session对象

    在ASP中,有两个内部对象可以进行一些信息存储,它们是 Application 对象和 Session 对象,其中 Application 对象是对于整个应用程序期间而言的,它对于所有访问网站的用户来 ...

  3. RobotFramework+Selenium2+Appium环境搭建

    转载:https://www.cnblogs.com/testway/p/7372326.html 装python 2.7 RobotFramework是python2 写的,图形界面使用的wxpyt ...

  4. window.btoa

    概述 将ascii字符串或二进制数据转换成一个base64编码过的字符串,该方法不能直接作用于Unicode字符串. 语法 var encodedData = window.btoa(stringTo ...

  5. C#.NET常见问题(FAQ)-如何声明list的多维数组

    可以用下面的方法来声明多维list数组,但是不推荐使用   //对于一维数组:List<数据类型> 变量 = new List<数据类型>(); List<int> ...

  6. JavaScript 之 JavaScript 对象

    重新看JavaScript对象,参考资料: RUNOOB.COM:http://www.runoob.com/jsref/jsref-fromcharcode.html: CodePlayer:htt ...

  7. Netcore使用MailKit进行邮件发送

    public void TestSendMailDemo() { var message = new MimeKit.MimeMessage(); message.From.Add(new MimeK ...

  8. C# FTP常规方法

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...

  9. pyqt、webkit和qt之间的关系

    前言 最近在维护一个PYQT的项目,有很多不明白的地方,总结一下,共其他直接使用pyqt的人参考一下.PyQT是一个生成图形应用程序的工具包.是python语言和成功的Qt库的绑定.Qt库是这个世界上 ...

  10. cocoahttpserver使用具体解释(二)

    接下来,我们接着去学习怎样去接收处理web上传的数据 1 首先我们创建一个 @interface WTZHTTPConnection : HTTPConnection 在这个类中我们用于处理接受文件并 ...