poj 1733 Parity game
来源:互联网 发布:缪雪峰python基础教程 编辑:程序博客网 时间:2024/05/21 11:34
离散化 + 区间上的种类并查集
人为的把区间分为两类,0类(偶数个1),1类(偶数个0)。
假若有两个区间[a,b],[c,d]。其中c = b + 1,换句话说两个闭区间相邻,当[c,d]为1类时,则[a,b]一定和[a,d]不同类,(不妨假设[a,b]为0类,因为[c,d]为1类,所以[a,d]为1类,偶加奇为奇,不妨假设[a,b]为1类,因为[c,d]为1类,所以[a,d]为0类,奇加奇为偶)。然后用右端点代表当前区间,左端-1代表前一个相邻区间(左相邻),然后右端点合并到,左端点-1,以[a,b]为例,把b合并到a-1,把当[a,b]为0类,把rank[b] = 0,否则rank[b] = 1。想想看这里的合并是有含义的每棵树的根结点a到任意一个结点b,代表着这样一个区间(a,b]。
代码:
#include <cstdio>#include <iostream>#include <cstring>#include <algorithm>#include <cstdlib>#include <math.h>using namespace std;const int MAXN = 100005;int pre[MAXN];int ranks[MAXN];int a[MAXN];int cnt = 0;struct Q{ int x; int y; char s[10];}q[MAXN];int binary(int r){ int x = 0,y = cnt; int m = x + (y-x)/2; while(r != a[m]){ if(a[m]>r)y = m; else x = m+1; m = x + (y-x)/2; } return m;}int find_DS(int r){ if(r == pre[r])return r; int fa = pre[r]; pre[r] = find_DS(pre[r]); ranks[r] = (ranks[r] + ranks[fa])%2; return pre[r];}bool merge_DS(int a,int b,int c){ int fx = find_DS(a),fy = find_DS(b); if(fx == fy) if((ranks[b]-ranks[a]+2)%2 == c)return 1; else return 0; pre[fy] = fx; ranks[fy] = (ranks[a] - ranks[b] + c)%2; return 1;}int main(){ //freopen("in.txt","r",stdin); int n,m,a1,b1,c,num; scanf("%d%d",&n,&m); for(int i = 0;i<m;i++){ scanf("%d%d%s",&q[i].x,&q[i].y,q[i].s); q[i].x--; a[cnt++] = q[i].x;a[cnt++] = q[i].y; } sort(a,a+cnt); cnt = unique(a,a+cnt) - a; for(int i = 0;i<cnt;i++){ pre[i] = i; ranks[i] = 0; } num = 0; for(int i = 0;i<m;i++){ a1 = binary(q[i].x),b1 = binary(q[i].y); if(q[i].s[0] == 'e')c = 0; else c = 1; if(!merge_DS(a1,b1,c))break; num++; } printf("%d\n",num);return 0;}
0 0
- POJ 1733 Parity Game
- poj 1733 parity game
- POJ 1733 Parity game
- poj 1733 Parity game
- poj 1733 Parity game
- POJ-1733-Parity game
- poj 1733 Parity game
- POJ 1733 Parity game
- poj 1733 Parity game
- POJ 1733 Parity game
- POJ 1733 Parity game
- POJ-1733Parity game
- POJ 1733Parity game
- POJ 1733 Parity game 笔记
- POJ 1733 Parity game(并查集)
- POJ 1733 Parity game (并查集)
- 【POJ】1733 Parity game 并查集
- poj-1733 Parity game 并查集
- 该怎么样用C++来实现类Unity3d的AddComponent
- vc界面库技术比较
- http://blog.csdn.net/zzp_403184692/article/details/8184751
- 从源码中深入学习Handler,HandlerThread,MessageQueue,Looper。
- Linux C/C++ 模板:模板术语 模板参数/模板实参/模板名字/模板ID/特化/实例化
- poj 1733 Parity game
- 流水线技术理解
- MySql下视图的创建
- 第五章 继承
- 字符串匹配, Zenefits的面试题
- JVM-4-JVM类加载器总结及自定义类加载器
- C#实现的事务
- Hibernate查询,返回new对象(注意这个新定义的类要有构造函数),使用sql带条件分页查询并且把结果显示到一个对象的集里面的解决方案
- 二叉查找树