并查集——奇偶性(Parity)
题目描述
b even/odd。表示第a位到第b位元素之间的元素总和是偶数/奇数。
输入
输出
输出第一条错误信息的位置-1.
如果没有错误信息,则输出n
样例输入
10
5
1 2 even
3 4 odd
5 6 even
1 6 even
7 10 odd
样例输出
3
- #include<cstdio>
- #include<cstring>
- #include<cmath>
- #include<algorithm>
- using namespace std;
- int abs(int x){return x>=0?x:-x;}
- int a[10001];
- int f[10001],s[10001];
- int m,n,k;
- int find(int x)//找父亲与离父亲的距离,顺便找沿路所有元素与离父亲的距离(看不懂?自己慢慢想)
- {
- if(f[x]==0)return x;
- int xx=find(f[x]);
- s[x]+=s[f[x]];
- s[x]%=2;
- return f[x]=xx;
- }
- int main()
- {
- int i,j;
- scanf("%d%d",&m,&n);
- for(i=1;i<=n;i++)
- {
- char c[11];
- int x,y,r1,r2;
- scanf("%d%d%s",&x,&y,c);
- if(x>m||y>m){printf("%d",i-1);return 0;}
- if(x>y)swap(x,y);
- x--;//半开半闭区间,小的元素减减
- bool p=0,q=0;
- for(j=1;j<=k;j++)//找数组中是否有x和y
- <span style="white-space:pre"> </span>{
- if(a[j]==x&&!p)x=j,p=1;
- if(a[j]==y&&!q)y=j,q=1;
- }
- if(!p)a[++k]=x,x=k;//没有就将其加进去
- if(!q)
- {
- if(x!=y)a[++k]=y,y=k;//这里要注意
- else y=x;
- }
- r1=find(x),r2=find(y);
- if(r1!=r2)
- {
- if(r1>r2)swap(r1,r2);
- f[r2]=r1;
- if(c[0]=='o')s[r2]=abs(s[x]+1-s[y])%2;
- else s[r2]=abs(s[x]-s[y])%2;
- }
- else//验证
- {
- if(c[0]=='o'&&abs(s[x]-s[y])%2!=1){printf("%d",i-1);return 0;}
- if(c[0]=='e'&&abs(s[x]-s[y])%2){printf("%d",i-1);return 0;}
- }
- }
- printf("%d",n);
- }
并查集——奇偶性(Parity)的更多相关文章
- U137971 公司搬迁 - 并查集 奇偶性
题目描述 因为人员规模扩大,T公司准备搬到新的写字楼去,写字楼分为A座和B座,n名不同工号的员工x(p1,p2,p3...pn) 按照下面两个规则确定在A座或者B座进行办公:(1)如果工号为x的员工在 ...
- 【POJ1733】【带标记并查集】Parity game
Description Now and then you play the following game with your friend. Your friend writes down a seq ...
- 【转】并查集&MST题集
转自:http://blog.csdn.net/shahdza/article/details/7779230 [HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基 ...
- 51nod 1204 Parity(并查集应用)
1204 Parity 题目来源: Ural 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 你的朋友写下一串包含1和0的串让你猜,你可以从中选择一个连续的子串 ...
- URAL - 1003:Parity (带权并查集&2-sat)
Now and then you play the following game with your friend. Your friend writes down a sequence consis ...
- POJ1733 Parity game 【带权并查集】*
POJ1733 Parity game Description Now and then you play the following game with your friend. Your frie ...
- 【poj1733】Parity game--边带权并查集
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15776 Accepted: 5964 Description Now ...
- POJ 1733 Parity game(离散化+带权并查集)
离散化+带权并查集 题意:长度为n的0和1组成的字符串,然后问第L和R位置之间有奇数个1还是偶数个1. 根据这些回答, 判断第几个是错误(和之前有矛盾)的. 思路:此题同HDU 3038 差不多,询问 ...
- POJ - 1733 Parity game 种类并查集+离散化
思路:d(i, j)表示区间(i, j]的1的个数的奇偶性.输入最多共有5000*2个点,需要离散化处理一下.剩下的就是并查集判冲突. AC代码 #include <cstdio> #in ...
随机推荐
- Checkpoint checkup中文报告模板使用
步骤: Step1:下载中文版语言包和字体 https://supportcenter.checkpoint.com/supportcenter/portal?action=portlets.DCFi ...
- CP防火墙备份与还原
Step1:进入专家模式 ====================================================== 如果没有设置专家模式的密码,执行下面命令进行设置: BJ-OFF ...
- Xgboost参数调节
转自:https://segmentfault.com/a/1190000014040317 整体: # 1.调试n_estimators cv_params = {'n_estimators': [ ...
- 第二阶段:流程图:8.axure绘制简单业务流程图
元件库选择流程图 流程图示例 一个简单的注册练习 线上可以写字 作业
- 假期汇总表handler中涉及的基础知识
1,stringBuffer 的常用方法,append () 方法, public StringBuffer append(boolean b) 追加内容到当前StringBuffer对象的末尾 ,我 ...
- 【他山之石】jenkins忘记初始化密码解决办法
没有太好的方式,网上有的是这样子的,找到 /var/lib/jenkins/users/username/config.xml, 修改为一个已知的 hash 值 #jbcrypt:$2a$10$Dda ...
- 洛谷P2657 [SCOI2009]windy数 题解 数位DP
题目链接:https://www.luogu.com.cn/problem/P2657 题目大意:找区间 \([A,B]\) 范围内 不含前导零 且 相邻两个数字之差至少为2 的正整数的个数. 题目分 ...
- 「Luogu P2508」[HAOI2008]圆上的整点 解题报告
题面 给定圆的半径,求圆上整点数 这是一道很Nice的数学题!超爱!好吧,由于这道题,我去Study了一下复数(complex number)复杂的数 真棒!!! 有兴趣的戳这里!!!\(\huge ...
- shiro采坑指南—基础概念与实战
说明 代码及部分相关资料根据慕课网Mark老师的视频进行整理. 其他资料: shiro官网 基础概念 Authenticate/Authentication(认证) 认证是指检查用户身份合 ...
- schedule of 2016-10-17~2016-10-23(Monday~Sunday)——1st semester of 2nd Grade
most important things to do 1.joint phd preparations 2.journal paper to write 3.solid fundamental kn ...