【POJ1733】【带标记并查集】Parity game
Description
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
Output
Sample Input
10
5
1 2 even
3 4 odd
5 6 even
1 6 even
7 10 odd
Sample Output
3
Source
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <utility>
#include <iomanip>
#include <string>
#include <cmath>
#include <queue>
#include <map> const int MAXN = + ;
const int MAX = + ;
using namespace std;
struct DATA{
int num, order;
bool operator < (DATA b)const{
return num < b.num;
}
}data[MAXN * ];//data记录的是出现过的数字
struct DATA2{
int a, b, type;//表示sum[b] - sum[a - 1]的是否是同一类型
}data2[MAXN];//直接的输入数据
int parent[MAXN * ];
int val[MAXN * ]; int n, q, cnt; void init(){
scanf("%d%d", &n, &q);
for (int i = ; i <= q; i++){
char str[];
scanf("%d%d", &data2[i].a, &data2[i].b);
data[i * - ].num = data2[i].a; data[i * - ].order = i * - ;
data[i * ].num = data2[i].b; data[i * ].order = i * ;
scanf("%s", str);
//odd是奇数,even是偶数
if (str[] == 'o') data2[i].type = ;
else if (str[] == 'e') data2[i].type = ;
}
//准备离散化
sort(data + , data + + * q);
cnt = ;//记录数字的
data[].num = -;
for (int i = ; i <= * q; i++){
if (data[i].num != data[i - ].num) ++cnt;
if (data[i].order % == ) data2[((data[i].order - ) / ) + ].a = cnt;
else data2[((data[i].order - ) / ) + ].b = cnt;
}
/*for (int i = 1;i <= q; i++){
printf("%d %d %d\n", data2[i].a, data2[i].b, data2[i].type);
}*/
}
int find(int x){
int tmp = , f = x;
//注意要加上路径压缩
while (x != parent[x]){
tmp += val[x];
x = parent[x];
}
parent[f] = x;
val[f] = tmp % ;
return x;
} void work(){
//val[i] = 1则与父亲的奇偶性不同,0则相同
for (int i = ; i <= cnt; i++){
parent[i] = i;
val[i] = ;
}
for (int i = ; i <= q; i++){
int x = data2[i].a, y = data2[i].b ,t = data2[i].type;
y++;
if (i == )
printf("");
int xx = find(x), yy = find(y), flag = ;
if (xx == yy){//同根
if (t == ){//需要相同
if (val[x] != val[y]) flag = ;
//相同则躲过一劫
}else{//需要不同
if (val[x] == val[y]) flag = ;
}
}else{//不管怎么样都要合并
if (t == ){
parent[xx] = y;
val[xx] = (-val[x] + ) % ;
}else{
parent[xx] = y;
val[xx] = (-val[x] + ) % ;
}
}
if (flag == ) {printf("%d\n", i - );return;}
}
printf("%d\n", q);
} int main(){
int T;
#ifdef LOCAL
freopen("data.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
init();
work();
return ;
}
。
【POJ1733】【带标记并查集】Parity game的更多相关文章
- poj1733(种类并查集+离散化)
题目链接: http://poj.org/problem?id=1733 题意: 输入n表示有一个长度为n的0,1字符串, m表示接下来有m行输入, 接下来的m行输入中x, y, even表示第x到第 ...
- 【POJ1417】【带标记并查集+DP】True Liars
Description After having drifted about in a small boat for a couple of days, Akira Crusoe Maeda was ...
- poj1733(并查集+离散化)
题目大意:有一个长度为n的0,1字符串, 给m条信息,每条信息表示第x到第y个字符中间1的个数为偶数个或奇数个, 若这些信息中第k+1是第一次与前面的话矛盾, 输出k; 思路:x, y之间1的个数为偶 ...
- POJ1733 Parity game 【扩展域并查集】*
POJ1733 Parity game Description Now and then you play the following game with your friend. Your frie ...
- POJ1733 Parity game 【带权并查集】*
POJ1733 Parity game Description Now and then you play the following game with your friend. Your frie ...
- POJ1733:Parity Game(离散化+带权并查集)
Parity Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12853 Accepted: 4957 题目链接 ...
- POJ1733 Parity game —— 种类并查集
题目链接:http://poj.org/problem?id=1733 Parity game Time Limit: 1000MS Memory Limit: 65536K Total Subm ...
- 【poj1733】Parity game--边带权并查集
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15776 Accepted: 5964 Description Now ...
- Poj1733 Parity Game(带权并查集)
题面 Poj 题解 反正只要你判断是否满足区间的奇偶性,假设每一位要么是\(1\)要么是\(0\)好了. 假设有\(S\)的前缀和为\(sum[]\),则有: 若\(S[l...r]\)中有奇数个\( ...
随机推荐
- MessageFormat.format处理单引号和大括号
在MessageFormat.format方法中组装jason数据字符串:{code:"w1",des:"w2"},起止分别有左大括号和右大括号.方法是将单引号 ...
- Linux的IP设置参考
位置:etc/network/interfaces 内容: 第一段是网口1自动从DHCP处获得IP 第二段是网口2静态分配IP 如果是IPv6,请把 iface eth0 inet dhcp(stat ...
- [Java] Java IO 概况
Java IO 是 Java 的一套 API, 用于读入和写出数据(输入和输出).Java IO API 位于 java.io package.实际上 java.io package 没有解决所有的输 ...
- IDEA新建SpringMVC项目报错解决办法
网页运行的错误: HTTP Status 500 - Handler processing failed; nested exception is java.lang.NoClassDefFoundE ...
- js实现table中前端搜索(模糊查询)
项目中用到js前端搜索功能,根据 姓名或姓名 进行 搜索,实现方法如下,遍历table所有行中的某列,符合条件则置tr为display:'',不满足条件置tr为display:none. 代码如下: ...
- SQL server 创建表,索引,主键,外键
if object_id('student', 'U') is not null drop table student go create table student( sno varchar(20) ...
- DevExpress 控件 GridControl常见用法
刚接触DevExpress第三方控件,把GridControl的常见用法整理一下,以供参考: 说明: gcTest GridControl gvText GridView //隐藏最上面的G ...
- android 37 线程通信Looper
安卓程序的主线程也叫UI线程. 工作线程和主线程的差别:安卓主线程已经调用了Looper.prepare()方法了,已经有一个MessageQueue对象了,所以才可以在工作线程用Handler发消息 ...
- 一次优化web项目的经历记录(三)
一次优化web项目的经历记录 这段时间以来的总结与反思 前言:最近很长一段时间没有更新博客了,忙于一堆子项目的开发,严重拖慢了学习与思考的进程. 开水倒满了需要提早放下杯子,晚了就会烫手,这段时间以来 ...
- 调试exynos4412—ARM嵌入式Linux—LEDS/GPIO驱动之二
/** ****************************************************************************** * @author 暴走的小 ...