poj2513连接木棍(字典树+欧拉回路+并查集)
题目大意:给你一堆木棍,每根木管都有两种颜色,相同颜色的部分可以连接起来,问你这堆木棍可不可以连接成1根。
思路:大致的思路很好想,就是判断欧拉回路的方法(1.联通,2,要么顶点读书全为偶数,要么有两个奇数),统计每种颜色出现的次数就可以了。问题的关键是怎么统计,大家第一反应肯定是并查集统计联通路,用map统计次数,但这道题的数据量是25w*2,map会超时,然后想到了字典树。
我是用数组写的字典树,root表示的是某一种颜色的标号,(哈希的思想),这样空间开的会很大,理论上应该过不了,但是A了。
还有一种方法是链表写字典树,这也是我第一次用链表写题目,借鉴了kuangbin老师的博客,接下来两种代码都附上。
//数组写法 好理解
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<bitset>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#define INF 0x3f3f3f3f
#define CLR(x,y) memset(x,y,sizeof(x))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
using namespace std;
typedef pair<int,int> pii;
typedef long long ll;
const double PI=acos(-1.0);
int fact[10]= {1,1,2,6,24,120,720,5040,40320,362880};
const int maxn= 5000010;
int in[maxn],ou[maxn];
int tot;
int trie[maxn][26];// 最多50w个单词 开50w*10 (居然没爆 md)
int fa[maxn];
int find(int x){
return x==fa[x]?x:fa[x]=find(fa[x]);
}
void baba(int x,int y){
int fx=find(x),fy=find(y);
fa[fx]=fy;
}
int iinsert(char *s)
{
int root=0;
int len=strlen(s);
for(int i=0;i<len;i++)
{
int id=s[i]-'a';
if(!trie[root][id])
{
trie[root][id]=++tot;
}
root=trie[root][id];
}
return root;//root表示某一个字母的标号 虽然数字分布的很宽 不过空间换时间
}
bool v[maxn];
int main(){
char s1[20],s2[20];
int x,y;
int cas=0;
memset(v,false,sizeof(v));
for(int i=1;i<=maxn;i++)fa[i]=i;
while(scanf("%s%s",s1,s2)!=EOF)
{
x=iinsert(s1);
y=iinsert(s2);
in[x]++;//统计出度 和 入度 欧拉回路判定标准
ou[y]++;
baba(x,y);//将可以连着的木棍并查集并起来 防止出现两个欧拉回路
v[x]=true;//由于之前的root分布的太广了 所以用v[]来表示某一个数字是否代表了一种颜色 不是的话就直接跳过 节约时间
v[y]=true;
}
int f1=0,m=-1,flag=0;
for(int i=1;i<=tot;i++)
{
if(v[i])
if(find(i)!=m){
if(!flag){
m=find(i);
flag=1;
}else{//如果连通图超过两个就不对了
printf("Impossible\n");
return 0;
}
}
if(!v[i]||in[i]==ou[i])continue;
if((in[i]+ou[i])%2)
{
f1++;
}
if(f1>2)//如果奇数点超过两个就不对了
{
printf("Impossible\n");
return 0;
}
}
printf("Possible\n");
}
//链表写法
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<bitset>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#define INF 0x3f3f3f3f
#define CLR(x,y) memset(x,y,sizeof(x))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
using namespace std;
typedef long long ll;
const int maxn=500010;
int color;
int fa[maxn];
int degree[maxn];
int find(int x)
{
return fa[x]==x?x:fa[x]=find(fa[x]);
}
void baba(int x,int y)
{
int fx=find(x),fy=find(y);
fa[fx]=fy;
}
const int MAX=26;
typedef struct trie_node
{
bool isword;
struct trie_node *next[MAX];//链表
int id;
}trie;
int insert(trie *root,char *s)
{
trie *p=root;
int i=0;
while(s[i]!='\0')
{
if(p->next[s[i]-'a']==NULL)
{
trie *temp=new trie;
temp->isword=false;//如果没有这个节点,则标记为没有访问过
for(int j=0;j<MAX;j++)
{
temp->next[j]=NULL;//将新节点的每一个子节点都赋值为NULL
}
temp->id=0;
p->next[s[i]-'a']=temp;
}
p=p->next[s[i]-'a'];//顺着字典树走
i++;
}
if(p->isword)//如果访问过(之前插入过 id应该已经有值了)
{
return p->id;
}
else
{
p->isword=true;//如果没有 说明是新的颜色
p->id=color++;
return p->id;
}
}
int main(){
char s1[20],s2[20];
trie *root=new trie;
for(int i=0;i<26;i++)
root->next[i]=NULL;
root->isword=false;
root->id=0;
color=0;
for(int i=0;i<maxn;i++)fa[i]=i;
memset(degree,0,sizeof(degree));
while(scanf("%s%s",&s1,&s2)!=EOF)
{
int t1=insert(root,s1);
int t2=insert(root,s2);
degree[t1]++;
degree[t2]++;
baba(t1,t2);
}
int cnt1=0,cnt2=0;
for(int i=0;i<color;i++)
{
if(fa[i]==i)cnt1++;
if(degree[i]%2==1)cnt2++;
if(cnt1>1)break;
if(cnt2>2)break;
}
if((cnt1==0||cnt1==1)&&(cnt2==0||cnt2==2))
printf("Possible\n");
else printf("Impossible\n");
return 0;
}
| Time Limit: 5000MS | Memory Limit: 128000K | |
| Total Submissions: 38938 | Accepted: 10168 |
Description
Input
Output
Sample Input
blue red
red violet
cyan blue
blue magenta
magenta cyan
Sample Output
Possible
Hint
poj2513连接木棍(字典树+欧拉回路+并查集)的更多相关文章
- POJ2513——Colored Sticks(Trie树+欧拉回路+并查集)
Colored Sticks DescriptionYou are given a bunch of wooden sticks. Each endpoint of each stick is col ...
- POJ2513:Colored Sticks(字典树+欧拉路径+并查集)
http://poj.org/problem?id=2513 Description You are given a bunch of wooden sticks. Each endpoint of ...
- POJ-2153Colored Sticks解题报告+欧拉回路,字典树,并查集;
传送门:http://poj.org/problem?id=2513 题意:给你许多木棍,木棍两端都有颜色,问能不能首尾相接,要求颜色相同. 参考:https://www.cnblogs.com/ku ...
- POJ 2513 Colored Sticks(欧拉回路,字典树,并查集)
题意:给定一些木棒,木棒两端都涂上颜色,求是否能将木棒首尾相接,连成一条直线,要求不同木棒相接的一边必须是相同颜色的. 无向图存在欧拉路的充要条件为: ① 图是连通的: ② 所有节 ...
- POJ 2513 Colored Sticks 字典树、并查集、欧拉通路
Description You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some ...
- BZOJ4399魔法少女LJJ——线段树合并+并查集
题目描述 在森林中见过会动的树,在沙漠中见过会动的仙人掌过后,魔法少女LJJ已经觉得自己见过世界上的所有稀奇古怪的事情了LJJ感叹道“这里真是个迷人的绿色世界,空气清新.淡雅,到处散发着醉人的奶浆味: ...
- [bzoj3123][sdoi2013森林] (树上主席树+lca+并查集启发式合并+暴力重构森林)
Description Input 第一行包含一个正整数testcase,表示当前测试数据的测试点编号.保证1≤testcase≤20. 第二行包含三个整数N,M,T,分别表示节点数.初始边数.操作数 ...
- Educational Codeforces Round 51 (Rated for Div. 2) G. Distinctification(线段树合并 + 并查集)
题意 给出一个长度为 \(n\) 序列 , 每个位置有 \(a_i , b_i\) 两个参数 , \(b_i\) 互不相同 ,你可以进行任意次如下的两种操作 : 若存在 \(j \not = i\) ...
- 2018.09.30 bzoj4025: 二分图(线段树分治+并查集)
传送门 线段树分治好题. 这道题实际上有很多不同的做法: cdq分治. lct. - 而我学习了dzyo的线段树分治+并查集写法. 所谓线段树分治就是先把操作分成lognlognlogn个连续不相交的 ...
随机推荐
- python的面向对象编程
面向对象编程是一种程序的范式,它把程序看成是对不同对象的相互调用,对现实世界建立的一种模型. 面向对象编程的基本思想,类和实例.类用于定义抽象对象,实例根据类的定义被创建出来. 在python当中我们 ...
- python去掉括号之间的字符
在字符串中识别括号并删除括号及其中的内容括号包括 大中小 3种括号 输入为 1个字符串 s="我是一个人(中国人)[真的]{确定}"; 输出为 result = "我是一 ...
- ActiveMQ (三) Spring整合JMS入门
Spring整合JMS入门 前提:安装好了ActiveMQ ActiveMQ安装 Demo结构: 生产者项目springjms_producer: pom.xml <?xml versio ...
- day36-hibernate检索和优化 01-上次课内容回顾
clear():直接清空一级缓存的所有对象.evict(Object obj):清空一个对象.flush():控制你的刷出的时机.refresh(Object obj);将你的数据查出来把一级缓存覆盖 ...
- 【总结整理】关于ArcGIS中拓扑的理解
空间拓扑: https://www.baidu.com/link?url=f8fd1d75GhwtT1JuyPDZydZlWCgEXB9DeuTzDqGQIIRpq0bM-8t3MlC5tXYvEwQ ...
- winform 公共控件 ListView
//数据显示,刷新 public void F5() { listView1.Items.Clear(); List<Students> Stu = new StudentsData(). ...
- Socket编程--TCP粘包问题
TCP是个流协议,它存在粘包问题 产生粘包的原因是: TCP所传输的报文段有MSS的限制,如果套接字缓冲区的大小大于MSS,也会导致消息的分割发送. 由于链路层最大发送单元MTU,在IP层会进行数据的 ...
- Java数据结构与算法分析-第一章(引论)-Java中的范型<T,E>构件
一.为什么需要使用范型? 官方的说法是:Java 泛型(generics)是 JDK 5 中引入的一个新特性, 泛型提供了编译时类型安全检测机制,该机制允许程序员在编译时检测到非法的类型. 泛型的本质 ...
- 一劳永逸搭建android开发环境(android官网reference sample api tutorial全下载)
[摘要]本文简单介绍了android开发环境的搭建,重点介绍了SDK manager和AVD升级问题:并提供了android reference,sample,api,及docs的下载信息. [1]为 ...
- ArcGIS Server GP服务使用常见错误总结
ArcGIS GP服务问题列表 输入参数错误 在使用GP服务时,从创建模型到发布服务,再到调用服务,整个过程都需要注意输入参数和输出参数的问题.GP服务支持的输入和输出参数可详见 http://hel ...