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. PHP学习笔记:对命名空间(namespace)学习资料的翻译

    Name collisions means: you create a function named db_connect, and somebody elses code that you use ...

  2. C#读写ini文件操作

    ini文件,是windows操作系统下的配置文件,ini文件是一种按照特点方式排列的文本文件,它的构成分为三部分,结构如下: [Section1] key 1 = value2 key 1 = val ...

  3. JavaScript的一些小技巧(转)

    本文是一篇翻译文章,原文信息如下: 原文:45 Useful JavaScript Tips, Tricks and Best Practices 作者:Saad Mousliki JavaScrip ...

  4. For循环语句的使用

    一.For循环语句 说明:For循环用于循环次数已经确定的情况下.  格式:for(循环变量赋初值; 循环条件; 循环变量增值)       {             ·····语句  } 举例:求 ...

  5. 在 SharePoint Server 2013 中配置建议和使用率事件类型

    http://technet.microsoft.com/zh-cn/library/jj715889.aspx 适用于: SharePoint Server 2013 利用使用事件,您可以跟踪用户与 ...

  6. Oracle LPAD/RPAD函数在处理中文时的注意事项

    首先看下Oracle官方对函数的定义: The RPAD function returns an expression, right-padded to a specified length with ...

  7. 在cmd中获取ip地址和主机名

    将下面的文件放到一个bat文件当中,以管理员身份运行. @echo off &setlocal enabledelayedexpansion Rem '/*========获取本机的IP地址( ...

  8. 【读书笔记】iOS-读取文本文件

    一,文本文件的内容. 二,工程目录 三,ViewController.m - (void)viewDidLoad { [super viewDidLoad]; // Do any additional ...

  9. VIP - virtual IP address

    virtual IP address (虚拟 IP 地址)1.是集群的ip地址,一个vip对应多个机器2.与群集关联的唯一 IP 地址 see wiki: A virtual IP address ( ...

  10. 采用 PAT工具及CSP语言,对一个问题进行自动机 建模

    pat是新加坡国立开发的工具,需要的去官网下http://www.comp.nus.edu.sg/~pat/ ,学了一天,是个不错的自动机验证工具,感觉还不错啊. 验证一个数是否为斐波那契数且为质数 ...