POJ2513(字典树+图的连通性判断)
//用map映射TLE,字典树就AC了
#include"cstdio"
#include"set"
using namespace std;
const int MAXN=;
const int N=;//26个小写英文字母
struct node{
int val;//存放字符串的hash值
node* next[N];
};
node memory[MAXN];
int ant;
node* root;
node* create_tree()
{
node* p=&memory[ant++];
for(int i=;i<N;i++)
{
p->next[i]=NULL;
}
return p;
} void insert(char *s,int x)
{
node* p=root;
for(int i=;s[i];i++)
{
int k=s[i]-'a';
if(p->next[k]==NULL) p->next[k]=create_tree();
p=p->next[k];
}
p->val=x;
} int search(char *s)
{
node* p=root;
for(int i=;s[i];i++)
{
int k=s[i]-'a';
if(p->next[k]==NULL) return -;
p=p->next[k];
}
return p->val;
} int par[MAXN];
int rnk[MAXN];
void init()
{
for(int i=;i<=MAXN;i++)
{
par[i]=i;
rnk[i]=;
}
} int fnd(int x)
{
if(par[x]==x)
{
return x;
}
return par[x]=fnd(par[x]);
} void unite(int x,int y)
{
int a=fnd(x);
int b=fnd(y);
if(a==b) return; if(rnk[a]<rnk[b])
{
par[a]=b;
}
else
{
par[b]=a;
if(rnk[a]==rnk[b]) rnk[a]++;
} } int deg[MAXN];
int cnt;
/*
bool judge()
{
int odd=0;
for(int i=0;i<cnt;i++)
{
if(deg[i]%2==1) odd++;
}
if(odd!=0&&odd!=2) return false;
int f=fnd(0);
for(int i=1;i<cnt;i++)
{
if(f!=fnd(i)) return false;
}
return true;
}
*/
int main()
{
root=create_tree();
init();
char a[];
char b[];
int numer=;
while(scanf("%s%s",a,b)!=EOF)
{
if(a[]=='') break;
int y1,y2;
y1=search(a);
y2=search(b);
if(y1==-)
{
y1=cnt;
insert(a,y1);
cnt++;
}
if(y2==-)
{
y2=cnt;
insert(b,y2);
cnt++;
}
deg[y1]++;
deg[y2]++;
unite(y1,y2);
} int l=;
set<int> vec;
for(int i=;i<cnt;i++)
{
if(deg[i]%==) l++;
}
if((l==)||l==) //满足欧拉回路
{
for(int i=;i<cnt;i++)
{
int x=fnd(i);
vec.insert(x);
}
if(vec.size()<=) //满足图的连通性
{
printf("Possible\n");
return ;
}
}
printf("Impossible\n"); return ;
}
POJ2513(字典树+图的连通性判断)的更多相关文章
- poj2513字典树+欧拉图判断+并查集断连通
题意:俩头带有颜色的木棒,要求按颜色同的首尾相连,可能否? 思路:棒子本身是一条边,以俩端为顶点(同颜色共点),即求是否有无向图欧拉路(每条棒子只有一根, 边只能用一次,用一次边即选一次棒子). 先判 ...
- poj2513- Colored Sticks 字典树+欧拉通路判断
题目链接:http://poj.org/problem?id=2513 思路很容易想到就是判断欧拉通路 预处理时用字典树将每个单词和数字对应即可 刚开始在并查集处理的时候出错了 代码: #includ ...
- HDU-2072-单词数(字典树)
链接: https://vjudge.net/problem/HDU-2072 题意: lily的好朋友xiaoou333最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词的总数.下面 ...
- leetcode之820. 单词的压缩编码 | python极简实现字典树
题目 给定一个单词列表,我们将这个列表编码成一个索引字符串 S 与一个索引列表 A. 例如,如果这个列表是 ["time", "me", "bell& ...
- 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---欧拉回路+并查集+字典树
题目链接: https://vjudge.net/problem/POJ-2513 题目大意: 给一些木棍,两端都有颜色,只有两根对应的端点颜色相同才能相接,问能不能把它们接成一根木棍 解题思路: 题 ...
- POJ2513:Colored Sticks(字典树+欧拉路径+并查集)
http://poj.org/problem?id=2513 Description You are given a bunch of wooden sticks. Each endpoint of ...
- poj2513 Colored Sticks —— 字典树 + 并查集 + 欧拉回路
题目链接:http://poj.org/problem?id=2513 题解:通过这题了解了字典树.用字典树存储颜色,并给颜色编上序号.这题为典型的欧拉回路问题:将每种颜色当成一个点.首先通过并查集判 ...
- POJ2513 欧拉 + 字典树
POJ 2513 有N根木棒,一根木棒有2头,我们把每头涂色(相同或不同),如果2根木棒有相同颜色的一端就可以连接,颜色全部不同就不能连接,现在给你N根木棒以及它们的颜色,问最后能不能链接成1条链. ...
随机推荐
- Data Decisions: DSP vs. DMP
http://www.cmo.com/features/articles/2016/3/9/data-decisions-dsp-vs-dmp.html As marketers assess the ...
- Python菜鸟之路:Python基础(三)
一.编码 推荐阅读<字符编码的前世今生>:http://tgideas.qq.com/webplat/info/news_version3/804/808/811/m579/201307/ ...
- genymotion device manager列表没有
1.第一种原因:链接Genymotion官网的网络超时,无法加载Genymotion device列表,解决办法百度一下:配置Genymotion代理服务器,联网下载 2.第二种可能:检查是否正确安装 ...
- ALV行 列颜色设置
ALV的颜色设置分为3种:行.列.单元格. 1.列颜色的设置 在 slis_t_fieldcat_alv-emphasize 中,写入需要的颜色代码. Eg: DATA: fc TYP ...
- (3)mac下"-bash: mysql: command not found"解决方案
针对 mysql: command not found 输入命令 $ ln -s /usr/local/mysql/bin/mysql /usr/bin 假如你人品不好,被打脸了,提示你权限不够: l ...
- 读a paper of ICCV 2017 : Areas of Attention for Image Captioning
前言废话,作者说把代码公布在gitub上,但是迟迟没有公布,我发邮件询问代码情况,邮件也迟迟不回,表示很尴尬..虽然种种这些,但是工作还是好工作,这个没的黑,那我们今天就来详细的介绍这篇文章. 导论: ...
- 关于树莓派Pi2通过UART连接攀藤G5传感器的python
1.准备工作:树莓派Pi2板子,攀藤G5传感器 关于树莓派40pin口网上很多,我们只了解与攀藤G5连接的问题 (1)攀藤G5pin1(VCC5v)要注意是5V,有很多板子接的是3V,而树莓派的pin ...
- 小程序网络请求arraybuffer 转为base64
wx.request({ url: result.tempFilePath, method: 'GET', responseType: 'arraybuffer', success: function ...
- HDU - 1114 Piggy-Bank 【完全背包】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1114 题意 给出一个储钱罐 不知道里面有多少钱 但是可以通过重量来判断 先给出空储钱罐的重量 再给出装 ...
- Python 运算符(算术运算符(+,-,*,**,/,//),逻辑运算符(not , or ,and),比较运算符(>,<,>=,=<),复合运算符(+=,-=,*=,/=,**=,//=))
# 一.算术运算符(+,-,*,**, /, //, %) # 加法运算符+ print(1 + 2) # 字符串相连 ") # 重载 print([1,2] + [3,4]) # 幂运算* ...