如果sm[j]和sm[i]奇偶性相同,那么(i+1,j)个数为偶数
如果奇偶性相同看成是朋友,不同的看成是敌人,那么就跟bzoj1370的做法差不多了。
如果奇偶性相同,就将x和y合并,x+n,y+n合并
如果奇偶性不同,就将x和y+n合并,y和x+n合并

#include<cstdio>
#include<cstring>
#include<cctype>
#include<algorithm>
using namespace std;
#define rep(i,s,t) for(int i=s;i<=t;i++)
#define dwn(i,s,t) for(int i=s;i>=t;i--)
#define clr(x,c) memset(x,c,sizeof(x))
int read(){
int x=0;char c=getchar();
while(!isdigit(c)) c=getchar();
while(isdigit(c)) x=x*10+c-'0',c=getchar();
return x;
}
const int nmax=2e5+5;
int fa[nmax];
int find(int x){
return fa[x]==x?x:fa[x]=find(fa[x]);
}
int main(){
int n=read(),m=read(),u,v,ta,tb,tc,td;char ch[10];
rep(i,1,n*2+2) fa[i]=i;
int ans=0;
rep(i,1,m){
u=read()-1,v=read();scanf("%s",ch);
if(ans) continue;
if(ch[0]=='e'){
ta=find(u),tb=find(v),tc=find(u+n+1),td=find(v+n+1);
if(ta==td||tb==tc) ans=i;
else fa[ta]=tb,fa[tc]=td;
}else {
ta=find(u),tb=find(v),tc=find(u+n+1),td=find(v+n+1);
if(ta==tb||tc==td) ans=i;
else fa[ta]=td,fa[tb]=tc;
}
}
if(ans) printf("%d\n",ans);else puts("-1");
return 0;
}

  

题目来源: Ural
基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题
 收藏
 关注
你的朋友写下一串包含1和0的串让你猜,你可以从中选择一个连续的子串(例如其中的第3到第5个数字)问他,该子串中包含了奇数个还是偶数个1,他会回答你的问题,然后你可以继续提问......你怀疑朋友的答案可能有错,或说同他之前的答案相互矛盾,例如:1 - 2 奇数,3 - 4 奇数,那么可以确定1 - 4 一定是偶数,如果你的朋友回答是奇数,就产生了矛盾。给出所有你朋友的答案,请你找出第一个出现矛盾的答案。

 
Input
第1行:2个数N, Q,N为串的长度,Q为询问的数量。(2 <= N <= 100000, 2 <= Q <= 50000)
第2 - Q + 1行:每行包括两个数以及一个字符串来描述朋友的回答,2个数中间用空格分隔,分别表示区间的起点和终点,后面的字符为"even"或"odd",表示朋友的答案。
Output
输出1个数,表示朋友的答案中第一个错误答案的位置,如果所有答案均不矛盾,则输出-1。
Input示例
10 5
1 2 even
3 4 odd
5 6 even
1 6 even
7 10 odd
Output示例
4

51nod1204 Parity的更多相关文章

  1. Codeforces 549C. The Game Of Parity[博弈论]

    C. The Game Of Parity time limit per test 1 second memory limit per test 256 megabytes input standar ...

  2. Codeforces Round #180 (Div. 2) C. Parity Game 数学

    C. Parity Game 题目连接: http://www.codeforces.com/contest/298/problem/C Description You are fishing wit ...

  3. POJ 1733 Parity game (并查集)

    Parity game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6816   Accepted: 2636 Descr ...

  4. 1003. Parity(并查集)

    1003 看篇国家论文 <从<parity>的解法谈程序优化> 对于区间i,j 如果用sum[i],sum[j]来表示到i的1的个数的奇偶性 那么仔细想下 sum[i-1] 若 ...

  5. poj 1733 Parity game

    Parity game 题意:一个长度为N(N < 1e9)内的01串,之后有K(K <= 5000)组叙述,表示区间[l,r]之间1的个数为odd还是even:问在第一个叙述矛盾前说了几 ...

  6. UVA 11464 Even Parity(部分枚举 递推)

    Even Parity We have a grid of size N x N. Each cell of the grid initially contains a zero(0) or a on ...

  7. HDU-2700 Parity

    http://acm.hdu.edu.cn/showproblem.php?pid=2700 题目意思很重要:  //e:是要使字符串中1的个数变成偶数.o:是要使字符串中1的个数变成奇数 Parit ...

  8. Codeforces 549C The Game Of Parity(博弈)

    The Game Of Parity Solution: 这个题只需要分类讨论就可以解决. 先分别统计奇数和偶数的个数. 然后判断谁走最后一步,如果走最后一步时候同时有偶数和奇数,那么走最后一步的赢. ...

  9. HDOJ/HDU 2700 Parity(奇偶判断~)

    Problem Description A bit string has odd parity if the number of 1's is odd. A bit string has even p ...

随机推荐

  1. laravel笔记

    向视图中传递变量 使用with()方法 return view('articles.lists')->with('title',$title); 直接给view()传参数 return view ...

  2. sream bytes[] img相互转换

    /// <summary> /// 将 Stream 转成 byte[] /// </summary> /// <param name="stream" ...

  3. Android调用天气预报的WebService简单例子

    下面例子改自网上例子:http://express.ruanko.com/ruanko-express_34/technologyexchange5.html 不过网上这个例子有些没有说明,有些情况不 ...

  4. 数据库链接 mysql,sqlserver

    1.生成对象工厂 /// <summary> /// 生成对象工厂 /// </summary> public class DBFactory { /// <summar ...

  5. java 继承类与接口问题

    java 先extends 继承类,再implements 继承接口 public class DataBase extends ClassBase implements Ijiekou { }// ...

  6. MYSQL 遭遇 THE TOTAL NUMBER OF LOCKS EXCEEDS THE LOCK TABLE SIZE

    今天进行MySql 一个表数据的清理,经过漫长等待后出现 The total number of locks exceeds the lock table size 提示.以为是table_cache ...

  7. 无限互联IOS电影项目视频笔记

    下面是该iOS项目视频教程的内容大纲: 观看指南 (1)项目为第一阶段内容 (2)需要熟练掌握OC语言 (3)UI部分需要学习到第十节课 (4)项目适合刚入门的iOS开发者 1.第一天 (1)iOS ...

  8. Linux信号处理2

    引言 先看以下两个信号量: 13)SIGPIPE     当管道读端关闭,再往管道写东西,会发出SIGPIPE信号 17)SIGCHLD   子进程退出会向父进程发出SIGCHLD信号,系统默认处理是 ...

  9. linux下top命令查看cpu占用情况

    可以通过 top 命令来查看 CPU 使用状况.运行 top 命令后,CPU 使用状态会以全屏的方式显示,并且会处在对话的模式 -- 用基于 top 的命令,可以控制显示方式等等.退出 top 的命令 ...

  10. Linux静态库和动态库

    Linux 工具 ❑ GCC: The GNU Compiler Collection, containing the GNU C compiler❑ G++: A C++ compiler, inc ...