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)次(即在这条边连接的两个点 ...
随机推荐
- 自制rpm包
参考自:https://blog.csdn.net/u010384744/article/details/80929319 https://blog.csdn.net/samxx8/article/d ...
- 执行maven install跳过执行maven test方法(网上搜的记录一下,方面以后使用)
直接在pom文件加上这段配置就可以了 <plugin> <groupId>org.apache.maven.plugins</groupId> ...
- 【防坑指南】nginx重启后出现[error] open() “/usr/local/var/run/nginx/nginx.pid” failed
重新启动nginx后,出现报错,原因就是下没有nginx文件夹或没有nginx.pid文件,为什么会没有呢? 原因就是每次重新启动,系统都会自动删除文件,所以解决方式就是更改pid文件存储的位置, 打 ...
- pythonの递归锁
首先看一个例子,让我们lock = threading.Lock() 时(代码第33行),程序会卡死在这里 #!/usr/bin/env python import threading,time de ...
- LwIP Application Developers Manual3---链路层和网络层协议之ARP,IPV4
1.前言 本文主要讲述链路层和网络层的几种协议:ARP,ipv4 2. ARP 2.1 ARP的主要应用 ARP的主要应用是在与互联网相连的以太网网络层,该层需要一些机制将MAC地址(该地址主要由制造 ...
- TreeGrid 控件集 :delphi 学习群 ---- 166637277 (Delphi学习交流与分享)
delphi 学习群: 166637277 (Delphi学习交流与分享). 群主QQ: 1936431438 TreeGrid 控件集 收集: 1.https://www.lmd.de/produ ...
- Python备份MySQL数据库【转】
#!/usr/bin/env python # coding: utf- import os import time ''' defined variable ''' databases=['hch' ...
- EMOS之邮件服务器
作者:邓聪聪 EMOS一键自动安装镜像,邮件服务器配置
- Liunx之Centos系统无人值守全自动化安装
作者:邓聪聪 定制centos6.8自动安装ISO光盘 安装系统为centos6.8 (base server),安装方式为全新安装 使用ext4分区格式 安装前可以交互输入root密码,主机名,分区 ...
- 关于hostapd的调试
对于hostapd和wpa_supplicant 的调试时,希望显示更多的调试信息. 未改动代码时,可以将hostapd 进程拉起时所跟的参数加上"-dd". 即使这样,也不能满足 ...