bzoj4025二分图(线段树分治 并查集)

时间:2023-12-22 09:37:02
/*
思维难度几乎没有, 就是线段树分治check二分图
判断是否为二分图可以通过维护lct看看是否链接出奇环
然后发现不用lct, 并查集维护奇偶性即可 但是复杂度明明一样哈 */
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<iostream>
#define f1 first
#define f2 second
#define ll long long
#define mmp make_pair
#define lson l, mid, now << 1
#define rson mid + 1, r, now << 1 | 1
#define ls now << 1
#define rs now << 1 | 1
#define M 100010
using namespace std;
int read() {
int nm = 0, f = 1;
char c = getchar();
for(; !isdigit(c); c = getchar()) if(c == '-') f = -1;
for(; isdigit(c); c = getchar()) nm = nm * 10 + c - '0';
return nm * f;
}
vector<pair<int, int> > seg[M << 2]; int n, m, t, father[M], sz[M], ans[M], dis[M];
int find(int x) {
return father[x] == x ? x : find(father[x]);
} int getdis(int x) {
return father[x] == x ? 0 : getdis(father[x]) ^ dis[x];
}
void modify(int l, int r, int now, int ln, int rn, pair<int, int> v) {
if(l > rn || r < ln) return;
if(l >= ln && r <= rn) {
seg[now].push_back(v);
return;
}
int mid = (l + r) >> 1;
modify(lson, ln, rn, v);
modify(rson, ln, rn, v);
} void work(int l, int r, int now) {
int mid = (l + r) >> 1, f = 0;
vector<pair<int, int> > tmp;
for(int i = 0; i < seg[now].size(); i++) {
int vi = seg[now][i].f1, vj = seg[now][i].f2;
int a = find(vi), b = find(vj);
if(a == b) {
if((getdis(vi) ^ getdis(vj)) == 0) {
f = 1;
break;
}
} else {
if(sz[a] > sz[b]) swap(a, b), swap(vi, vj);
sz[b] += sz[a];
dis[a] = dis[vi] ^ dis[vj] ^ 1;
father[a] = b;
tmp.push_back(mmp(a, b));
}
}
if(!f) {
if(l == r) ans[l] = 1;
else work(lson), work(rson);
}
for(int i = tmp.size() - 1; i >= 0; i--) {
int a = tmp[i].first, b = tmp[i].second;
father[a] = a;
dis[a] = 0;
sz[b] -= sz[a];
}
}
int main() {
n = read(), m = read(), t = read();
for(int i = 1; i <= n; i++) father[i] = i, sz[i] = 1;
for(int i = 1; i <= m; i++) {
int vi = read(), vj = read(), be = read(), ed = read();
modify(1, t, 1, be + 1, ed, mmp(vi, vj));
}
work(1, t, 1);
for(int i = 1; i <= t; i++) puts(ans[i] ? "Yes" : "No");
return 0;
}