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 ...
随机推荐
- IIS中的application总是报404错误
在IIS的一个站点下面建立了一个application,访问其中页面的时候总是报404(找不到页面)的错误,哪怕是最简单只包含一个简单html页面的application也是如此,而其他同级的ap ...
- 如何启动docker service
From powershell prompt following works for me with no issues restart-service *docker* [注意] 我试了一下,这个命 ...
- oauth2-server-php-docs 概念
PHP的OAuth2服务器库 将OAuth2.0干净地安装到您的PHP应用程序中. 从GitHub 下载代码开始. 要求 这个库需要PHP 5.3.9+.然而,有一个稳定的版本和开发分支的PHP 5. ...
- [Spring Boot] Singleton and Prototype
When we use Bean to do autowired, it actually use singleton, so even we create multi instanses, they ...
- [Algorithm] Powerset Problem
By given an array [1,2,3]: Return all possible combinations. for example: [[], [1], [2], [3], [1, 2] ...
- opencv直方图拉伸
1.首先计算出一幅图像的直方图 //计算直方图 cv::MatND ImageHist::getHist(const cv::Mat &image){ cv::Mat im; if(image ...
- ASP入门(十六)-ASP开发的规范
毋容置疑,在开发中遵守一套规范,将会有利于提高代码的可读性,较低后期维护成本. 文件存放目录规范 js 目录下存放着页面所使用的 JavaScript 脚本文件,因为我们可能用到第三方提供的免费的 J ...
- 使用Nexus管理maven仓库,setting文件理解
来到新公司对很多陌生的技术一头雾水,以前在工作中没有真正使用过maven,于是强迫自己蛋定下来一个一个的突破,下面是我对maven的setting配置文件的理解,由于是现学的,难免可能会理解偏差,还请 ...
- SQL Server临时表
[]SQL Server临时表]()https://docs.microsoft.com/zh-cn/sql/relational-databases/tables/temporal-tables)
- string format 格式化小数位
String具体的格式化数据的方法 int a = 12345678;格式为sring输出Label1.Text = string.Format("asdfadsf{0}adsfasdf&q ...