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 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. 題目:給定若干個區間,在給定這個區間'1'個數的奇偶性,問第幾個與前面的答案沖突,假設第X個沖突,則輸出X-1,如果都沒沖突,則輸出詢問的個數, 思路:我們可以用帶權并查集解決,因為范圍1e9,于是我們需要離散化一下,權值分別為:區間為偶數0,區間為奇數1,細節問題,(x,y)區間則需要查詢(x-1,y),這樣我們可以把并查集初始權值為0,因為自身無意義,需要與其他相關聯才有區間的意義,1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <queue> 5 #include <vector> 6 #include <cmath> 7 8 using namespace std; 9 10 #define ll long long 11 #define pb push_back 12 #define fi first 13 #define se second 14 15 16 const int N = 5010 << 1; 17 struct node 18 { 19 int rt, v; 20 }fa[N]; 21 struct info 22 { 23 int x, y, d; 24 }; 25 vector<info > a; 26 vector<int > id; 27 int n, m; 28 29 int Find(int x) 30 { 31 if(fa[x].rt == x) return x; 32 else{ 33 int tmp = fa[x].rt; 34 fa[x].rt = Find(tmp); 35 fa[x].v = (fa[x].v + fa[tmp].v) % 2; 36 return fa[x].rt; 37 } 38 } 39 40 bool Union(int x, int y, int d) 41 { 42 int fax = Find(x); 43 int fay = Find(y); 44 if(fax != fay){ 45 fa[fay].rt = fax; 46 fa[fay].v = (fa[x].v + d - fa[y].v + 2) % 2; 47 return true; 48 }else{ 49 if(0 == (fa[x].v + d - fa[y].v + 2) % 2) return true; 50 else return false; 51 } 52 } 53 54 void solve() 55 { 56 scanf("%d%d", &n, &m); 57 int x, y; 58 char op[10]; 59 for(int i = 1; i <= m; ++i){ 60 scanf("%d%d%s", &x, &y, op); 61 id.pb(x); 62 id.pb(y); 63 a.pb({x, y, op[0] == 'e' ? 0 : 1}); 64 } 65 sort(id.begin(), id.end()); 66 id.erase(unique(id.begin(), id.end()), id.end()); 67 int n = id.size(); 68 69 for(int i = 0; i <= n; ++i){ 70 fa[i].rt = i; 71 fa[i].v = 0; 72 } 73 74 int inx = -1; 75 for(int i = 0; i < m; ++i){ 76 int x = lower_bound(id.begin(), id.end(), a[i].x) - id.begin() + 1; 77 int y = lower_bound(id.begin(), id.end(), a[i].y) - id.begin() + 1; 78 if(!Union(x - 1, y, a[i].d)){ 79 inx = i; 80 break; 81 } 82 } 83 84 //printf("ans = %d\n", inx == -1 ? m : inx); 85 printf("%d\n", inx == -1 ? m : inx); 86 } 87 88 int main() 89 { 90 91 solve(); 92 93 return 0; 94 }
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/24753.html
標籤:其他
