BZOJ4025

# 分析：

$(u,v)$已经联通，则不加入这条边，而是查询$u$$v$两点间的距离。若为偶数则加上这条边后会形成奇环。一个奇环不可能分成数个偶环，所以从此以后都不再是二分图。若为奇数则直接忽略这条边，因为如果将来某条边会与这条边形成奇环，则用当前$u$$v$之间的路径（长度为奇数）替代这条边（长度为$1$）同样也会形成奇环。

namespace UFS
{
int fa[N], top, rk[N];
bool dis[N];
struct node
{
int x, y, f, r;
bool d;
}stack[N];
int f(const int x)
{
return x == fa[x] ? x : f(fa[x]);
}
inline void init()
{
for (int i = 1; i <= n; i++)
fa[i] = i, dis[i] = 0, rk[i] = 1;
}
inline bool dist(int x)
{
return x == fa[x] ? dis[x] : dist(fa[x]) ^ dis[x];
}
inline void merge(const int u, const int v)
{
int x = f(u), y = f(v);
if (rk[x] > rk[y])
swap(x, y);
int tmp = fa[x];
stack[top++] = (node){x, y, tmp, rk[y], dis[x]};
if (rk[x] == rk[y])
++rk[y];
dis[x] ^= dist(u) ^ dist(v) ^ 1;
fa[x] = y;
}
inline void undo(const int bck)
{
while (top > bck)
{
fa[stack[top - 1].x] = stack[top - 1].f;
rk[stack[top - 1].y] = stack[top - 1].r;
dis[stack[top - 1].x] = stack[top - 1].d;
--top;
}
}
}


# 代码：

#include <cstdio>
#include <algorithm>
#include <cctype>
#include <cstring>
using namespace std;

namespace zyt
{
template<typename T>
inline void read(T &x)
{
bool f = false;
char c;
x = 0;
do
c = getchar();
while (c != '-' && !isdigit(c));
if (c == '-')
f = true, c = getchar();
do
x = x * 10 + c - '0', c = getchar();
while (isdigit(c));
if (f)
x = -x;
}
template<typename T>
inline void write(T x)
{
static char buf[20];
char *pos = buf;
if (x < 0)
x = -x;
do
*pos++ = x % 10 + '0';
while (x /= 10);
while (pos > buf)
putchar(*--pos);
}
inline void write(const char *const s)
{
printf("%s", s);
}
const int N = 1e5 + 10, M = 2e5 + 10, B = 17;
int n, m, T;
namespace UFS
{
int fa[N], top, rk[N];
bool dis[N];
struct node
{
int x, y, f, r;
bool d;
}stack[N];
int f(const int x)
{
return x == fa[x] ? x : f(fa[x]);
}
inline void init()
{
for (int i = 1; i <= n; i++)
fa[i] = i, dis[i] = 0, rk[i] = 1;
}
inline bool dist(int x)
{
return x == fa[x] ? dis[x] : dist(fa[x]) ^ dis[x];
}
inline void merge(const int u, const int v)
{
int x = f(u), y = f(v);
if (rk[x] > rk[y])
swap(x, y);
int tmp = fa[x];
stack[top++] = (node){x, y, tmp, rk[y], dis[x]};
if (rk[x] == rk[y])
++rk[y];
dis[x] ^= dist(u) ^ dist(v) ^ 1;
fa[x] = y;
}
inline void undo(const int bck)
{
while (top > bck)
{
fa[stack[top - 1].x] = stack[top - 1].f;
rk[stack[top - 1].y] = stack[top - 1].r;
dis[stack[top - 1].x] = stack[top - 1].d;
--top;
}
}
}
int ecnt, head[1 << (B + 1)];
bool ans[N];
struct edge
{
int to, next;
}e[M * 18];
struct ed
{
int u, v, st, ed;
}arr[M];
void add(const int a, const int b)
{
e[ecnt] = (edge){b, head[a]}, head[a] = ecnt++;
}
namespace Segment_Tree
{
void insert(const int rot, const int lt, const int rt, const int ls, const int rs, const int x)
{
if (ls <= lt && rt <= rs)
{
add(rot, x);
return;
}
int mid = (lt + rt) >> 1;
if (ls <= mid)
insert(rot << 1, lt, mid, ls, rs, x);
if (rs > mid)
insert(rot << 1 | 1, mid + 1, rt, ls, rs, x);
}
void solve(const int rot, const int lt, const int rt)
{
using namespace UFS;
int bck = top;
bool flag = true;
for (int i = head[rot]; ~i; i = e[i].next)
{
int now = e[i].to, x = f(arr[now].u), y = f(arr[now].v);
if (x == y && !(dist(arr[now].u) ^ dist(arr[now].v)))
{
flag = false;
break;
}
else if (x != y)
merge(arr[now].u, arr[now].v);
}
if (flag)
{
if (lt == rt)
ans[lt] = true;
else
{
int mid = (lt + rt) >> 1;
solve(rot << 1, lt, mid);
solve(rot << 1 | 1, mid + 1, rt);
}
}
undo(bck);
}
}

int work()
{
using namespace Segment_Tree;
read(n), read(m), read(T);
UFS::init();
memset(head, -1, sizeof(head));
for (int i = 1; i <= m; i++)
{
read(arr[i].u), read(arr[i].v), read(arr[i].st), read(arr[i].ed);
insert(1, 1, T, arr[i].st + 1, arr[i].ed, i);
}
solve(1, 1, T);
for (int i = 1; i <= T; i++)
if (ans[i])
write("Yes\n");
else
write("No\n");
return 0;
}
}
int main()
{
return zyt::work();
}

posted @ 2018-11-25 11:27  Inspector_Javert  阅读(885)  评论(1编辑  收藏  举报