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

题意:有一个长度为n的01串,有m句话,每句话说明x到y之间是有奇数(odd)个1还是偶数(even)个1;求的是前几句话是真的;如果全部为真输出m;

每输入一句话时,可以判断是否和之前的话相冲突,用r[i]表示i到i的跟节点之间1的个数是偶数还是奇数,1表示奇数,0代表偶数;

本题的n是10亿,所以不能定义一个10亿的数组;用map<int,int>定义就行

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<map>
#define N 105
#define INF 0xfffffff
using namespace std; //int f[N], r[N];//r[i]代表i到i的跟节点之间的关系;
map<int,int> f;
map<int,int> r; int Find(int x)
{
if(!f[x])
return x;
int k = f[x];
f[x] = Find(f[x]);
r[x] = (r[x]+r[k])%;
return f[x];
}
int main()
{
int px, py, flag, ans, i, n, m, x, y, d;
char str[];
while(scanf("%d", &n)!=EOF)
{
flag = ;
f.clear();
r.clear();
scanf("%d", &m);
for(i = ; i <= m; i ++)
{
scanf("%d%d%s", &x, &y, str);
x--;
if(str[] == 'e')//偶数是0;奇数是1;
d = ;
else
d = ;
px = Find(x);
py = Find(y);
if(px != py)
{
f[px] = py;
r[px] = (r[y] + d - r[x] +)%;//画向量图找关系;
}
else if(flag== && ( + r[x]-r[y])% != d)
{
flag = ;
ans = i;
}
}
if(flag==)//如果全部都正确的话输出m;-_-在这wa了一次;
ans=m+;
printf("%d\n", ans-);
}
return ;
}
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<map>
#define N 105
#define INF 0xfffffff
using namespace std; //int f[N], r[N];//r[i]代表i到i的跟节点之间的关系;
map<int,int> f;
map<int,int> r; int Find(int x)
{
if(!f[x])
return x;
int k = f[x];
f[x] = Find(f[x]);
r[x] = (r[x]+r[k])%;
return f[x];
}
int main()
{
int px, py, flag, ans, i, n, m, x, y, d;
char str[];
while(scanf("%d", &n)!=EOF)
{
flag = ;
f.clear();
r.clear();
scanf("%d", &m);
for(i = ; i <= m; i ++)
{
scanf("%d%d%s", &x, &y, str);
x--;
if(str[] == 'e')//偶数是0;奇数是1;
d = ;
else
d = ;
px = Find(x);
py = Find(y);
if(px < py)
{
f[px] = py;
r[px] = (r[y] + d - r[x]+)%;//画向量图找关系;
}
else if(px > py)
{
f[py] = px;
r[py] = (r[x] - r[y] - d + )%;
} else if(px==py &&flag== && (r[y] - r[x] + )% != d)
{
flag = ;
ans = i;
}
}
if(flag==)//如果全部都正确的话输出m;-_-在这wa了一次;
ans=m+;
printf("%d\n", ans-);
}
return ;
}

Parity game---poj1733的更多相关文章

  1. POJ1733 Parity game 【扩展域并查集】*

    POJ1733 Parity game Description Now and then you play the following game with your friend. Your frie ...

  2. POJ1733 Parity game 【带权并查集】*

    POJ1733 Parity game Description Now and then you play the following game with your friend. Your frie ...

  3. 【POJ1733】Parity game

    [POJ1733]Parity game 题面 vjudge 题解 比较简单的分类并查集 将一个查询操作看作前缀和\(s_r-s_{l-1}\)的奇偶性 将每个点拆成一奇一偶然后分别连边即可 如果一个 ...

  4. 【poj1733】 Parity game

    http://poj.org/problem?id=1733 (题目链接) 题意 一个由0,1组成的序列,每次给出一段区间的奇偶,问哪一条信息不合法. Solution 并查集. 题目中序列的长度有很 ...

  5. 【POJ1733】【带标记并查集】Parity game

    Description Now and then you play the following game with your friend. Your friend writes down a seq ...

  6. POJ1733 Parity game

    题意 Language:Default Parity game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13833 Acc ...

  7. poj1733 Parity Game(扩展域并查集)

    描述 Now and then you play the following game with your friend. Your friend writes down a sequence con ...

  8. POJ1733:Parity Game(离散化+带权并查集)

    Parity Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12853   Accepted: 4957 题目链接 ...

  9. POJ1733 Parity game —— 种类并查集

    题目链接:http://poj.org/problem?id=1733 Parity game Time Limit: 1000MS   Memory Limit: 65536K Total Subm ...

  10. 【poj1733】Parity game--边带权并查集

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15776   Accepted: 5964 Description Now ...

随机推荐

  1. Window日志分析

    0X00 简介 0x01 基本设置 A.Windows审核策略设置 前提:开启审核策略,若日后系统出现故障.安全事故则可以查看系统的日志文件,排除故障,追查入侵者的信息等. 打开设置窗口 Window ...

  2. Explaining Delegates in C# - Part 2 (Events 1)

    In my previous post, I spoke about a few very basic and simple reasons of using delegates - primaril ...

  3. Redis 操作集合数据

    Redis 操作集合数据: > sadd set1 Tom // sadd 用于往集合中添加元素 (integer) > sadd set1 John (integer) > sad ...

  4. 原创:超简单!windows配置NDK开发环境使用JNI

    前段时间看android版的opencv的配置教程时,看到了它的NDK配置方法,感觉简单又不会出错!!! 1.下载NDK,设置NDK路径: 在windows的系统环境变量中添加NDK的路径,环境变量名 ...

  5. JS 省市县三级联动

    $(".area").change(function(ent,arg){ var $this = $(this), level = $(this).attr("id&qu ...

  6. 微信JSSDK支付接口-安卓机无法正常调起接口

    今天碰到个问题,是项目调起微信支付接口,苹果机都没有什么问题,但是安卓机的情况就是支付接口出现一下就消失了 试了将error反馈信息弹出来,也就是显示choosewxpay fail 找了半天不知道什 ...

  7. 【Mac】WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!

    使用Mac 自带终端 链接服务器时候,报错如下 处理办法: 第一种:  直接删除:  /users/username/.ssh/known_hosts  文件 第二种: ssh-keygen -R   ...

  8. RH318之域控服务器

    Windows2012域控服务器 一.安装域服务及DNS 1.配置静态IP 2.点击左下角 3.进入--->服务器管理器 4.点击角色和功能 勾选Active Directory域服务与DNS服 ...

  9. python tkinter Listbox用法

    python tkinter组件的Listbox的用法,见下面代码的演示: from tkinter import * root=Tk() v=StringVar() #Listbox与变量绑定' l ...

  10. GTID与MHA

    MHA 基于binlog文件位置的复制 * Phase 3: Master Recovery Phase.. * Phase 3.1: Getting Latest Slaves Phase.. La ...