POJ 1733 Parity【擴充套件域並查集】
題目連結:http://poj.org/problem?id=1733
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.
Input
The first line of input contains one number, which is the length of the sequence of zeroes and ones. This length is less or equal to 1000000000. In the second line, there is one positive integer which is the number of questions asked and answers to them. The number of questions and answers is less or equal to 5000. The remaining lines specify questions and answers. Each line contains one question and the answer to this question: two integers (the position of the first and last digit in the chosen subsequence) and one word which is either `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).
Output
There is only one line in output containing one integer X. Number X says that there exists a sequence of zeroes and ones satisfying first X parity conditions, but there exists none satisfying X+1 conditions. If there exists a sequence of zeroes and ones satisfying all the given conditions, then number X should be the number of all the questions asked.
Sample Input
10 5 1 2 even 3 4 odd 5 6 even 1 6 even 7 10 odd
Sample Output
3
題解:P190。如果我們用 sum 陣列表示序列 S 的字首和,那麼在每個回答中:(1)、S[l~r] 有偶數個 1,等價於 sum[l - 1] 與 sum[r] 的奇偶性相同。(2)、S[l ~ r] 有奇數個 1,等價於 sum[l - 1] 與 sum[r] 的奇偶性不同。
傳遞關係:1、若 x1 與 x2 的奇偶性相同,x2 與 x3 的奇偶性相同,則 x1 與 x3 的奇偶性相同。
2、若 x1 與 x2 的奇偶性不同,x2 與 x3 的奇偶性相同,則 x1 與 x3 的奇偶性不同。
3、若 x1 與 x2 的奇偶性不同,x2 與 x3 的奇偶性不同,則 x1 與 x3 的奇偶性相同。
另外,序列長度N很大,但問題數M較少,可以用離散化方法將每個問題的兩個整數 l - 1 和 r 縮小到等價的 1~2M 以內的範圍。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 10000+7;
struct node{
int l, r, ans;
} query[maxn];
int a[maxn], d[maxn], fa[maxn];
int n, m, t;
void read_discrete(){
cin >> n >> m;
for(int i = 1; i <= m; i++){
char str[5];
scanf("%d %d %s", &query[i].l, &query[i].r, str);
query[i].ans = (str[0] == 'o' ? 1 : 0);
a[++t] = query[i].l - 1;
a[++t] = query[i].r;
}
sort(a+1, a+t+1);
n = unique(a+1, a+t+1) - a - 1;
}
int get(int x) {
if(x == fa[x]) return x;
return fa[x] = get(fa[x]);
}
int main()
{
read_discrete();
for(int i = 1; i <= 2*n; i++) fa[i] = i;
for(int i = 1; i <= m; i++) {
int x = lower_bound(a+1, a+n+1, query[i].l - 1) - a;
int y = lower_bound(a+1, a+n+1, query[i].r) - a;
int x_odd = x, x_even = x + n;
int y_odd = y, y_even = y + n;
if(query[i].ans == 0){
if(get(x_odd) == get(y_even)){
cout << i - 1 << endl;
return 0;
}
fa[get(x_odd)] = get(y_odd);
fa[get(x_even)] = get(y_even);
}else {
if(get(x_odd) == get(y_odd)){
cout << i - 1 << endl;
return 0;
}
fa[get(x_odd)] = get(y_even);
fa[get(x_even)] = get(y_odd);
}
}
cout << m << endl;
return 0;
}
相關文章
- POJ 2492 A bug's life【擴充套件域 | 邊帶權並查集】套件並查集
- POJ 1182 食物鏈【擴充套件域 | 邊帶權並查集】套件並查集
- 並查集擴充套件並查集套件
- 並查集擴充套件應用並查集套件
- 關押罪犯 擴充套件域並查集 帶權並查集 二分圖+二分套件並查集
- HDU 3234 Exclusive-OR 擴充套件並查集套件並查集
- POJ 2891 Strange Way to Express Integers(擴充套件GCD)Express套件GC
- POJ1061擴充套件歐幾里得定理套件
- 擴充套件叢集blk數套件
- POJ2492(種類並查集)並查集
- 乾貨丨如何水平擴充套件和垂直擴充套件DolphinDB叢集?套件
- POJ 1308-Is It A Tree?(並查集)並查集
- 【POJ 1182】食物鏈(並查集)並查集
- in_list查詢與or擴充套件套件
- kotlin 擴充套件(擴充套件函式和擴充套件屬性)Kotlin套件函式
- C Looooops(poj2115+擴充套件歐幾里德)OOP套件
- POJ1797 Heavy Transportation【並查集+貪心】並查集
- POJ 1703-Find them, Catch them(並查集)並查集
- POJ 2492 A Bug's Life(關係並查集)並查集
- WCF擴充套件:行為擴充套件Behavior Extension套件
- 開源 | FLUI : Flutter 的元件擴充套件集UIFlutter元件套件
- POJ2253 Frogger【並查集+貪心】並查集
- POJ 2492-A Bug's Life(帶權並查集)並查集
- POJ 2236-Wireless Network(並查集)並查集
- POJ 2524-Ubiquitous Religions(入門並查集)UI並查集
- POJ 1703 Find them, Catch them (關係並查集)並查集
- poj 1182 並查集經典問題並查集
- 【Kotlin】擴充套件屬性、擴充套件函式Kotlin套件函式
- 檢查PHP擴充套件是否安裝成功PHP套件
- Sanic 擴充套件套件
- ORACLE 擴充套件Oracle套件
- 擴充套件工具套件
- 擴充套件歐幾里得套件
- DOM擴充套件套件
- 擴充套件ACL套件
- Lua擴充套件套件
- 照片擴充套件套件
- 擴充套件篇套件