poj 1733 并查集+hashmap
题意:题目:有一个长度 已知的01串,给出多个条件,[l,r]这个区间中1的个数是奇数还是偶数,问前几个是正确的,没有矛盾
链接:点我
解题思路:hash离散化+并查集
首先我们不考虑离散化:s[x]表示(root[x],x]区间1的个数的奇偶性,0-偶数,1-奇数
每个输入区间[a,b],首先判断a-1与b的根节点是否相同
a)如果相同表示(a-1,b]之间1的个数奇偶性已知s((a-1,b])=s[a-1]^s[b],此时只需简单判断即可
b)如果不同,我们需要合并两个子树,我们将root较大的子树(例root[a])合并到root较小的子树(例root[b]),且此时s[root[a]]=s[a]^s[b]^s((a-1,b])
在路径压缩的过程中s[i]=s[i]^s[root[i]],s[root[i]]为(root[root[i]], root[i]]区间内1个数的奇偶性,例(a, b]区间1的个数为偶数,(b, c]区间1的个数为奇数,(a, c]之间1的个数显然为0^1=1奇数
/*
POJ 1733
HASH+并查集实现
*/
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <string.h>
using namespace std; const int HASH=;
const int MAXN=; struct HASHMAP
{
int head[HASH];
int next[MAXN];
int size;
int state[MAXN];
void init()
{
size=;
memset(head,-,sizeof(head));
}
int push(int st)
{
int i,h=st%HASH;
for(i=head[h];i!=-;i=next[i])
if(state[i]==st)
return i;
state[size]=st;
next[size]=head[h];
head[h]=size++;
return size-;
}
}hm;
int F[MAXN];
int val[MAXN];
int find(int x)
{
if(F[x]==-)return x;
int tmp=find(F[x]);
val[x]^=val[F[x]];
return F[x]=tmp;
} int main()
{
int n,m;
int u,v;
char str[];
while(scanf("%d%d",&n,&m)==)
{
hm.init();
memset(F,-,sizeof(F));
memset(val,,sizeof(val));
int ans=m;
for(int i=;i<m;i++)
{
scanf("%d%d%s",&u,&v,&str);
if(u>v)swap(u,v);
if(ans<m)continue; u=hm.push(u-);
v=hm.push(v);
//printf("%d %d\n",u,v); int tmp;
if(str[]=='e')tmp=;
else tmp=;
int t1=find(u);
int t2=find(v); if(t1==t2)
{
if(val[u]^val[v]!=tmp)ans=i;
}
else
{
F[t2]=t1;
val[t2]=tmp^val[u]^val[v];
} }
printf("%d\n",ans);
}
return ;
}
poj 1733 并查集+hashmap的更多相关文章
- poj 1984 并查集
题目意思是一个图中,只有上下左右四个方向的边.给出这样的一些边, 求任意指定的2个节点之间的距离. 就是看不懂,怎么破 /* POJ 1984 并查集 */ #include <stdio.h& ...
- poj 1797(并查集)
http://poj.org/problem?id=1797 题意:就是从第一个城市运货到第n个城市,最多可以一次运多少货. 输入的意思分别为从哪个城市到哪个城市,以及这条路最多可以运多少货物. 思路 ...
- POJ 2492 并查集扩展(判断同性恋问题)
G - A Bug's Life Time Limit:10000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u S ...
- POJ 2492 并查集应用的扩展
A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 28651 Accepted: 9331 Descri ...
- POJ 3228 [并查集]
题目链接:[http://poj.org/problem?id=3228] 题意:给出n个村庄,每个村庄有金矿和仓库,然后给出m条边连接着这个村子.问题是把所有的金矿都移动到仓库里所要经过的路径的最大 ...
- poj 3310(并查集判环,图的连通性,树上最长直径路径标记)
题目链接:http://poj.org/problem?id=3310 思路:首先是判断图的连通性,以及是否有环存在,这里我们可以用并查集判断,然后就是找2次dfs找树上最长直径了,并且对树上最长直径 ...
- POJ 3657 并查集
题意: 思路: 1.二分+线段树(但是会TLE 本地测没有任何问题,但是POJ上就是会挂--) 2.二分+并查集 我搞了一下午+一晚上才搞出来----..(多半时间是在查错) 首先 如果我们想知道这头 ...
- poj 2236 并查集
并查集水题 #include<cstdio> #include<iostream> #include<algorithm> #include<cstring& ...
- poj 1417(并查集+简单dp)
True Liars Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2087 Accepted: 640 Descrip ...
随机推荐
- HDU 1148 Rock-Paper-Scissors Tournament (模拟)
题目链接 Problem Description Rock-Paper-Scissors is game for two players, A and B, who each choose, inde ...
- python笔记之BytesIO
1. 什么是BytesIO BytesIO与StringIO类似,不同的是StringIO只能存放string,BytesIO是用来存放bytes的,它提供了在内存中读写字节的能力. 即在内存中读写字 ...
- java.lang.NoClassDefFoundError: HttpServletRequest
在eclipse里启动tomcat报错,错误日志:Caused by: java.lang.ClassNotFoundException: HttpServletRequest 在build path ...
- cgic实现输入文件名,打开文件的功能
a.c文件 #include <stdio.h>#include <stdlib.h>#include <string.h>#include <stdarg. ...
- VMW虚拟机生成的文件说明
VMDK(VMWare Virtual Machine Disk Format)是虚拟机VMware创建的虚拟硬格式,文件存在于VMware文件系统中,被称为VMFS(虚拟机文件系统) NVRAM 非 ...
- js基础练习(四)
练习: 通过循环按行顺序为一个5×5的二维数组a赋1到25的自然数,然后输出该数组的左下半三角.试编程. 2 3 4 5 6 7 8 9 10 11 12 13 14 ...
- codeforce 1A Theatre Square
A. Theatre Square Theatre Square in the capital city of Berland has a rectangular shape with the siz ...
- bzoj 1113
思路:对于两张高度一样的海报 i, j, 即 y[ i ] = y[ j ], 如果对于任意i < k < j 有y[ k ] > y[ i ] && y[ k ] ...
- webpack3.0+总结
ul>li{ color:blue; font-size:20px } .items>ul>li>ul>li{ color:crimson } --> webpac ...
- info.plist文件里面添加描述 -> 配置定位,相册等
<key>NSAppleMusicUsageDescription</key> <string>App需要您的同意,才能访问媒体资料库</string> ...