ural1003 Parity
Parity
Memory limit: 64 MB
Input
even” or “odd” (the answer, i.e. the parity of the number of ones in the chosen subsequence, where “even” means an even number of ones and “odd” means an odd number). The input is ended with a line containing −1.Output
Sample
| input | output |
|---|---|
10 |
3 |
分析:带权并查集,注意离散化;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
const int maxn=1e5+;
const int dis[][]={{,},{-,},{,-},{,}};
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
int n,m,k,t,id,p[maxn],a[maxn],s[maxn],d[maxn],q[maxn],v[maxn];
char b[];
int find(int x)
{
if(x!=p[x])
{
int fa=find(p[x]);
a[x]^=a[p[x]];
p[x]=fa;
}
return p[x];
}
bool join(int x,int y,int z)
{
int px=find(x),py=find(y);
if(px!=py)
{
p[py]=px;
a[py]=a[y]^a[x]^z;
return false;
}
return true;
}
int main()
{
int i,j;
while(~scanf("%d",&t)&&t!=-)
{
scanf("%d",&n);
memset(a,,sizeof(a));
j=;
rep(i,,n)scanf("%d%d%s",&s[i],&d[i],b),q[j++]=s[i],q[j++]=d[i],v[i]=(b[]=='o');
sort(q+,q+j+);
int num=unique(q+,q+j+)-q-;
rep(i,,n)
{
s[i]=lower_bound(q+,q+num+,s[i])-q-;
d[i]=lower_bound(q+,q+num+,d[i])-q-;
}
rep(i,,num)p[i]=i;
rep(i,,n)
{
if(join(s[i],d[i],v[i]))
{
if(a[s[i]]^a[d[i]]!=v[i])break;
}
}
printf("%d\n",i-);
}
//system("pause");
return ;
}
ural1003 Parity的更多相关文章
- 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 ...
- 51nod1204 Parity
如果sm[j]和sm[i]奇偶性相同,那么(i+1,j)个数为偶数如果奇偶性相同看成是朋友,不同的看成是敌人,那么就跟bzoj1370的做法差不多了. 如果奇偶性相同,就将x和y合并,x+n,y+n合 ...
- Codeforces Round #180 (Div. 2) C. Parity Game 数学
C. Parity Game 题目连接: http://www.codeforces.com/contest/298/problem/C Description You are fishing wit ...
- POJ 1733 Parity game (并查集)
Parity game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6816 Accepted: 2636 Descr ...
- 1003. Parity(并查集)
1003 看篇国家论文 <从<parity>的解法谈程序优化> 对于区间i,j 如果用sum[i],sum[j]来表示到i的1的个数的奇偶性 那么仔细想下 sum[i-1] 若 ...
- poj 1733 Parity game
Parity game 题意:一个长度为N(N < 1e9)内的01串,之后有K(K <= 5000)组叙述,表示区间[l,r]之间1的个数为odd还是even:问在第一个叙述矛盾前说了几 ...
- 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 ...
- HDU-2700 Parity
http://acm.hdu.edu.cn/showproblem.php?pid=2700 题目意思很重要: //e:是要使字符串中1的个数变成偶数.o:是要使字符串中1的个数变成奇数 Parit ...
- Codeforces 549C The Game Of Parity(博弈)
The Game Of Parity Solution: 这个题只需要分类讨论就可以解决. 先分别统计奇数和偶数的个数. 然后判断谁走最后一步,如果走最后一步时候同时有偶数和奇数,那么走最后一步的赢. ...
随机推荐
- As3.0 视频缓冲、下载总结
来源:http://www.cuplayer.com/player/PlayerCodeAs/2012/0913404.html 利用NetStream的以下属性: bufferTime — 缓冲区大 ...
- 注意题目条件!!! 团问题 HDU 5952
题目大意:团的定义就是,团内的所有点,两两之间各有一条边,团的大小就是点的个数.现给你一个n个点,m条边的图.问,该图中有多少点的个数为s的团. (题目保证每个点的度数不超过20,n<=100, ...
- MySql 加锁问题
1.设置非自动提交 set autocommit=0; 这时候 for update才会起作用 2.一般用法 set autocommit=0; for update(加锁) ; commit ...
- Jquery-获取勾选的checkbox的同级节点数量
// 获取勾选的标签值得上一个兄弟节点 var groups = []; $('input[name="group_name"]:checked').siblings(" ...
- Html的第一次小结
一 Html的文档结构 (1) <html> 标记html文件的头标记,没有什么实质性的作用,但是却是必不可少的 (2) <head> 放置html文件信息.如css的一些 ...
- Spring Boot 系列教程11-html页面解析-jsoup
需求 需要对一个页面进行数据抓取,并导出doc文档 html解析器 jsoup 可直接解析某个URL地址.HTML文本内容.它提供了一套非常省力的API,可通过DOM,CSS以及类似于JQuery的操 ...
- 转一篇分析C语言调用时栈的变化的好文
http://blog.csdn.net/zsy2020314/article/details/9429707
- C++调用com控件方法
转载自:http://blog.csdn.net/haijun286972766/article/details/6273414 最近要求做一个C++调用com组件的DEMO.由于自己对C++并无研究 ...
- linux命令chown和chmod什么区别
chown一般用来 更改属主.也就是文件所属用户.chmod功能要比chown要强大.可更改文件所有属性和权限.只有管理员账户才有权限用此命令. chown 是修改文件的所有者(owner),和所属组 ...
- Swift\本地文件管理
转载自:http://www.coloroud.com/2015/06/01/Swift-File-Manager/ 开头 看来Swift这趟浑水是非干不可,既然如此,那索性就来的彻底吧,来一次全方位 ...