Poj 1733 Parity game

Source: Internet
Author: User

Idea: weighted and query set
Analysis:
1. Given n conditions, we need to find the first number that does not meet the conditions.
2 each condition gives the parity of 1 in the range [l, r]. Obviously, the number of 1 in the range [l, r] can be [0, r]-[0 L-1]
3. Using the concept of querying sets, rank [x] indicates the number of [x, father [x] from x to 1 of the following node, the parity can be expressed by 1 and 0.
4. Another difficulty is discretization. The general discretization step is sorting + deduplication, And Then binary can be used when searching.

Code:

 

#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;const int MAXN = 50010;struct Node{    int x;    int y;    int mark; };Node node[MAXN];int arr[MAXN] , pos;int father[MAXN] , rank[MAXN] , num;void init(){    sort(arr , arr+pos);    num = unique(arr , arr+pos)-arr;    memset(rank , 0 , sizeof(rank));    for(int i = 0 ; i <= num ; i++)        father[i] = i;}int find(int x){    if(father[x] != x){        int fa = father[x];        father[x] = find(father[x]);        rank[x] = (rank[x]+rank[fa])%2;    }    return father[x];}int search(int x){    int left = 0;    int right = num-1;    while(left <= right){        int mid = (left+right)>>1;        if(arr[mid] == x)            return mid;         else if(arr[mid] < x)            left = mid+1;        else             right = mid-1;    }}int solve(int n){    for(int i = 0 ; i < n ; i++){        int x = search(node[i].x)+1;            int y = search(node[i].y)+1;            int fx = find(x);         int fy = find(y);        int val = node[i].mark;        if(fx == fy){            if((rank[y]-rank[x]+2)%2 != val)                 return i;        }        else{            father[fx] = fy;            rank[fx] = (val+rank[y]-rank[x]+2)%2;        }    }    return n;}int main(){    int len , n;    char str[10];    while(scanf("%d%d" , &len , &n) != EOF){        pos = 0;        for(int i = 0 ; i < n ; i++){             scanf("%d %d %s" , &node[i].x , &node[i].y , str);            node[i].x--;            arr[pos++] = node[i].x;            arr[pos++] = node[i].y;            if(str[0] == 'e')                node[i].mark = 0;            else                node[i].mark = 1;        }        init();        printf("%d\n" , solve(n));    }    return 0;}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.