bzoj4025 二分图 LCT + 最小生成树
题目传送门
https://lydsy.com/JudgeOnline/problem.php?id=4025
题解
貌似这道题有一个非常简单的做法是线段树分治+并查集。
可是我是为了练 LCT 来做这道题的啊。
所以我还偏要用 LCT 把这道题 A 掉!
如果一个图是二分图,那么就是说,这个图中只要有环就必须是偶环。
所以不妨建立一棵生成树,然后对于每一条边都在上面求出覆盖距离。如果覆盖距离是偶数就是错的。
但是我们需要保证这棵生成树的在该存在的时候一定要存在啊。
所以我们不妨按照出现时间加边,以消失时间为权值建立最大生成树。
然后我们用一个桶记录一下目前不合法的边就可以了。
细节非常多:
- 图中有重边,有自环;
- 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 + 最小生成树的更多相关文章
- [BZOJ4025] 二分图 LCT/(线段树分治+并查集)
4025: 二分图 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 2667 Solved: 989[Submit][Status][Discuss] ...
- bzoj4025: 二分图 lct
题意:带增删边的查询二分图 题解:因为二分图肯定带奇环,lct维护,每次要加入一条边之前判断会不会构成环,如果会就把最先会删除的边删掉,然后如果是奇环就打个标记,然后把奇环数++,删除的时候,把标记删 ...
- 【BZOJ4025】二分图 LCT
[BZOJ4025]二分图 Description 神犇有一个n个节点的图.因为神犇是神犇,所以在T时间内一些边会出现后消失.神犇要求出每一时间段内这个图是否是二分图.这么简单的问题神犇当然会做了,于 ...
- BZOJ4025: 二分图(LCT)
Description 神犇有一个n个节点的图.因为神犇是神犇,所以在T时间内一些边会出现后消失.神犇要求出每一时间段内这个图是否是二分图.这么简单的问题神犇当然会做了,于是他想考考你. Input ...
- [BZOJ4025]二分图(线段树分治,并查集)
4025: 二分图 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 2191 Solved: 800[Submit][Status][Discuss] ...
- [bzoj4025]二分图_LCT
二分图 bzoj-4025 题目大意:给定一个n个节点的图,m条边,每条边有一个产生时间和一个删除时间,询问所有时间点是否是连通图. 注释:$1\le n\le 10^5$,$1\le m\le 2\ ...
- bzoj4025 二分图
支持加边和删边的二分图判定,分治并查集水之(表示我的LCT还很不熟--仅仅停留在极其简单的模板水平). 由于是带权并查集,并且不能路径压缩,所以对权值(到父亲距离的奇偶性)的维护要注意一下. 有一个小 ...
- bzoj4025二分图(线段树分治 并查集)
/* 思维难度几乎没有, 就是线段树分治check二分图 判断是否为二分图可以通过维护lct看看是否链接出奇环 然后发现不用lct, 并查集维护奇偶性即可 但是复杂度明明一样哈 */ #include ...
- BZOJ4025 二分图 分治 并查集 二分图 带权并查集按秩合并
原文链接http://www.cnblogs.com/zhouzhendong/p/8683831.html 题目传送门 - BZOJ4025 题意 有$n$个点,有$m$条边.有$T$个时间段.其中 ...
随机推荐
- Delphi XE2 之 FireMonkey 入门(33) - 控件基础: TFmxObject: SaveToStream、LoadFromStream、SaveToBinStream、LoadFromBinStream
Delphi XE2 之 FireMonkey 入门(33) - 控件基础: TFmxObject: SaveToStream.LoadFromStream.SaveToBinStream.LoadF ...
- Java面试题集(86-115)
Java程序员面试题集(86-115) 摘要:下面的内容包括Struts 2和Hibernate的常见面试题,虽然Struts 2在2013年6月曝出高危漏洞后已经显得江河日下,而Spring MVC ...
- 【MM系列】SAP MM模块-BAPI:BAPI_GOODSMVT_CREATE的CODE分析
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP MM模块-BAPI:BAPI ...
- 【ABAP系列】SAP ABAP 取两个内表的交集 比较两个内表的不同
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP 取两个内表的交 ...
- 修改jupyter notebook默认路径,亲测
anaconda环境 任务栏中找到anaconda/jupyter notebook,鼠标右键属性 点击确认即可.
- Mysql 5.7存储过程的学习
存储过程:对sql的封装和重用,经编译创建并保存在数据库中,通过指定存储过程的名字并给定参数(需要时)来调用执行. 优缺点: (1) 优点: 执行速度快------存储过程只在创建时进行编译,以后每次 ...
- 掌握这些 Redis 技巧,百亿数据量不在话下!
一.Redis封装架构讲解 实际上NewLife.Redis是一个完整的Redis协议功能的实现,但是Redis的核心功能并没有在这里面,而是在NewLife.Core里面. 这里可以打开看一下,Ne ...
- 解决IDEA中自动生成返回值带final修饰的问题
修改配置文件: Editor--Code Style--Java--Code Generation--将Make generated local variables final勾选上
- RabbitMq学习5-路由(Routing)
一.路由(Routing) 在前面的教程中,我们实现了一个简单的日志系统.可以把日志消息广播给多个接收者. 本篇教程中我们打算新增一个功能——使得它能够只订阅消息的一个字集.例如,我们只需要把严重的错 ...
- UML类图(一)
前言 最近在学习程杰老师的<大话设计模式>,觉得非常不错,就做了一些学习笔记和总结.如果对设计模式很感兴趣的,可以直接阅读书籍,相信会有更多的收获. 本人小菜一枚,如果理解的不对的还请多多 ...