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

题意:给一些木棒,木棒两端图上颜色,将端点颜色相同的木棒连在一起,问是否能连成一条直线。

思路:将两端的颜色看成点,将木棒看成边,判断是否构成欧拉路。

欧拉路:图G,若存在一条路,经过G中每条边有且仅有一次,称这条路为欧拉路,如果存在一条回路经过G每条边有且仅有一次,

称这条回路为欧拉回路。具有欧拉回路的图成为欧拉图。

判断欧拉路是否存在的方法

有向图:图连通,有一个顶点出度大入度1,有一个顶点入度大出度1,其余都是出度=入度。

无向图:图连通,只有两个顶点是奇数度,其余都是偶数度的。

判断欧拉回路是否存在的方法

有向图:图连通,所有的顶点出度=入度。

无向图:图连通,所有顶点都是偶数度。

其中判断图的连通性用并查集。

 #include <stdio.h>
#include <string.h> const int N=;
using namespace std; struct trie
{
int id;
trie *next[];
trie()
{
id = ;
for (int i = ; i < ; i ++)
next[i] = NULL;
}
}*root;
int degree[N];
int f[N],cnt;
void init()
{
for (int i = ; i < N; i ++)
{
degree[i] = ;
f[i] = i;
}
cnt = ;
}
int find(int x)
{
if (x!=f[x])
f[x] = find(f[x]);
return f[x];
}
void merge(int x,int y)
{
x = find(x);
y = find(y);
f[x] = y;
}
int find_id(char s[])
{
int i = ;
trie *p = root;
while(s[i]!='\0')
{
if (p->next[s[i]-'a']==NULL)
p->next[s[i]-'a'] = new trie;
p = p->next[s[i++]-'a'];
}
if (p->id==)
p->id = ++cnt;
return p->id;
}
int main()
{
//freopen("sad.txt", "r", stdin);
int id1,id2;
char s1[],s2[];
init();
root = new trie;
while(~scanf("%s%s",s1,s2))
{ id1 = find_id(s1);
id2 = find_id(s2);
degree[id1]++;
degree[id2]++;
merge(id1,id2);
}
int count = ;
int father = find();
for (int i = ; i <= cnt; i ++)
{
if (degree[i]%)
count++;
if(count > ||find(i)!=father)
{
printf("Impossible\n");
return ;
}
}
printf("Possible\n");
return ;
}

Colored Sticks(trie)的更多相关文章

  1. 【python】Leetcode每日一题-前缀树(Trie)

    [python]Leetcode每日一题-前缀树(Trie) [题目描述] Trie(发音类似 "try")或者说 前缀树 是一种树形数据结构,用于高效地存储和检索字符串数据集中的 ...

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

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

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

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

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

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

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

    题目链接:http://poj.org/problem?id=2513 题目大意:你有好多根棍子,这些棍子的两端分都别涂了一种颜色.请问你手中的这些棍子能否互相拼接,从而形成一条直线呢? 两根棍子只有 ...

  6. LA3942-Remember the Word(Trie)

    题意: 有s个不同的单词,给出一个长字符串把这个字符串分解成若干个单词的连接(可重复使用),有多少种分解方法 分析: dp[i]表示i开始的字符串能分解的方法数 dp[i]=sum(dp[i+len( ...

  7. HDU 1671 Phone List (Trie)

    pid=1671">Phone List Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  8. HDU 3573 Buy Sticks (逻辑)

    题意:a,b,c三种棍子长度分别为20,28,32,现需要这三种棍子数根,欲买长为75的棍子来剪成这三种(不够长的就废弃) ,问需要买多少根. 思路:将所有棍子尽可能和其他搭配起来,使得数量减到最少. ...

  9. hdu 1251 统计难题 (字典树(Trie)<PS:C++提交不得爆内存>)

    统计难题Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submis ...

随机推荐

  1. CentOS7配置VSFTP服务器

    [1] 安装VSFTP [root@localhost ~]# yum -y install vsftpd [2] 配置vsftpd.conf文件 [root@localhost ~]# vi /et ...

  2. Nagios事件机制实践

    Nagios事件机制实践  blog地址:http://www.cnblogs.com/caoguo 一.事件触发执行脚本 [root@Nagios ~]# cd /usr/local/nagios/ ...

  3. Centos7安装gitlab服务器

    1.先按照官方教程 https://about.gitlab.com/downloads/#centos7 大概内容如下: 1. Install and configure the necessary ...

  4. VM虚拟机NAT链接外网

    1.vi /etc/sysconfig/networkNETWORKING=yesHOSTNAME=localhost.localdomainGATEWAY=192.168.110.2 2.vi /e ...

  5. Qt Creator 中文乱码问题

    一. Qt 4 乱码问题 解决方案 1. 在Qt 中 快捷菜单选项功能中 Edit(编辑)  --> Select Encoding...(选择编码) 选择载入(显示)编码和储存编码,要解决中文 ...

  6. CentOS 7.2安装配置Vsftp服务器

    一.配置防火墙,开启FTP服务器需要的端口 CentOS 7.2默认使用的是firewall作为防火墙,这里改为iptables防火墙. 1.关闭firewall: systemctl stop fi ...

  7. 【转载】Apache shutdown unexpectedly启动错误解决方法

    http://blog.csdn.net/dong123dddd/article/details/21372179 xampp启动时显示的错误为: 9:52:41  [Apache] Attempti ...

  8. 【codeforces 755F】PolandBall and Gifts

    [题目链接]:http://codeforces.com/contest/755/problem/F [题意] n个人; 计划是每个人都拿一个礼物来送给一个除了自己之外的人; 且如果一个人没有送出礼物 ...

  9. hdu_1065_I Think I Need a Houseboat_201311160023

    I Think I Need a Houseboat Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Ja ...

  10. [poj1062]昂贵的聘礼_最短路_离散化

    昂贵的聘礼 poj-1062 题目大意:原文链接?不是英文题,自己看 注释:$1\le N \le 100$. 想法:开始的想法有些过于简单,因为落下了一个条件:就是等级限制是一条路径上的任意两点而不 ...