POJ 2513 Colored Sticks (欧拉回路 + 字典树 +并查集)
Colored Sticks
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 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 (欧拉回路 + 字典树 +并查集)的更多相关文章
- [欧拉] poj 2513 Colored Sticks
主题链接: http://poj.org/problem? id=2513 Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Tota ...
- poj 2513 Colored Sticks(欧拉路径+并检查集合+特里)
题目链接:poj 2513 Colored Sticks 题目大意:有N个木棍,每根木棍两端被涂上颜色.如今给定每一个木棍两端的颜色.不同木棍之间拼接须要颜色同样的 端才干够.问最后是否能将N个木棍拼 ...
- POJ 2513 Colored Sticks (欧拉回路+并查集+字典树)
题目链接 Description You are given a bunch of wooden sticks. Each endpoint of each stick is colored with ...
- POJ 2513 Colored Sticks(欧拉回路,字典树,并查集)
题意:给定一些木棒,木棒两端都涂上颜色,求是否能将木棒首尾相接,连成一条直线,要求不同木棒相接的一边必须是相同颜色的. 无向图存在欧拉路的充要条件为: ① 图是连通的: ② 所有节 ...
- POJ 2513 - Colored Sticks - [欧拉路][图的连通性][字典树]
题目链接: http://poj.org/problem?id=2513 http://bailian.openjudge.cn/practice/2513?lang=en_US Time Limit ...
- POJ 2513 Colored Sticks(欧拉道路+字典树+并查集)
http://poj.org/problem?id=2513 题意: 给定一些木棒,木棒两端都涂上颜色,求是否能将木棒首尾相接,连成一条直线,要求不同木棒相接的一边必须是相同颜色的. 思路: 题目很明 ...
- POJ - 2513 Colored Sticks(欧拉通路+并查集+字典树)
https://vjudge.net/problem/POJ-2513 题解转载自:優YoU http://user.qzone.qq.com/289065406/blog/1304742541 题 ...
- poj2513 Colored Sticks —— 字典树 + 并查集 + 欧拉回路
题目链接:http://poj.org/problem?id=2513 题解:通过这题了解了字典树.用字典树存储颜色,并给颜色编上序号.这题为典型的欧拉回路问题:将每种颜色当成一个点.首先通过并查集判 ...
- poj 2513 Colored Sticks (trie树+并查集+欧拉路)
Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 40043 Accepted: 10406 ...
随机推荐
- 转:查看linux系统版本号
转自: http://blog.csdn.net/zhuying_linux/article/details/6859286 lsb_release -a
- (转)No row with the given identifier exists问题的解决
产生此问题的原因: 有两张表,table1和table2.产生此问题的原因就是table1里做了关联<one-to-one>或者<many-to-one unique ...
- [Canvas]人物型英雄出现(前作仅为箭头)
源码点此下载,用浏览器打开index.html观看. 代码: <!DOCTYPE html> <html lang="utf-8"> <meta ht ...
- Oracle导入excel数据快速方法
Oracle导入excel数据快速方法 使用PLSQL Developer工具,这个可是大名鼎鼎的Oracle DBA最常使用的工具. 在单个文件不大的情况下(少于100000行),并且目的 ...
- 实验室报告:VMware vSphere Data Protection
dd Lab Reports VMware vSphere Data Protection Fast, Simple, and Agentless Deduplicated Virtual Machi ...
- Ubuntu/Centos 系统上安装与配置Apache
一.在线安装: Ubuntu:sudo apt-get install apache2 Centos: sudo yum install apache2 二.安装后的位置: 1.服务地址:/etc/i ...
- 再谈IE的浏览器模式和文档模式[转]
http://www.cnblogs.com/liuzhendong/archive/2012/04/27/2474363.html 以前在 “IE8兼容视图(IE7 mode)与独立IE7的区别”一 ...
- python获取命令行参数 启动文件
import sys # python D:\MARK\temp.py m1 start 9090 print(sys.argv) # ['D:\\MARK\\temp.py', 'm1', 'sta ...
- Python标准库笔记(3) — datetime模块
datetime模块提供了简单和复杂的方式用于操纵日期和时间的类.虽然支持日期和时间运算,但实现的重点是为了输出格式化和操作高效地提取属性. 1. 模块内容 内容 描述 常量 datetime.M ...
- android 安装调试第三方应用失败
Android Studio 3.0会在debug apk的manifest文件application标签里自动添加 android:testOnly="true"属性,导致IDE ...