并查集——奇偶性(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 ...
随机推荐
- 关于electron中入口文件main.js一些重要参数(持续更新maybe)
const {app, BrowserWindow} = require('electron') const path = require('path') let mainWindow functio ...
- dotnet 通过 WMI 获取系统安装的驱动
本文告诉大家如何通过 WMI 获取用户已经安装的驱动程序 通过 Win32_SystemDriver 可以获取用户已经安装的驱动程序 var mc = "Win32_SystemDriver ...
- HDU5618 Jam's problem again CDQ分治
Jam's problem again CDQ分治 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=5618 题意: \[ 有n 个元素,第 i 个元素有 ...
- 让Word Add-in For MediaWiki支持Word 2013
1.下载并安装Word Add-in For MediaWiki https://www.microsoft.com/en-us/download/details.aspx?id=12298 2.在“ ...
- U3D Shader_图片模糊处理
shader"practice/12.14"{ properties { _MainTex("MainTex",2D) = ""{} } S ...
- pyspider遇到的第一个坑:Active Tasks成功,Results无内容
#!/usr/bin/env python# -*- encoding: utf-8 -*-# Created on 2020-01-04 16:30:27# Project: HomeWork fr ...
- Codeforces Round #612 (Div. 2) 前四题题解
这场比赛的出题人挺有意思,全部magic成了青色. 还有题目中的图片特别有趣. 晚上没打,开virtual contest打的,就会前三道,我太菜了. 最后看着题解补了第四道. 比赛传送门 A. An ...
- 共识网络BFT-SMaRt:理论与实践
目录 BFT-SMaRt 简介 分布式计数器服务 功能描述 组网配置 启动节点 常见问题 计数服务 容错服务 BFT-SMaRt 理论 BFT-SMR 典型模式 SMR 状态机复制 VP-Consen ...
- 《深入理解JAVA虚拟机》-自己动手编译JDK
环境 Ubuntu12.4-http://mirrors.163.com/ubuntu-releases/12.04/ --需要安装纯英文版,避免中文引起不必要的麻烦 OpenJDK7-https:/ ...
- Good Bye 2019(前五题题解)
这套也是后来补得. 我太菜了,第三题就卡着了.想了好久才做出来,要是参加了绝对掉分. D题是人生中做完的第一道交互题,不容易. 比赛传送门 A.Card Game 题目大意:一共有n张互不相同的牌,玩 ...