洛谷.1333.瑞瑞的木棍(欧拉路径 Hash)
#include <cstdio>
#include <cstring>
const int N=2e6+5,M=5e5+5,mod=2e6;
const int seed[5]={31,37,131,41};
int dgr[N],fa[M];
char s[15];
namespace Hash
{
int cnt,val[N],pos[N];
int Get_Hash(char *s)
{
int x=0,l=strlen(s);
for(int i=0; i<l; ++i) x=(x+s[i]*seed[i%4])%mod;
return x;
}
int Insert(char *s)
{
int p=Get_Hash(s);
while(val[p]&&val[p]!=p){
++p;
if(p>=mod) p-=mod;
}
if(val[p]) return pos[p];
val[p]=p;
return pos[p]=++cnt;
}
}
int Find(int x){
return x==fa[x]?x:fa[x]=Find(fa[x]);
}
int main()
{
int p1,p2,r1,r2,t=0;
for(int i=1; i<M; ++i) fa[i]=i;
while(~scanf("%s",s))
{
p1=Hash::Insert(s);
scanf("%s",s);
p2=Hash::Insert(s);
++dgr[p1], ++dgr[p2];
r1=Find(p1), r2=Find(p2);
if(r1!=r2) ++t,fa[r1]=r2;
}
if(t<Hash::cnt-1) {printf("Impossible"); return 0;}
int tot=Hash::cnt; t=0;
for(int i=1; i<=tot; ++i)
if(dgr[i]&1) ++t;
printf(t>2?"Impossible":"Possible");
return 0;
}
洛谷.1333.瑞瑞的木棍(欧拉路径 Hash)的更多相关文章
- 洛谷 P4036 [JSOI2008]火星人(splay+字符串hash)
题面 洛谷 题解 首先,我们知道求最长公共前缀可以用二分答案+hash来求 因为有修改操作, 考虑将整个字符串的hash值放入splay中 接着就是splay的基本操作了 Code #include& ...
- 【洛谷3546_BZOJ2803】[POI2012]PRE-Prefixuffix(String Hash)
Problem: 洛谷3546 Analysis: I gave up and saw other's solution when I had nearly thought of the method ...
- 洛谷P3234 抄卡组 [HNOI2014] 字符串hash
正解:字符串hash 解题报告: 传送门! 字符串hash是字符串匹配中很常见的一个方法,原理也很好懂,这里就不做太多阐述辣有时间放到hash笔记里面去QAQ 题意不说了挺好理解的,自带一句话概括好评 ...
- 洛谷P3538 [POI2012]OKR-A Horrible Poem [字符串hash]
题目传送门 A Horrible Poem 题目描述 Bytie boy has to learn a fragment of a certain poem by heart. The poem, f ...
- 洛谷P1333 瑞瑞的木棍(欧拉回路)
题目描述 瑞瑞有一堆的玩具木棍,每根木棍的两端分别被染上了某种颜色,现在他突然有了一个想法,想要把这些木棍连在一起拼成一条线,并且使得木棍与木棍相接触的两端颜色都是相同的,给出每根木棍两端的颜色,请问 ...
- 洛谷【P1090】合并果子&&洛谷【P1334】瑞瑞的木板
浅谈堆:https://www.cnblogs.com/AKMer/p/10284629.html 合并果子题目传送门:https://www.luogu.org/problemnew/show/P1 ...
- 洛谷 P1334 瑞瑞的木板==P2664 【题目待添加】
题目描述 瑞瑞想要亲自修复在他的一个小牧场周围的围栏.他测量栅栏并发现他需要N(1≤N≤20,000)根木板,每根的长度为整数Li(1≤Li≤50,000).于是,他神奇地买了一根足够长的木板,长度为 ...
- P1334 瑞瑞的木板 洛谷
https://www.luogu.org/problem/show?pid=1334 题目描述 瑞瑞想要亲自修复在他的一个小牧场周围的围栏.他测量栅栏并发现他需要N(1≤N≤20,000)根木板,每 ...
- 洛谷 P1334 瑞瑞的木板
P1334 瑞瑞的木板 题目描述 瑞瑞想要亲自修复在他的一个小牧场周围的围栏.他测量栅栏并发现他需要N(1≤N≤20,000)根木板,每根的长度为整数Li(1≤Li≤50,000).于是,他神奇地买了 ...
随机推荐
- C语言中malloc函数返回值是否需要类型强制转换问题
1. 在C语言中, 如果调用的函数没有函数原型, 则其返回值将默认为 int 型. 考虑调用malloc函数时忘记了 #include <stdlib.h>的情况 此时malloc函数返回 ...
- AD域相关的属性和C#操作AD域
“常规”标签 姓 Sn 名 Givename 英文缩写 Initials 显示名称 displayName 描述 Description 办公室 physicalDeliveryOfficeNam ...
- Python之matplotlib库学习
matplotlib 是python最著名的绘图库,它提供了一整套和matlab相似的命令API,十分适合交互式地进行制图.而且也可以方便地将它作为绘图控件,嵌入GUI应用程序中. 它的文档相当完备, ...
- OpenWrt启动过程分析+添加自启动脚本【转】
一.OpenWrt启动过程分析 转自: http://www.eehello.com/?post=107 总结一下OpenWrt的启动流程:1.CFE->2.linux->3./etc/p ...
- python令牌桶算法
import time class TokenBucket(object): # rate是令牌发放速度,capacity是桶的大小 def __init__(self, rate, capacity ...
- Android通讯:短信
Android通讯之短信功能实现: 使用android.telephony.SmsManager对象,可以发送短信和彩信.// 构造回调函数,短信发送结束后,会发出对应的Intent请求Intent ...
- Go语言规格说明书 之 Go语句(Go statements)
go version go1.11 windows/amd64 本文为阅读Go语言中文官网的规则说明书(https://golang.google.cn/ref/spec)而做的笔记,介绍Go语言的 ...
- LeetCode(38): 报数
Easy! 题目描述: 报数序列是指一个整数序列,按照其中的整数的顺序进行报数,得到下一个数.其前五项如下: 1. 1 2. 11 3. 21 4. 1211 5. 111221 1 被读作 &qu ...
- linux 终端上网设置
原网址: https://www.aliyun.com/jiaocheng/215068.html 摘要:第一步,需要安装一个名为w3m的软件工具,打开终端,输入如下命令sudoapt-getinst ...
- pytest十:用例 a 失败,跳过测试用例 b 和 c 并标记失败 xfail
当用例 a 失败的时候,如果用例 b 和用例 c 都是依赖于第一个用例的结果,那可以直接跳过用例 b 和 c 的测试,直接给他标记失败 xfail用到的场景,登录是第一个用例,登录之后的操作 b 是第 ...