POJ1733 Parity game (美丽的jo)

Description

Now and then you play the following game with your friend. Your friend writes down a sequence consisting of zeroes and ones. You choose a continuous subsequence (for example the subsequence from the third to the fifth digit inclusively) and ask him, whether this subsequence contains even or odd number of ones. Your friend answers your question and you can ask him about another subsequence and so on. Your task is to guess the entire sequence of numbers.

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.

题目大意:(codevs上的奇偶游戏,vijos上的小胖的奇偶。)给定一个长度为L的01串,告诉你一定区间(闭区间)内1的个数是even还是odd。然后判断到那句话时出了错。

思路:用并查集,保存结点到根的奇偶情况(用数字相加,最后%2就好了),注意路径压缩的时候要处理。。。
 int rool(int x)

{

int j;

if (fa[x]!=x)

{

j=rool(fa[x]);

tot[x]=tot[x]+tot[fa[x]];

fa[x]=j;

}

return fa[x];

} (A little different)
每次对一条边的两点进行合并的时候要注意公式判断。

r1=rool(x);

r2=rool(y);

if (r1!=r2)

{

fa[r1]=r2;

tot[r1]=tot[y]+kind[i]-tot[x];

}
因为L很长,所以要离散化
#include<algorithm>
 sort(b+1,b+m*2+1);

size=unique(b+1,b+m*2+1)-b-1;

for (i=1;i<=2*m;++i)

a[i]=upper_bound(b+1,b+size+1,a[i])-b-1;

code:

#include<iostream>

#include<cstdio>

#include<cstring>

#include<algorithm>

using namespace std;

int fa[10001]={0},a[10001]={0},b[10001]={0},tot[10001]={0},kind[10001]={0};

int rool(int x)

{

int j;

if (fa[x]!=x)

{

j=rool(fa[x]);

tot[x]=tot[x]+tot[fa[x]];

fa[x]=j;

}

return fa[x];

}

int main()

{

int size,i,j,n,m,r1,r2,x,y;

bool f=false;

char ch[10];

scanf("%d%d",&n,&m);

for (i=1;i<=m;++i)

{

scanf("%d%d%s",&a[i*2-1],&a[i*2],&ch);

if (ch[0]==‘e‘) kind[i]=0;

else kind[i]=1;

++a[i*2];

b[i*2-1]=a[i*2-1];b[i*2]=a[i*2];

}

for (i=1;i<=m*2;++i)

fa[i]=i;

sort(b+1,b+m*2+1);

size=unique(b+1,b+m*2+1)-b-1;

for (i=1;i<=2*m;++i)

a[i]=upper_bound(b+1,b+size+1,a[i])-b-1;

for (i=1;i<=m;++i)

{

x=a[i*2-1];

y=a[i*2];

r1=rool(x);

r2=rool(y);

if (r1!=r2)

{

fa[r1]=r2;

tot[r1]=tot[y]+kind[i]-tot[x];

}

else

{

if (abs(tot[x]-tot[y])%2!=kind[i])

{

f=true;

cout<<i-1<<endl;

break;

}

}

}

if (!f) cout<<m<<endl;

}

时间: 2024-10-09 21:21:26

POJ1733 Parity game (美丽的jo)的相关文章

POJ1733 - Parity game - 并查集

这道题是带权并查集,只需要把权设成0或1就行,分别表示与根节点的关系.因为10亿个点,所以要用到离散化 #include<iostream> #include<stdio.h> #include<algorithm> using namespace std; int p[10005]; int dis[10005]; int ran[10005]; int tot; struct num { char oe[10]; int st,ed; int id; }po[500

poj1733 Parity Game(扩展域并查集)

描述 Now and then you play the following game with your friend. Your friend writes down a sequence consisting of zeroes and ones. You choose a continuous subsequence (for example the subsequence from the third to the fifth digit inclusively) and ask hi

并查集练习2(带权并查集)

明天旅游去爬山逛庙玩,今天练一天然后早早睡觉啦~ poj1703 Find them, Catch them (带权并查集) 1 #include<cstdio> 2 const int N=1e5+1; 3 int f[N]; 4 int r[N];//表示与父节点的关系,0同类,1不同类 5 int n; 6 void init(){ 7 for(int i=1;i<=n;++i){ 8 f[i]=i; r[i]=0; 9 } 10 } 11 int fin(int x){ 12 i

题单二:图论500

http://wenku.baidu.com/link?url=gETLFsWcgddEDRZ334EJOS7qCTab94qw5cor8Es0LINVaGMSgc9nIV-utRIDh--2UwRLvsvJ5tXFjbdpzbjygEdpGehim1i5BfzYgYWxJmu ==========  以下是最小生成树+并查集=========================[HDU]1213         How Many Tables        基础并查集★1272         小

图论五百题!

生死看淡不服就淦,这才是人生! =============================以下是最小生成树+并查集======================================[HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基础并查集★1325&&poj1308 Is It A Tree? 基础并查集★1856 More is better 基础并查集★1102 Constructing Roads 基础最小生成树★1232 畅通工程 基

图论 500题——主要为hdu/poj/zoj

转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并查集======================================[HDU]1213   How Many Tables   基础并查集★1272   小希的迷宫   基础并查集★1325&&poj1308  Is It A Tree?   基础并查集★1856   More i

并查集&amp;MST

[HDU] 1198 Farm Irrigation 基础最小生成树★ 1598 find the most comfortable road 枚举+最小生成树★★ 1811 Rank of Tetris 并查集+拓扑排序★★ 3926 Hand in Hand 同构图★ 3938 Portal 离线+并查集★★ 2489     Minimal Ratio Tree dfs枚举组合情况+最小生成树★ 4081     Qin Shi Huang's National Road System 最

图论精炼500题

忘了从哪转的了... =============================以下是最小生成树+并查集====================================== [HDU] 1213               How Many Tables                    基础并查集★ 1272               小希的迷宫                     基础并查集★ 1325&&poj1308    Is It A Tree?       

hdu图论题目分类

=============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many Tables 基础并查集★ 1272 小希的迷宫 基础并查集★ 1325&&poj1308 Is It A Tree? 基础并查集★ 1856 More is better 基础并查集★ 1102 Constructing Roads 基础最小生成树★ 1232 畅通工程 基础并查集★ 123