题目传送门

https://lydsy.com/JudgeOnline/problem.php?id=4025

题解

貌似这道题有一个非常简单的做法是线段树分治+并查集。

可是我是为了练 LCT 来做这道题的啊。

所以我还偏要用 LCT 把这道题 A 掉!


如果一个图是二分图,那么就是说,这个图中只要有环就必须是偶环。

所以不妨建立一棵生成树,然后对于每一条边都在上面求出覆盖距离。如果覆盖距离是偶数就是错的。

但是我们需要保证这棵生成树的在该存在的时候一定要存在啊。

所以我们不妨按照出现时间加边,以消失时间为权值建立最大生成树。

然后我们用一个桶记录一下目前不合法的边就可以了。


细节非常多:

  1. 图中有重边,有自环;
  2. LCT 要开一堆变量,注意要分清每个变量的含义。

时间复杂度 \(O(m\log n)\),被线段树分治+并查集的 \(O(m\log^2n)\) 吊打。

#include<bits/stdc++.h>

#define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to)
#define dbg(...) fprintf(stderr, __VA_ARGS__)
#define File(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
#define fi first
#define se second
#define pb push_back template<typename A, typename B> inline char smax(A &a, const B &b) {return a < b ? a = b, 1 : 0;}
template<typename A, typename B> inline char smin(A &a, const B &b) {return b < a ? a = b, 1 : 0;} typedef long long ll; typedef unsigned long long ull; typedef std::pair<int, int> pii; template<typename I> inline void read(I &x) {
int f = 0, c;
while (!isdigit(c = getchar())) c == '-' ? f = 1 : 0;
x = c & 15;
while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15);
f ? x = -x : 0;
} const int N = 100000 + 7;
const int M = 200000 + 7;
const int INF = 0x3f3f3f3f; #define lc c[0]
#define rc c[1] int n, m, T, cnt;
struct Edges { int x, y, st, ed; } e[M];
std::vector<int> v1[N], v2[N];
int wg[M]; struct Node { int c[2], fa, s, sum, min, v, val, rev, p; } t[N + M];
int st[N + M];
inline bool idtfy(int o) { return t[t[o].fa].rc == o; }
inline bool isroot(int o) { return t[t[o].fa].lc != o && t[t[o].fa].rc != o; }
inline void connect(int fa, int o, int d) { t[fa].c[d] = o, t[o].fa = fa; }
inline void pushup(int o) {
t[o].s = t[t[o].lc].s + t[t[o].rc].s + 1;
t[o].sum = t[t[o].lc].sum + t[t[o].rc].sum + t[o].v;
t[o].min = t[o].val, t[o].p = o;
if (t[o].lc && smin(t[o].min, t[t[o].lc].min)) t[o].p = t[t[o].lc].p;
if (t[o].rc && smin(t[o].min, t[t[o].rc].min)) t[o].p = t[t[o].rc].p;
assert(t[o].s == 1 || (t[o].min != INF));
}
inline void pushdown(int o) {
if (!t[o].rev) return;
if (t[o].lc) t[t[o].lc].rev ^= 1, std::swap(t[t[o].lc].lc, t[t[o].lc].rc);
if (t[o].rc) t[t[o].rc].rev ^= 1, std::swap(t[t[o].rc].lc, t[t[o].rc].rc);
t[o].rev = 0;
}
inline void rotate(int o) {
int fa = t[o].fa, pa = t[fa].fa, d1 = idtfy(o), d2 = idtfy(fa), b = t[o].c[d1 ^ 1];
if (!isroot(fa)) t[pa].c[d2] = o; t[o].fa = pa;
connect(o, fa, d1 ^ 1), connect(fa, b, d1);
pushup(fa), pushup(o);
}
inline void splay(int o) {
int x = o, tp = 0;
st[++tp] = x;
while (!isroot(x)) st[++tp] = x = t[x].fa;
while (tp) pushdown(st[tp--]);
while (!isroot(o)) {
int fa = t[o].fa;
if (isroot(fa)) rotate(o);
else if (idtfy(o) == idtfy(fa)) rotate(fa), rotate(o);
else rotate(o), rotate(o);
}
}
inline void access(int o) {
for (int x = 0; o; o = t[x = o].fa)
splay(o), t[o].rc = x, pushup(o);
}
inline void mkrt(int o) {
access(o), splay(o);
t[o].rev ^= 1, std::swap(t[o].lc, t[o].rc);
}
inline int getrt(int o) {
access(o), splay(o);
while (pushdown(o), t[o].lc) o = t[o].lc;
return splay(o), o;
}
inline void link(int x, int y) {
assert((x > n) ^ (y > n));
mkrt(x);
if (getrt(y) != x) t[x].fa = y;
}
inline void cut(int x, int y) {
assert((x > n) ^ (y > n));
mkrt(x), access(y), splay(y);
if (t[y].lc == x && !t[x].rc) t[y].lc = t[x].fa = 0, pushup(y);
} inline void work() {
for (int i = 1; i <= n; ++i) t[i].s = 1, t[i].sum = t[i].v = 0, t[i].val = t[i].min = INF, t[i].p = i;
for (int i = 0; i < T; ++i) {
int len = v1[i].size();
for (int j = 0; j < len; ++j) {
int id = v1[i][j], x = e[id].x, y = e[id].y;
t[id + n].s = t[id + n].sum = t[id + n].v = 1;
t[id + n].val = t[id + n].min = e[id].ed, t[id + n].p = id + n;
if (x == y) wg[id] = 1, ++cnt;
else if (getrt(x) != getrt(y)) link(x, id + n), link(y, id + n);
else {
mkrt(x), access(y), splay(y);
int p = t[y].p - n;
assert(p > 0);
if (!(t[y].sum & 1)) {
if (e[p].ed >= e[id].ed) wg[id] = 1, ++cnt;
else wg[p] = 1, ++cnt;
}
if (e[p].ed >= e[id].ed) continue;
cut(e[p].x, p + n), cut(e[p].y, p + n);
link(x, id + n), link(y, id + n);
}
}
len = v2[i].size();
for (int j = 0; j < len; ++j) {
int id = v2[i][j], x = e[id].x, y = e[id].y;
if (wg[id]) --cnt, wg[id] = 0;
cut(x, id + n), cut(y, id + n);
}
if (!cnt) puts("Yes");
else puts("No");
}
} inline void init() {
read(n), read(m), read(T);
for (int i = 1; i <= m; ++i) {
int x, y, s, t;
read(x), read(y), read(s), read(t);
e[i].x = x, e[i].y = y, e[i].st = s, e[i].ed = t;
v1[s].push_back(i), v2[t].push_back(i);
}
} int main() {
#ifdef hzhkk
freopen("hkk.in", "r", stdin);
#endif
init();
work();
fclose(stdin), fclose(stdout);
return 0;
}

bzoj4025 二分图 LCT + 最小生成树的更多相关文章

  1. [BZOJ4025] 二分图 LCT/(线段树分治+并查集)

    4025: 二分图 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 2667  Solved: 989[Submit][Status][Discuss] ...

  2. bzoj4025: 二分图 lct

    题意:带增删边的查询二分图 题解:因为二分图肯定带奇环,lct维护,每次要加入一条边之前判断会不会构成环,如果会就把最先会删除的边删掉,然后如果是奇环就打个标记,然后把奇环数++,删除的时候,把标记删 ...

  3. 【BZOJ4025】二分图 LCT

    [BZOJ4025]二分图 Description 神犇有一个n个节点的图.因为神犇是神犇,所以在T时间内一些边会出现后消失.神犇要求出每一时间段内这个图是否是二分图.这么简单的问题神犇当然会做了,于 ...

  4. BZOJ4025: 二分图(LCT)

    Description 神犇有一个n个节点的图.因为神犇是神犇,所以在T时间内一些边会出现后消失.神犇要求出每一时间段内这个图是否是二分图.这么简单的问题神犇当然会做了,于是他想考考你. Input ...

  5. [BZOJ4025]二分图(线段树分治,并查集)

    4025: 二分图 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 2191  Solved: 800[Submit][Status][Discuss] ...

  6. [bzoj4025]二分图_LCT

    二分图 bzoj-4025 题目大意:给定一个n个节点的图,m条边,每条边有一个产生时间和一个删除时间,询问所有时间点是否是连通图. 注释:$1\le n\le 10^5$,$1\le m\le 2\ ...

  7. bzoj4025 二分图

    支持加边和删边的二分图判定,分治并查集水之(表示我的LCT还很不熟--仅仅停留在极其简单的模板水平). 由于是带权并查集,并且不能路径压缩,所以对权值(到父亲距离的奇偶性)的维护要注意一下. 有一个小 ...

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

    /* 思维难度几乎没有, 就是线段树分治check二分图 判断是否为二分图可以通过维护lct看看是否链接出奇环 然后发现不用lct, 并查集维护奇偶性即可 但是复杂度明明一样哈 */ #include ...

  9. BZOJ4025 二分图 分治 并查集 二分图 带权并查集按秩合并

    原文链接http://www.cnblogs.com/zhouzhendong/p/8683831.html 题目传送门 - BZOJ4025 题意 有$n$个点,有$m$条边.有$T$个时间段.其中 ...

随机推荐

  1. rosbag 那些事

    ..bag文件转.txt 将file_name.bag文件中topic_name话题的消息转换到Txt_name.txt文件中: rostopic echo -b file_name.bag -p / ...

  2. python - DBUtils 连接池减少oracle数据库的连接数

    问题: 接到需求,告知项目的oracle连接次数过多,对系统造成太过大的负担,要求减少oracle数据库的连接次数 分析: 仔细分析代码以后,发现产生问题的原因,在于之前要求提升oracle监控的监控 ...

  3. Quartz快速上手

    快速上手你需要干啥: 下载Quartz 安装Quartz 根据你的需要来配置Quartz 开始一个示例应用 下载和安装 The Quartz JAR Files The main Quartz lib ...

  4. [数据结构] 2.3 Trie树

    抱歉更新晚了,看了几天三体,2333,我们继续数据结构之旅. 一.什么是Tire树? Tire树有很多名字:字典树.单词查找树. 故名思意,它就是一本”字典“,当我们查找"word" ...

  5. html的标签规范

    if/else标签{ % if condition1 %} ... display 1{ % elif conditon2 %} ... display 2{ % else % } ... displ ...

  6. SIRIM上海,http://www.sirim-global.com

    SIRIM上海 http://www.sirim-global.com

  7. ARP协议基础

    ARP 什么是ARP协议 ARP协议是能够根据IP地址解析出该IP地址所在设备的MAC地址,叫(Address Resolution Protocol)地址解析协议 ARP地址的工作流程 当一台主机A ...

  8. 三、Zabbix-zabbix server部署-zabbix server

    LNMP基础环境准备完成,进行zabbix server部署参考官方文档: [https://www.zabbix.com/documentation/3.4/zh/manual/installati ...

  9. [Python3] 004 字符串的基本使用

    目录 1. 字符串简介 1.1 作用 1.2 注意点 2. 使用方式 2.1 用引号括起来 少废话,上例子 2.2 单.双引号可以"轮换交替" 少废话,上例子 3. 转义字符 3. ...

  10. 秒懂Vuejs、Angular、React原理和前端发展历史

    「前端程序发展的历史」 「 不学自知,不问自晓,古今行事,未之有也 」 我们都知道现在流行的框架:Vue.Js.AngularJs.ReactJs,已经逐渐应用到各个项目和实际应用中,它们都是MVVM ...