并查集——奇偶性(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 ...
随机推荐
- 土旦:移动端 Vue+Vant 的Uploader 实现 :上传、压缩、旋转图片
面向百度开发 html <van-uploader :after-read="onRead" accept="image/*"> <img s ...
- CodeForces - 1162E Thanos Nim (博弈论)
Alice and Bob are playing a game with nn piles of stones. It is guaranteed that nn is an even number ...
- [竞赛]Beat Matching(对拍)
对拍的基本理论这里恕我不一一叙述,不会的请转身到这里:http://blog.csdn.net/code12hour/article/details/51252457 分为以下几个部分: 1.暴力伪标 ...
- 学习Java第七周
重要知识点 1.“super”的用法 构造器和方法,都用关键字super指向超类,但是用的方法不一样.方法用这个关键字去执行被重载的超类中的方法 2.接口和抽象类的异同 相同: 1.接口和抽象类都有抽 ...
- filter 应用
一.统一全站字符编码 通过配置参数charset指明使用何种字符编码,以处理Html Form请求参数的中文问题 1 package me.gacl.web.filter; 2 3 import ja ...
- 不仅仅是双11大屏—Flink应用场景介绍
双11大屏 每年天猫双十一购物节,都会有一块巨大的实时作战大屏,展现当前的销售情况. 这种炫酷的页面背后,其实有着非常强大的技术支撑,而这种场景其实就是实时报表分析. 实时报表分析是近年来很多公司采用 ...
- leetcode.1275找出井字棋的获胜者
A 和 B 在一个 3 x 3 的网格上玩井字棋. 井字棋游戏的规则如下: 玩家轮流将棋子放在空方格 (" ") 上.第一个玩家 A 总是用 "X" 作为棋子, ...
- $CH5104\ I-country$ 线性$DP$
CH Sol ”凸联通块“是什么意思呢? 其实就是图形的左端点先减小再增大,右端点先增大再减小 阶段 考虑到第k行,已经选了i个格子 状态 1.第i行的左端点与右端点 2.这一行的左端点相对于上一行的 ...
- ES6异步操作之Promise
一直以来觉得异步操作在我心头像一团迷雾,可是它重要到我们非学不可,那就把它的面纱解开吧. ES6 诞生以前,异步编程的方法,大概有下面四种. 回调函数 事件监听 发布/订阅 Promise 对象 异步 ...
- centos 7.3 服务器环境搭建——MySQL 安装和配置
centos 7.3 服务器环境搭建——MySQL 安装和配置服务器信息如下:服务器:阿里云系统 centos 7.3 (阿里云该版本最新系统)mysql版本:5.7.18 (当前时间最新版本)连接服 ...