poj1733 Parity game

question Eye pass deliver door p O j 1733


Topic description

has a length of N 01 string, given M information. information l , r , p Express s u m [ l . . r ] p ( m O d 2 ) . find the smallest k , so that the first k + 1 Words must be lies.


answer

This topic is related to l u O g u 2024 resemblance.
we know s u m [ l . . r ] = s u m [ 1.. r ] s u m [ 1.. l 1 ] .
This puts each prefix and s u m Split into two nodes: the same x , heterogeneous x + n .
For the next steps, see l u O g u 2024 Algorithm problem solving.
This topic is also related to l u O g u 1196 resemblance.
The blogger did not write in the late stage of lazy cancer.


Summarize

There are two methods for this question:
"Sideband right" and check the set: l u O g u 1196
"Extended Domain" and lookup: l u O g u 2024
Such questions can be used.


Standard range

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 5050;
int fa[N << 1];
int find(int x)
{
    return x == fa[x] ? x : fa[x] = find(fa[x]);
}
void merge(int x, int y)
{
    fa[find(x)] = find(y);
}
int main()
{
    int n, m = 0; scanf("%*d%d", &n);
    static int a[N], b[N], d[N << 1];
    static bool c[N];
    for (int i = 1; i <= n; i++)
    {
        char opt[10];
        scanf("%d%d%s", a + i, b + i, opt);
        a[i]--; c[i] = opt[0] == 'o';
        d[++m] = a[i]; d[++m] = b[i];
    }
    sort(d + 1, d + m + 1);
    m = unique(d + 1, d + m + 1) - d - 1;
    for (int i = 1; i <= n; i++)
    {
        a[i] = lower_bound(d + 1, d + m + 1, a[i]) - d;
        b[i] = lower_bound(d + 1, d + m + 1, b[i]) - d;
    }
    for (int i = 1; i <= m * 2; i++)
        fa[i] = i;
    int ans = n + 1;
    for (int i = 1; i <= n; i++)
    {
        if (c[i])
        {
            if (find(a[i]) == find(b[i]))
            {
                ans = i;
                break;
            }
            merge(a[i], b[i] + m);
            merge(a[i] + m, b[i]);
        }
        else
        {
            if (find(a[i]) == find(b[i] + m))
            {
                ans = i;
                break;
            }
            merge(a[i], b[i]);
            merge(a[i] + m, b[i] + m);
        }
    }
    printf("%d\n", ans - 1);
    return 0;
}

Guess you like

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