poj - 1733 kinds of union search + discretization

The meaning of the question: There is a 01 string with a known length, give [l,r] whether the number of 1s in the interval is odd or even, and give a series of statements to ask whether the first few are correct

A class of classic union-check set problems, the classic model is to convert the interval [l, r] into (l-1, r], then the number of 1s can be expressed as sum[r]-sum[l-1] , which also determines the parity, we can use the r[] array to represent the parity of 1 from this endpoint to its root node (this interval is (i,root(i)] (0 means even, 1 means odd)   For each input interval, we find whether their root nodes are the same.

1. If they are the same, it proves that the parity of this interval has been known before, so you can judge directly.

2. If it is different, then u-1 and v are not in the same set at this time, then we can know the interval of (u-1,root([u-1])] and (v,root([v])] The parity of interval 1, and we know the parity of (u-1,v] interval 1, then we can calculate the properties of the interval (root([u-1]),root([v])], and then merge the two Or. When merging, the root node, r[root(u)]=r(u)^r(v)^r(u-1,v], r[i]=r[i] in the path compression process ^r[root(i)], for example, the number of 1s in (a,b] is even, the number of 1s in (b,c] is odd, and the number of 1s in (a,c) is obviously odd.

The data range is 1e9, which is very large, and there are 5000 sentences, so the data needs to be discretized

Learned about discretization https://blog.csdn.net/xiangaccepted/article/details/73276826

const int maxn=1e5+10;

int a[maxn], t[maxn];

int n;

scanf("%d",&n);

for(int i=1;i<=n;i++)

scanf("%d",a[i]),t[i]=a[i];

    sort(t+1,t+n+1);

    m=unique(t+1,t+1+n)-t-1;//m is the number of unique elements

for(int i=1;i<=n;i++)

a[i]=lower_bound(t+1,t+1+m,a[i])-t;

//The original a[i] became the later a[i] after discretization;

//The range of a[i] after discretization is (1-m);

Link: poj 1733

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
#include<string>
#include <cmath>
#include <map>
#include <stack>
using namespace std;

const int maxn = 10000 + 10;
const int inf = 0x3f3f3f3f;

int n, m;
int father[maxn];
int r[maxn];
int que[maxn];
int cnt;

struct node {
    int u, v;
    int res;
} a [maxn];

int getf(int x) {
    if(x != father[x]) {
        int t = father[x];
        father[x] = getf(father[x]);
        r[x] = r[x] ^ r[t];
    }
    return father[x];
}

intmain()
{
    int T;
    while(cin >> n) {
        cin >> m;
        char str[10];
        cnt = 0;
        for(int i = 0; i < m; i++) {
            scanf("%d %d %s", &a[i].u, &a[i].v, str);
            a[i].u--;
            if(str[0] == 'e') {
                a[i].res = 1;
            }
            else {
                a[i].res = 0;
            }
            que[cnt++] = a[i].u;
            that [cnt ++] = a [i] .v;
        }
        sort (que, que + cnt);
        int ans = unique (que, que + cnt) - que;
        for(int i = 0; i < ans; i++) {
            father[i] = i;
            r[i] = 0;
        }
        int sum = 0;
        for(int i = 0; i < m; i++) {
            int u = lower_bound(que, que + ans, a[i].u) - que;
            int v = lower_bound (que, que + ans, a [i] .v) - que;
            int ra = getf (u);
            int rb = getf(v);
            if(ra == rb) {
                if(r[u] == r[v] && a[i].res == 0) {
                    break;
                }
                if(r[u] != r[v] && a[i].res == 1) {
                    break;
                }
                sum++;
            }
            else {
                if(a[i].res == 0) {
                    father[ra] = rb;
                    r [ra] = r [u] ^ r [v] ^ 1;
                }
                else {
                    father[ra] = rb;
                    r [ra] = r [u] ^ r [v];
                }
                sum++;
            }
        }
        cout << sum << endl;
    }
    return 0;
}




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324605996&siteId=291194637