题目链接:

http://poj.org/problem?id=2513

http://bailian.openjudge.cn/practice/2513?lang=en_US

Time Limit: 5000MS Memory Limit: 128000K

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.

题意:

给出若干木棒,每个木棒两个端点有两个颜色,两个木棒的端点颜色相同,则可以接在一起。问是否把所有木棒接成一根。

题解:

(参考http://blog.csdn.net/lyy289065406/article/details/6647445

将每个颜色都看成一个节点,木棒就能看成一条连接两个端点的无向边。问题就变成在给定一个无向图问是否存在欧拉路

欧拉路存在的充要条件是:① 图是连通的; ② 所有节点的度为偶数,或者有仅有两个度数为奇数的节点。

这道题,求顶点的度数很好求,但是怎么判断图是否连通呢?通常来说,DFS/BFS或者并查集都能做。

不过,本题字典树的用处就是代替map,将输入字符串快速hash成一个值。

AC代码(BFS判图的连通性):

#include<bits/stdc++.h>
using namespace std;
const int maxn=+; int degree[*maxn];
vector<int> G[*maxn]; namespace Trie
{
const int SIZE=maxn*;
int sz;
int idtot;
struct TrieNode{
int ed;
int nxt[];
}trie[SIZE];
void init()
{
idtot=;
sz=;
}
int insert(const string& s)
{
int p=;
for(int i=;i<s.size();i++)
{
int ch=s[i]-'a';
if(!trie[p].nxt[ch]) trie[p].nxt[ch]=++sz;
p=trie[p].nxt[ch];
}
if(!trie[p].ed) trie[p].ed=++idtot;
return trie[p].ed;
}
}; bool vis[*maxn];
int bfs(int s)
{
int res=;
memset(vis,,sizeof(vis));
queue<int> Q;
Q.push(s), vis[s]=, res++;
while(!Q.empty())
{
int u=Q.front(); Q.pop();
for(int i=;i<G[u].size();i++)
{
int v=G[u][i];
if(!vis[v]) Q.push(v), vis[v]=, res++;
}
}
return res;
} int main()
{
ios::sync_with_stdio();
cin.tie(); cout.tie(); string s1,s2;
Trie::init();
while(cin>>s1>>s2)
{
int u=Trie::insert(s1), v=Trie::insert(s2);
G[u].push_back(v);
G[v].push_back(u);
degree[u]++;
degree[v]++;
} int cnt=;
for(int i=;i<=Trie::idtot;i++) {
if(degree[i]%) cnt++;
}
if(cnt== || cnt>) cout<<"Impossible\n";
else
{
if(bfs()<Trie::idtot) cout<<"Impossible\n";
else cout<<"Possible\n";
}
}

AC代码(并查集判图的连通性):

#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=+; namespace Trie
{
const int SIZE=maxn*;
int sz;
int idcnt;
struct TrieNode{
int ed;
int nxt[];
}trie[SIZE];
void init()
{
idcnt=;
sz=;
}
int insert(char *s)
{
int len=strlen(s), p=;
for(int i=;i<len;i++)
{
int ch=s[i]-'a';
if(!trie[p].nxt[ch]) trie[p].nxt[ch]=++sz;
p=trie[p].nxt[ch];
}
return (trie[p].ed)?(trie[p].ed):(trie[p].ed=++idcnt);
}
}; int par[*maxn],ran[*maxn];
int find(int x){return (par[x]==x)?x:(par[x]=find(par[x]));}
void unite(int x,int y)
{
x=find(x), y=find(y);
if(x==y) return;
if(ran[x]<ran[y]) par[x]=y;
else par[y]=x, ran[x]+=(ran[x]==ran[y]);
} int degree[*maxn];
int main()
{
Trie::init();
memset(degree,,sizeof(degree));
for(int i=;i<*maxn;i++) par[i]=i,ran[i]=; char s[][];
while(scanf("%s %s",s[],s[])!=EOF)
{
int u=Trie::insert(s[]);
int v=Trie::insert(s[]);
if(find(u)!=find(v)) unite(u,v);
degree[u]++;
degree[v]++;
} int cnt=;
int z=find();
for(int i=;i<=Trie::idcnt;i++)
{
if(find(i)!=z) {
printf("Impossible\n");
return ;
} if(degree[i]%) cnt++;
if(cnt>) {
printf("Impossible\n");
return ;
}
}
if(cnt==) {
printf("Impossible\n");
return ;
} else {
printf("Possible\n");
return ;
}
}

POJ 2513 - Colored Sticks - [欧拉路][图的连通性][字典树]的更多相关文章

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

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

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

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

  3. [欧拉] poj 2513 Colored Sticks

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

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

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

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

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

  6. POJ 2513 Colored Sticks(Tire+欧拉回(通)路判断)

    题目链接: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. POJ 2513 Colored Sticks (离散化+并查集+欧拉通路)

    下面两个写得很清楚了,就不在赘述. http://blog.sina.com.cn/s/blog_5cd4cccf0100apd1.htmlhttp://www.cnblogs.com/lyy2890 ...

  9. poj 2513 Colored Sticks (trie 树)

    链接:poj 2513 题意:给定一些木棒.木棒两端都涂上颜色,不同木棒相接的一边必须是 同样的颜色.求能否将木棒首尾相接.连成一条直线. 分析:能够用欧拉路的思想来解,将木棒的每一端都看成一个结点 ...

随机推荐

  1. Database数据库切片模式

    数据库切片模式关注的实现水平伸缩.切分是从单个数据库到平分数据访问两个或更多数据库切片.每个切片有和原始数据库相同的Schema.大多数据分布在每个切片每一行.从切片合并起来的数据和原始数据库一样.切 ...

  2. node服务器中打开html文件的两种方法

    方法1:利用 Express 托管静态文件,详情查看这里 方法2:使用fs模块提供的readFile方法打开文件,让其以text/html的形式输出. 代码: var express = requir ...

  3. 译:1. RabbitMQ Java Client 之 "Hello World"

    这些教程介绍了使用RabbitMQ创建消息传递应用程序的基础知识.您需要安装RabbitMQ服务器才能完成教程 1. 打造第一个Hello World 程序 RabbitMQ是一个消息代理:它接受和转 ...

  4. 译:3.RabbitMQ Java Client 之 Publish/Subscribe(发布和订阅)

    在上篇 RabbitMQ 之Work Queues (工作队列)教程中,我们创建了一个工作队列,工作队列背后的假设是每个任务都交付给一个工作者. 在这一部分,我们将做一些完全不同的事情 - 我们将向多 ...

  5. [ci] jenkins kubernetes插件配置(容器模式)-通过jnlp

    有个小伙用sh结合jenkins搞的k8s cicd还不错 jenkins kubernetes插件 首先插件管理,搜索kubernetes plugin安装 配置kubernetes云 配置项目 执 ...

  6. 基于mindwave脑电波进行疲劳检测算法的设计(1)

    一.简介 脑波,又称之为脑电波,是人大脑发出的电波,非常的微弱,只能通过设备来检测.人的脑波在不同状态下,会不同,因此可以通过脑波来量化分析人的精神状态. 科学家讲脑电波分为四种,以下为详细解释 (1 ...

  7. fiddler4 使用教程

    Fiddler是最强大最好用的Web调试工具之一,它能记录所有客户端和服务器的http和https请求,允许你监视,设置断点,甚至修改输入输出数据,Fiddler包含了一个强大的基于事件脚本的子系统, ...

  8. ABAP 内表(internal table) 标题行(header line) 工作区(work area) 简介 - [SAP]

    刚开始学ABAP的时候,学到iternal table时,感觉一阵混乱.搞不清楚什么是work area,什么是header line,以及occurs是干什么用的.今天终于差不多搞明白了(我还是太弱 ...

  9. Java知多少(101)图像缓冲技术

    当图像信息量较大,采用以上直接显示的方法,可能前面一部分显示后,显示后面一部分时,由于后面一部分还未从文件读出,使显示呈斑驳现象.为了提高显示效果,许多应用程序都采用图像缓冲技术,即先把图像完整装入内 ...

  10. Java如何从服务器获取文件大小?

    在Java编程中,如何从服务器获取文件大小? 以下示例演示如何从服务器获取文件大小. package com.yiibai; import java.net.URL; import java.net. ...