wrong answer 93rd lines differ - expected: 'NO', found: 'YES'
直接从 P5787 的板子上改的,线段树分治部分应该没有问题,但是 WA 了
#include <iostream>
#include <algorithm>
#include <string.h>
#include <math.h>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <map>
#define ll long long
#define ls p << 1
#define rs p << 1 | 1
#define db double
#define L(i, j, k) for (int i = (j); i <= (k); ++ i)
#define R(i, j, k) for (int i = (j); i >= (k); -- i)
#define mp(x, y) make_pair(x, y)
#define pii pair <int, int>
#define vi vector <int>
#define eb emplace_back
#define pb push_back
#define sz(v) ((int) v.size())
#define mem(f, x) memset(f, x, sizeof (f))
using namespace std;
template <typename INT> void rd(INT &res) {
res = 0; bool f = 0; char ch = getchar();
while (ch < '0' || ch > '9') f |= ch == '-', ch = getchar();
while (ch >= '0' && ch <= '9') res = (res << 1) + (res << 3) + (ch ^ 48), ch = getchar();
res = (f ? -res : res);
}
template <typename INT, typename ...Args>
void rd(INT &res, Args &..._res) { rd(res), rd(_res...); }
void get_ch(char &ch) {
ch = getchar();
while (ch == ' ' || ch == '\n') ch = getchar();
}
int get_ca(char *s) {
int len = 0; char ch = getchar();
while (ch == ' ' || ch == '\n') ch = getchar();
while (ch != ' ' && ch != '\n') s[len ++] = ch, ch = getchar();
return len;
}
void get_str(string &s) {
s.clear(); char ch = getchar();
while (ch == ' ' || ch == '\n') ch = getchar();
while (ch != ' ' && ch != '\n') s.pb(ch), ch = getchar();
}
template <typename DB> void rd_db(DB &res) {
res = 0; bool f = 0; char ch = getchar();
while (ch < '0' || ch > '9') f |= ch == '-', ch = getchar();
while (ch >= '0' && ch <= '9') res = res * 10 + (ch ^ 48), ch = getchar();
if (ch == '.') {
db mul = 1e-1; ch = getchar();
while (ch >= '0' && ch <= '9') res = res + mul * (ch ^ 48), ch = getchar(), mul /= 10;
}
res = (f ? -res : res);
}
template <typename DB, typename ...Args>
void rd_db(DB &res, Args &..._res) { rd_db(res), rd_db(_res...); }
const int maxn = 1e5;
const int N = maxn + 10;
int n, m, f[N << 1], top, h[N << 1];
struct Edge { int u, v; } e[N];
struct SegmentTree { int l, r; vi e; } t[N << 2];
struct Merge_Info { int x, y, h; } stk[N << 1];
vi EM;
void build(int p, int l, int r) {
t[p] = {l, r, EM};
if (l == r) return ;
int mid = l + r >> 1;
build(ls, l, mid); build(rs, mid + 1, r);
}
void insert(int p, int l, int r, int id) {
if (l <= t[p].l && t[p].r <= r) { t[p].e.eb(id); return ; }
int mid = t[p].l + t[p].r >> 1;
if (l <= mid) insert(ls, l, r, id);
if (r > mid) insert(rs, l, r, id);
}
int get_fa(int x) { return x == f[x] ? x : f[x] = get_fa(f[x]); }
void merge(int x, int y) {
int fx = get_fa(x), fy = get_fa(y);
if (h[fx] > h[fy]) swap(fx, fy);
stk[++ top] = {fx, fy, h[fy]};
f[fx] = fy;
h[fy] += (h[fx] == h[fy]);
}
void solve(int p) {
int cur = top; bool flag = true;
for (int i : t[p].e) {
if (get_fa(e[i].u) == get_fa(e[i].v)) {
L(j, t[p].l, t[p].r) puts("NO");
flag = false; break;
}
merge(e[i].u, e[i].v + n), merge(e[i].u + n, e[i].v);
}
if (flag) {
if (t[p].l == t[p].r) puts("YES");
else solve(ls), solve(rs);
}
while (top > cur) h[stk[top].y] = stk[top].h, f[stk[top].x] = stk[top].x, -- top;
}
map <pii, int> Map;
signed main() {
// ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
// freopen(".in", "r", stdin);
// freopen(".out", "w", stdout);
rd(n, m);
L(i, 1, n << 1) f[i] = i;
build(1, 1, m);
L(i, 1, m) {
int u, v; rd(u, v); pii tmp = mp(u, v); e[i] = {u, v};
if (Map[tmp]) insert(1, Map[tmp], i - 1, i), Map[tmp] = 0;
else Map[tmp] = i;
}
for (auto it : Map)
if (it.second)
insert(1, it.second, m, it.second);
solve(1);
return 0;
}