poj2513--并查集+欧拉路+字典树
经典好题,自己不知道哪里错了交上去是RE,可能是数组开的不好吧,字典树老碰到这种问题。。
先马上别人的代码,有空对拍看看
#include <cstdio>
#include <cstring>
int const MAX = ;
int fa[MAX], d[MAX], cnt; struct Trie
{
int sz, t[MAX][];
int jud[MAX];
Trie()
{
sz = ;
memset(t[], -, sizeof(t));
jud[] = ;
}
void clear()
{
sz = ;
memset(t[], -, sizeof(t));
jud[] = ;
}
int idx(char c)
{
return c - 'a';
}
void insert(char* s, int v)
{
int u = , len = strlen(s);
for(int i = ; i < len; i++)
{
int c = idx(s[i]);
if(t[u][c] == -)
{
memset(t[sz], -, sizeof(t[sz]));
jud[sz] = ;
t[u][c] = sz++;
}
u = t[u][c];
}
jud[u] = v;
}
int search(char* s)
{
int u = , len = strlen(s);
for(int i = ; i < len; i++)
{
int c = idx(s[i]);
if(t[u][c] == -)
return -;
u = t[u][c];
}
if(jud[u])
return jud[u];
return -;
}
}t; void Init()
{
for(int i = ; i < MAX; i++)
fa[i] = i;
} int Find(int x)
{
return x == fa[x] ? x : fa[x] = Find(fa[x]);
} void Union(int a, int b)
{
int r1 = Find(a);
int r2 = Find(b);
if(r1 != r2)
fa[r1] = r2;
} bool eluer()
{
int sum = , t = -;
for(int i = ; i < cnt; i++)
if(d[i] % )
sum++;
if(sum != && sum != )
return false;
for(int i = ; i < cnt; i++)
{
if(t == -)
t = Find(i);
else if(Find(i) != Find(t))
return false;
}
return true;
} int main()
{
char s1[],s2[];
cnt = ;
Init();
t.clear();
while(scanf("%s %s", s1, s2) != EOF)
{
if(t.search(s1) == -)
t.insert(s1, cnt++);
int u = t.search(s1);
if(t.search(s2) == -)
t.insert(s2, cnt++);
int v = t.search(s2);
Union(u, v);
d[u]++;
d[v]++;
}
if(eluer())
printf("Possible\n");
else
printf("Impossible\n");
}
下面是RE代码
/*
1 2
2 3
4 1
1 5
5 4
1 5,5 4,4 1,1 2,2 3
数据量大,不能用map
建立字典树,保留每个颜色的id
更新每个颜色的度,只要一个颜色的度为奇数,cnt1++
用并查集保存图的连通性,如果最后剩下两个集,那么图不连通
注意:每个颜色都是一个点,每根木棒连接两个颜色,这就可以看做是并查集的连接
500*26
*/
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
struct Trie{
int next[][];
int id[];
int root,L,cnt;
int newnode(){
for(int i=;i<;i++)
next[L][i]=-;
id[L]=;
return L++;
}
void init(){
L=cnt=;
root=newnode();
}
int insert(char s[]){
//cout << s << cnt<<endl;
int len=strlen(s);
int now=root;
for(int i=;i<len;i++){
if(next[now][s[i]-'a']==-)
next[now][s[i]-'a']=newnode();
now=next[now][s[i]-'a'];
}
if(id[now]==)
id[now]=++cnt;
return id[now];
}
}tr;
int degree[*];
int F[*];
int find(int x){
if(F[x]==-) return x;
return F[x]=find(F[x]);
}
void bing(int a,int b){
int t1=find(a);
int t2=find(b);
if(t1!=t2) F[t1]=t2;
}
int main(){
tr.init();
memset(degree,,sizeof degree);
memset(F,-,sizeof F);
char s1[],s2[];
while(scanf("%s%s",s1,s2)!=EOF){
int id1=tr.insert(s1);
int id2=tr.insert(s2);
degree[id1]++;
degree[id2]++;
bing(id1,id2);
}
int cnt1=,cnt2=;//度为奇数的颜色,联通块
for(int i=;i<=tr.cnt;i++){
if(degree[i]%==) cnt1++;
if(F[i]==-) cnt2++;
if(cnt1>) break;
if(cnt2>) break;
} if ((cnt2== || cnt2==) && (cnt1== || cnt1==))
puts("Possible");
else puts("Impossible");
return ;
}
poj2513--并查集+欧拉路+字典树的更多相关文章
- Colored Sticks (字典树哈希+并查集+欧拉路)
Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 27704 Accepted: 7336 Description You ...
- poj 2513 Colored Sticks (trie树+并查集+欧拉路)
Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 40043 Accepted: 10406 ...
- NYOJ--42--dfs水过||并查集+欧拉通路--一笔画问题
dfs水过: /* Name: NYOJ--42--一笔画问题 Author: shen_渊 Date: 18/04/17 15:22 Description: 这个题用并查集做,更好.在练搜索,试试 ...
- 【bzoj4399】魔法少女LJJ 并查集+权值线段树合并
题目描述 在森林中见过会动的树,在沙漠中见过会动的仙人掌过后,魔法少女LJJ已经觉得自己见过世界上的所有稀奇古怪的事情了LJJ感叹道“这里真是个迷人的绿色世界,空气清新.淡雅,到处散发着醉人的奶浆味: ...
- Colored Sticks POJ - 2513 并查集+欧拉通路+字典树hash
题意:给出很多很多很多很多个棒子 左右各有颜色(给出的是单词) 相同颜色的可以接在一起,问是否存在一种 方法可以使得所以棒子连在一起 思路:就是一个判欧拉通路的题目,欧拉通路存在:没奇度顶点 或者 ...
- POJ2513 欧拉 + 字典树
POJ 2513 有N根木棒,一根木棒有2头,我们把每头涂色(相同或不同),如果2根木棒有相同颜色的一端就可以连接,颜色全部不同就不能连接,现在给你N根木棒以及它们的颜色,问最后能不能链接成1条链. ...
- 【BZOJ-3673&3674】可持久化并查集 可持久化线段树 + 并查集
3673: 可持久化并查集 by zky Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 1878 Solved: 846[Submit][Status ...
- POJ 2513 Colored Sticks (离散化+并查集+欧拉通路)
下面两个写得很清楚了,就不在赘述. http://blog.sina.com.cn/s/blog_5cd4cccf0100apd1.htmlhttp://www.cnblogs.com/lyy2890 ...
- 牛客练习赛40 C 小A与欧拉路(树的直径)
链接:https://ac.nowcoder.com/acm/contest/369/C 题目描述 小A给你了一棵树,对于这棵树上的每一条边,你都可以将它复制任意(可以为0)次(即在这条边连接的两个点 ...
随机推荐
- Jmeter Md5加密操作之-------BeanShell PreProcessor
背景: 有一些登录会做一些md5校验,通过jmeter的BeanShell可以解决MD5加密情况. 1.首先需要一个解码的jar包,commons-codec-1.10.jar(网上很多),下载后,放 ...
- 域scope 介绍,及查找数据
书中介绍<jsp:useBean> 中属性 scope: <%@ page language="java" pageEncoding="UTF-8& ...
- CNN学习入门
https://blog.csdn.net/ice_actor/article/details/78648780
- 6034 Balala Power! (17多校)
题目大意:给出的字符串,每个字符建立一种与0-25的对应关系.然后每个字符串看成是一个26进制的数.问能获得的数的总和的最大值.(最后对1e9+7取模). 题目思考:把每个字符的贡献值看做一个26进制 ...
- python基础-----变量和简单数据类型
初识变量 1.变量命名规则: a.字母 b.数字(不能开头) c.下划线 ps.硬性规定,命名必须是字母,数字,下划线,且不能以数字开头. 软性规则,以下划线分割 2.实例: a.写法: age_of ...
- SpringSecurity如何在代码中获取认证用户信息
⒈ public Object getCurrentUser(){ return SecurityContextHolder.getContext().getAuthentication(); } ⒉ ...
- python 十大web框架排名总结
0 引言 python在web开发方面有着广泛的应用.鉴于各种各样的框架,对于开发者来说如何选择将成为一个问题.为此,我特此对比较常见的几种框架从性能.使用感受以及应用情况进行一个粗略的分析. 1 D ...
- Linker Scripts3--SECTIONS Command
1.前言 SECTIONS命令告诉链接器如何映射输入段到输出段以及在内存中如何放置输出段,SECTIONS命令的格式如下: SECTIONS { sections-command sections-c ...
- for..of和for..in和map、filter等循环区别
1.for in遍历的是数组的索引(即键名),而for of遍历的是数组元素值. for in遍历比较适合遍历对象,不太适合数组,有可能遍历出来的不按照顺序 遍历数组 ,,,,,] for (var ...
- win10 python27pyhton36共存
先前安装了python36 然后安装python27,安装步骤如下 1. 到官网下载https://www.python.org/downloads/windows/,我的是win10 64位,选择了 ...