1.POJ 1733

Parity game
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 5744   Accepted: 2233

Description

Now and then you play the following game with your friend. Your friend writes down a sequence consisting of zeroes and ones. You choose a continuous subsequence (for example the subsequence from the third to the fifth digit inclusively) and ask him, whether this subsequence contains even or odd number of ones. Your friend answers your question and you can ask him about another subsequence and so on. Your task is to guess the entire sequence of numbers. 
You suspect some of your friend's answers may not be correct and you want to convict him of falsehood. Thus you have decided to write a program to help you in this matter. The program will receive a series of your questions together with the answers you have received from your friend. The aim of this program is to find the first answer which is provably wrong, i.e. that there exists a sequence satisfying answers to all the previous questions, but no such sequence satisfies this answer.

Input

The first line of input contains one number, which is the length of the sequence of zeroes and ones. This length is less or equal to 1000000000. In the second line, there is one positive integer which is the number of questions asked and answers to them. The number of questions and answers is less or equal to 5000. The remaining lines specify questions and answers. Each line contains one question and the answer to this question: two integers (the position of the first and last digit in the chosen subsequence) and one word which is either `even' or `odd' (the answer, i.e. the parity of the number of ones in the chosen subsequence, where `even' means an even number of ones and `odd' means an odd number).

Output

There is only one line in output containing one integer X. Number X says that there exists a sequence of zeroes and ones satisfying first X parity conditions, but there exists none satisfying X+1 conditions. If there exists a sequence of zeroes and ones satisfying all the given conditions, then number X should be the number of all the questions asked.

Sample Input

10
5
1 2 even
3 4 odd
5 6 even
1 6 even
7 10 odd

Sample Output

3

1.题目数据量较大,要用map来做哈希。

2.num[x]表示(fa[x],x]区间1的个数的奇偶性,0代表偶数,1代表奇数。以树根作为参照,每个点的num值即为树根到该点之间1的个数的奇偶性。

输入a,b

首先判断a,b是否在同一集合,如果在,则以其共同树根作为参照点,看异或值是否为0或1(0代表偶数,1代表奇数)。

如果不在,则合并两个集合,画一个图就知道了,图类似:http://www.cnblogs.com/whatbeg/p/3503585.html 中的图类似,假设fa[x] = y;

则 num[x] = num[a] ^ num[b] ^ k;   (k = 0/1)

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
using namespace std;
#define N 10100 int fa[N],num[N];
map<int,int> mp; void makeset(int n)
{
for(int i=;i<=;i++)
{
fa[i] = i;
num[i] = ;
}
} int findset(int x)
{
if(x != fa[x])
{
int tmp = fa[x];
fa[x] = findset(fa[x]);
num[x] = num[x]^num[tmp];
}
return fa[x];
} int unionset(int a,int b,char oe)
{
int x = findset(a);
int y = findset(b);
int k = (oe == 'o'? :);
if(x == y)
{
if(num[a]^num[b] == k)
return ;
else
return ;
}
else
{
fa[x] = y;
num[x] = num[a]^num[b]^k;
return ;
}
} int main()
{
int n,m,i,k,a,b,cnt;
char ss[];
scanf("%d%d",&n,&m);
makeset(n);
k = ;
cnt = ;
for(i=;i<=m;i++)
{
scanf("%d%d %s",&a,&b,ss);
a = a-;
if(mp.find(a) == mp.end())
{
mp[a] = k++;
}
if(mp.find(b) == mp.end())
{
mp[b] = k++;
}
if(unionset(mp[a],mp[b],ss[]))
cnt++;
else
break;
}
printf("%d\n",cnt);
return ;
}

2.HDU 3038

题目大意:有n次询问,给出a到b区间的总和,问这n次给出的总和中有几次是和前面已近给出的是矛盾的,即命题为假的次数。

分析:与上面那题一样,用根节点作参照,每个点维护一个sum值,表示到根节点的距离,树根的距离为0。如果我们知道a到b之间的关系,a到c之间的关系,那么我们就可以知道a,b,c任意两个之间的关系。

输入a,b,判断是否属于同一集合,如果是,则判断 sum[b] - sum[a] 是否等于val。

如果不在同一集合,则合并,此时又画一个四点图可知, fa[y] = x; sum[y] = sum[b] - sum[a] + k; 不懂的话自己可以画一下。

然后就好搞了。

代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define N 200100 int fa[N],sum[N]; void makeset(int n)
{
for(int i=;i<=n;i++)
{
fa[i] = i;
sum[i] = ;
}
} int findset(int x)
{
if(x != fa[x])
{
int tmp = fa[x];
fa[x] = findset(fa[x]);
sum[x] = sum[x] + sum[tmp];
}
return fa[x];
} int unionset(int a,int b,int k)
{
int x = findset(a);
int y = findset(b);
if(x == y)
{
if(sum[b] - sum[a] == k)
return ;
else
return ;
}
else
{
fa[y] = x;
sum[y] = sum[a] - sum[b] + k;
return ;
}
} int main()
{
int n,m,i,a,b,cnt,val;
while(scanf("%d%d",&n,&m)!=EOF)
{
makeset(n);
cnt = ;
for(i=;i<=m;i++)
{
scanf("%d%d%d",&a,&b,&val);
a = a-;
if(unionset(a,b,val))
cnt++;
}
printf("%d\n",cnt);
}
return ;
}

类似区间计数的种类并查集两题--HDU 3038 & POJ 1733的更多相关文章

  1. poj1733(区间上的种类并查集)

    题目大意是:一个由0,1组成的数字串~~,现在你问一个人,第i位到第j位的1的个数为奇数还是偶数.一共会告诉你几组这样的数 要你判断前k组这个人回答的都是正确的,到第k+1组,这个人说的是错的,要你输 ...

  2. poj1182(种类并查集好题)

    不得不说,我得感谢@驱动幽灵百鬼夜行小肆,正是因为看明白了他给出的解析,我才完全弄懂种类并查集的,这里,我也不想去改其他的,就直接引用他的解题报告吧 转载:http://blog.csdn.net/c ...

  3. (并查集 建立关系)Parity game -- POJ -1733

    链接: http://poj.org/problem?id=1733 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82830#probl ...

  4. 【转】并查集&MST题集

    转自:http://blog.csdn.net/shahdza/article/details/7779230 [HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基 ...

  5. HDU3038【种类并查集】

    题意: 给出m组区间[a,b],以及其区间的和,问有矛盾的有几组: 思路: 种类并查集. 主要是几个关系:同类元素的关系,父亲与儿子的关系,不同类元素的关系: 我们可以类似看作一个前缀和,sum[x] ...

  6. NOI2001|POJ1182食物链[种类并查集 向量]

    食物链 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 65430   Accepted: 19283 Description ...

  7. hdu 3038 How Many Answers Are Wrong(种类并查集)2009 Multi-University Training Contest 13

    了解了种类并查集,同时还知道了一个小技巧,这道题就比较容易了. 其实这是我碰到的第一道种类并查集,实在不会,只好看着别人的代码写.最后半懂不懂的写完了.然后又和别人的代码进行比较,还是不懂,但还是交了 ...

  8. HDU - 3038 种类并查集

    思路:种类并查集的每个节点应该保存它的父节点以及他和父节点之间的关系.假设root表示根结点,sum[i-1]表示i到根结点的和,那么sum[j-1] - sum[i]可以得到区间[j, i]的和.那 ...

  9. 种类并查集(洛谷P2024食物链)

    题目描述 动物王国中有三类动物 A,B,C,这三类动物的食物链构成了有趣的环形.A 吃 B,B 吃 C,C 吃 A. 现有 N 个动物,以 1 - N 编号.每个动物都是 A,B,C 中的一种,但是我 ...

随机推荐

  1. git本地提交项目到你的github

    第一步:建立git仓库(若已经创建则跳过该步) cd到你的本地项目根目录下,执行git命令 git init 第二步:将项目的文件添加到仓库中 git add fileName 如果想添加本次仓库中的 ...

  2. ahjesus根据身份证号码获取相关信息(生日,省市县,性别)

    使用说明: //出自http://www.cnblogs.com/ahjesus 尊重作者辛苦劳动成果,转载请注明出处,谢谢! var idCard = new IdCard();var msg = ...

  3. MAC 隐藏文件的显示

    显示 defaults write com.apple.finder AppleShowAllFiles -bool true 隐藏 defaults write com.apple.finder A ...

  4. js argument实参集合与局部变量、参数关系

    形参 形式上传递的参数 function fn1(a,b,c) {//a,b,c就是形参 实参 实际传递的参数 fn1 (1,2,5);//1,2,5就是实参 argument 定义: 实参的集合 用 ...

  5. HTML 5 <mark> 标签

    一,定义和用法 <mark> 标签定义带有记号的文本.请在需要突出显示文本时使用 <m> 标签. 二,实例 突出显示部分文本: <!DOCTYPE HTML> &l ...

  6. 如何利用ArcGIS Engine接口实现打开Raster Catalog中的某一幅指定的影像?

    将IRasterCatalog转化为ITable,然后通过ITable.GetRow返回指定索引的IRow,将IRow转为IRasterCatalogItem,进而获取IRasterCatalogIt ...

  7. Sharepoint 2013 开启App和配置App

    在任何站点中,点Add App,然后点Sharepoint Store,如果没有Enable apps,打开app store的时候出出现错误: Sorry, apps are turned off. ...

  8. Android 计算布局背景的透明度

    1.#ff000000 此为16进制颜色代码,前2位ff为透明度,后6位为颜色值(000000为黑色,ffffff为白色,可以用ps等软件获取). 2.透明度分为256阶(0-255),计算机上用16 ...

  9. 安卓第十三天笔记-服务(Service)

    安卓第十三天笔记-服务(Service) Servcie服务 1.服务概念 服务 windows 服务没有界面,一直运行在后台, 运行在独立的一个进程里面 android 服务没有界面,一直运行在后台 ...

  10. 使用 eclipse+egit 将项目提交至 github ,本地的git仓库:eclipse工作项目目录

      新建github仓库   写一个github上仓库的名字,系统会自动检测重复性,无重复则可以提交                           大于号代表有需要提交的东西           ...