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. [python学习笔记]Day3

    函数 如: def is_leapyear(year): if (year%4 == 0 and year%100 != 0) or (year%400 == 0): return True else ...

  2. JS中标准对象

    JS中标准对象1:不要使用new Number().new Boolean().new String()创建包装对象:2:用parseInt()或parseFloat()来转换任意类型到number: ...

  3. [js开源组件开发]js多选日期控件

    js多选日期控件 详情请见:http://www.lovewebgames.com/jsmodule/calendar.html 它的github地址:https://github.com/tianx ...

  4. XML的介绍

    XML是指可扩展标记语言(eXtensible Markup Language),它是一种标记语言,类似于HTML.它被设计的宗旨是传输数据,而非显示数据.   XML标签没有被预定义,需要开发者自定 ...

  5. SAP ALV中同一列的不同行显示不同的小数位,并能够总计,小计

    物料数量字段,根据物料类型的不同,来显示不同的小数位:要求有点苛刻: 首先,要能够总计和小计的话,这一列的字段类型必须是数值类型. 这样的话,就不能通过截取的方式改变不同行的小数位. 以下是两种思路: ...

  6. 在sap系统设置纸张打印格式(针式打印机)

    在sap做一个打印报表,要先设置一个纸张打印格式,下面以工厂中常用来打印的针孔纸为例,在sap设置该纸张的打印格式,以用于报表: 1.运行事务代码SPAD:选择工具栏上的[完全管理]按钮——>选 ...

  7. Engine中如何更改lyr文件数据源的相对路径

    以下是使用relativebase存储layerfile的代码,如果需要修改数据源只要修改ILayer即可: string folderpath = @E:相对路径; string layerfile ...

  8. 关于ApplicationPoolIdentity

    一直以来IIS中的网站默认都是以network service在运行,但是从IIS7开始,默认会以ApplicationPoolIdentity运行. 这个账户比较特殊,是一种虚拟帐户,你无法在计算机 ...

  9. Arcengine实现创建网络数据集札记(三)

    后记 下面给出项目中用到的自定义的封装类. AE许可初始化封装类: public class AELicenseChecker { private static volatile AELicenseC ...

  10. Sharepoint学习笔记—习题系列--70-573习题解析 -(Q45-Q47)

    Question45You create and deploy a custom Web Part.You add the Web Part to a page and receive a run-t ...