H - Parity game 并查集
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
Output
Sample Input
10
5
1 2 even
3 4 odd
5 6 even
1 6 even
7 10 odd
Sample Output
3
#include<iostream>
#include<map>
#include<string>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<cstring>
#include<cmath>
#include<vector>
#include<set>
#include<queue>
#include<iomanip>
#include<iostream>
using namespace std;
#define MAXN 10004
#define INF 0x3f3f3f3f
typedef long long LL;
/*
数据量太大,数组开不下,在这里用map+并查集
*/
int pre[MAXN],rank[MAXN],n,m,tol;
map<int,int> Mp;
int insert(int x)
{
if(Mp.find(x)==Mp.end())
Mp[x] = tol++;
return Mp[x];
}
int find(int x)
{
if(pre[x]==-)
return x;
int fx = pre[x];
pre[x] = find(pre[x]);
rank[x] = (rank[x]+rank[fx])%;
return pre[x];
}
int main()
{
while(scanf("%d%d",&n,&m)==)
{
memset(pre,-,sizeof(pre));
memset(rank,,sizeof(rank));
tol = ;
Mp.clear();
int x,y,d,u,v,i;
char op[];
bool f = false;
for(i=;i<=m;i++)
{
cin>>x>>y>>op;
if(f) continue;
d = (op[]=='e')?:;
u = insert(x-);
v = insert(y);
int f1 = find(u),f2 = find(v);
if(f1==f2)
{
if((rank[u]+d)%!=rank[v])
{
f = true;
cout<<i-<<endl;
continue;
}
}
else
{
pre[f2] = f1;
rank[f2] = (rank[u]-rank[v]+d+)%;
}
}
if(!f)
cout<<m<<endl;
}
return ;
}
H - Parity game 并查集的更多相关文章
- POJ 1733 Parity game (并查集)
Parity game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6816 Accepted: 2636 Descr ...
- 并查集——合作网络D306
合作网络D306 运行时间限制:1000ms: 运行空间限制:51200KB: 试题描述 有n个结点,初始时每个结点的父结点都不存在.你的任务是执行若干次Set操作和Query ...
- 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 ...
- POJ 1733 Parity game(离散化+带权并查集)
离散化+带权并查集 题意:长度为n的0和1组成的字符串,然后问第L和R位置之间有奇数个1还是偶数个1. 根据这些回答, 判断第几个是错误(和之前有矛盾)的. 思路:此题同HDU 3038 差不多,询问 ...
- POJ 1773 Parity game 带权并查集
分析:带权并查集,就是维护一堆关系 然后就是带权并查集的三步 1:首先确定权值数组,sum[i]代表父节点到子节点之间的1的个数(当然路径压缩后代表到根节点的个数) 1代表是奇数个,0代表偶数个 2: ...
- POJ1733 Parity game 【扩展域并查集】*
POJ1733 Parity game Description Now and then you play the following game with your friend. Your frie ...
- POJ1733 Parity game 【带权并查集】*
POJ1733 Parity game Description Now and then you play the following game with your friend. Your frie ...
- POJ 1733 Parity game (带权并查集)
题意:有序列A[1..N],其元素值为0或1.有M条信息,每条信息表示区间[L,R]中1的个数为偶数或奇数个,但是可能有错误的信息.求最多满足前多少条信息. 分析:区间统计的带权并查集,只是本题中路径 ...
随机推荐
- 【杂文】5亿大质数表(5e8)
[杂文]\(5\) 亿大质数表(\(5e8\)) 在写哈希,刷数论题时曾一度想要查质数,\(F**k\) 百度文库数据又少,翻页蛋疼,还不给复制,真的是服了. 于是在我闲的蛋疼的时候就搞了个质数表出来 ...
- [BZOJ3223/Tyvj1729]文艺平衡树
Description 您需要写一种数据结构(可参考题目标题),来维护一个有序数列 其中需要提供以下操作: 翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 ...
- GIT学习之路第三天 文件操作
本文参考廖雪峰老师的博客进行总结,完整学习请转廖雪峰博客 一.版本回退 1.git log提交日志 在git中可以通过个git log 命令显示从最近到最远的提交日志. $ git log commi ...
- 贪心 Codeforces Round #135 (Div. 2) C. Color Stripe
题目传送门 /* 贪心:当m == 2时,结果肯定是ABABAB或BABABA,取最小改变量:当m > 2时,当与前一个相等时, 改变一个字母 同时不和下一个相等就是最优的解法 */ #incl ...
- Unity笔记(3)自学第二天
学习记录: 界面使用: 脚本使用: 脚本注意点:
- HDU_1166_敌兵布阵
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- JavaScript 实现页面中录音功能
页面中实现录音需要使用浏览器提供的 MediaRecorder API,所以前提是需要浏览器支持 MediaStream Recording 相关的功能. 以下代码默认工作在 Chrome 环境中. ...
- JS中的let和var的区别
最近很多前端的朋友去面试被问到let和var的区别,其实阮一峰老师的ES6中已经很详细介绍了let的用法和var的区别.我简单总结一下,以便各位以后面试中使用. ES6 新增了let命令,用来声明局部 ...
- CentOS下安装微软雅黑字体
CentOS下安装微软雅黑字体 微软雅黑下载地址:http://download.csdn.net/detail/u012547633/9796219 1.先从你本机 C:\Windows\Fon ...
- 【LeetCode】9、Palindrome Number(回文数)
题目等级:Easy 题目描述: Determine whether an integer is a palindrome. An integer is a palindrome when it rea ...