AcWing 239. 奇偶游戏 边带权并查集

AcWing 239. 奇偶游戏

题意

有一个长度为 n n n 仅由0和1组成的序列。有 m m m 个问题和回答:在区间 [ l i , R i ] [l_i,R_i] [li,Ri] 内有奇数个或者偶数个 1 1 1

输出一个整数 k k k ,表示这个01序列满足 1 ∼ k 1\sim k 1k 个回答,但不满足 1 ∼ k + 1 1\sim k+1 1k+1 个回答。如果满足所有回答,则输出问题的总数量。

解法

维护 1 1 1 的数量的前缀和的奇偶性 s [ i ] s[i] s[i]

  • 如果区间 [ l , r ] [l,r] [l,r] 内有奇数个 1 1 1 ,则说明 s [ l − 1 ] s[l-1] s[l1] s [ r ] s[r] s[r] 的奇偶性不同,即 s [ l − 1 ] ⊕ s [ r ] = 1 s[l-1]\oplus s[r]=1 s[l1]s[r]=1
  • 如果区间 [ l , r ] [l,r] [l,r] 内有偶数个 1 1 1 ,则说明 s [ l − 1 ] s[l-1] s[l1] s [ r ] s[r] s[r] 的奇偶性相同,即 s [ l − 1 ] ⊕ s [ r ] = s[l-1]\oplus s[r]= s[l1]s[r]= 0
  • 如果 l − 1 l-1 l1 r r r 不在同一个集合内,就说明他们原来没有关系,那就合并这两个点。
  • 如果 l − 1 l-1 l1 r r r 在同一个集合内,检查关系是否符合,不符合就输出答案。
  • 需要先对区间离散化。
代码
#pragma region
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, a, n) for (int i = a; i <= n; ++i)
#define per(i, a, n) for (int i = n; i >= a; --i)
namespace fastIO {
#define BUF_SIZE 100000
#define OUT_SIZE 100000
//fread->R
bool IOerror = 0;
//inline char nc(){char ch=getchar();if(ch==-1)IOerror=1;return ch;}
inline char nc() {
    static char buf[BUF_SIZE], *p1 = buf + BUF_SIZE, *pend = buf + BUF_SIZE;
    if (p1 == pend) {
        p1 = buf;
        pend = buf + fread(buf, 1, BUF_SIZE, stdin);
        if (pend == p1) {
            IOerror = 1;
            return -1;
        }
    }
    return *p1++;
}
inline bool blank(char ch) { return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t'; }
template <class T>
inline bool R(T &x) {
    bool sign = 0;
    char ch = nc();
    x = 0;
    for (; blank(ch); ch = nc())
        ;
    if (IOerror) return false;
    if (ch == '-') sign = 1, ch = nc();
    for (; ch >= '0' && ch <= '9'; ch = nc()) x = x * 10 + ch - '0';
    if (sign) x = -x;
    return true;
}
inline bool R(double &x) {
    bool sign = 0;
    char ch = nc();
    x = 0;
    for (; blank(ch); ch = nc())
        ;
    if (IOerror) return false;
    if (ch == '-') sign = 1, ch = nc();
    for (; ch >= '0' && ch <= '9'; ch = nc()) x = x * 10 + ch - '0';
    if (ch == '.') {
        double tmp = 1;
        ch = nc();
        for (; ch >= '0' && ch <= '9'; ch = nc())
            tmp /= 10.0, x += tmp * (ch - '0');
    }
    if (sign)
        x = -x;
    return true;
}
inline bool R(char *s) {
    char ch = nc();
    for (; blank(ch); ch = nc())
        ;
    if (IOerror)
        return false;
    for (; !blank(ch) && !IOerror; ch = nc())
        *s++ = ch;
    *s = 0;
    return true;
}
inline bool R(char &c) {
    c = nc();
    if (IOerror) {
        c = -1;
        return false;
    }
    return true;
}
template <class T, class... U>
bool R(T &h, U &... t) { return R(h) && R(t...); }
#undef OUT_SIZE
#undef BUF_SIZE
};  // namespace fastIO
using namespace fastIO;
template <class T>
void _W(const T &x) { cout << x; }
void _W(const int &x) { printf("%d", x); }
void _W(const int64_t &x) { printf("%lld", x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template <class T, class U>
void _W(const pair<T, U> &x) { _W(x.F), putchar(' '), _W(x.S); }
template <class T>
void _W(const vector<T> &x) {
    for (auto i = x.begin(); i != x.end(); _W(*i++))
        if (i != x.cbegin()) putchar(' ');
}
void W() {}
template <class T, class... U>
void W(const T &head, const U &... tail) { _W(head), putchar(sizeof...(tail) ? ' ' : '\n'), W(tail...); }
#pragma endregion
const int maxn = 2e4 + 5;
int n, m;
int fa[maxn], d[maxn];
int a[maxn], b[maxn];
char op[maxn][10];
int findroot(int x) {
    if (x == fa[x]) return x;
    int root = findroot(fa[x]);
    d[x] ^= d[fa[x]];
    return fa[x] = root;
}
void link(int x, int y, int f) {
    int xx = findroot(x), yy = findroot(y);
    fa[xx] = yy;
    d[xx] = d[x] ^ d[y] ^ f;
}
bool check(int x, int y, int f) {
    return d[x] ^ d[y] ^ f == 0;
}
int main() {
    R(n, m);
    rep(i, 1, maxn - 1) fa[i] = i;
    rep(i, 1, m) {
        R(a[i * 2 - 1], a[i * 2]);
        a[i * 2 - 1]--;
        R(op[i] + 1);
    }
    memcpy(b, a, sizeof(a));
    sort(b + 1, b + 1 + 2 * m);
    rep(i, 1, 2 * m) a[i] = lower_bound(b + 1, b + 1 + 2 * m, a[i]) - b;
    rep(i, 1, m) {
        int f = 0;
        int u = a[i * 2 - 1], v = a[i * 2];
        if (op[i][1] == 'o') f = 1;
        int ru = findroot(u), rv = findroot(v);
        if (ru != rv)
            link(u, v, f);
        else if (!check(u, v, f))
            return 0 * printf("%d\n", i - 1);
    }
    W(m);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值