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$个时间段.其中 ...
随机推荐
- 使用foreach进行批量更新
public void addEmps(@Param("emps")List<Employee> emps); 2映射文件配置 <!-- 批量保存 --> ...
- Jquery Ajax调用asmx出错问题
1.//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释. [System.Web.Script.Services.ScriptService] 这个 ...
- C# CLR20R3 程序终止的几种解决方案 【转】
[转]CLR20R3 程序终止的几种解决方案 这是因为.NET Framework 1.0 和 1.1 这两个版本对许多未处理异常(例如,线程池线程中的未处理异常)提供支撑,而 Framework ...
- Python学习之==>有依赖关系的接口开发
一.接口需求 1.登录接口 (1)登录成功后将session信息存入redis数据库并设置失效时间为600秒 (2)构造返回结果的对象flask.make_response() (3)产生cookie ...
- Oracle11g数据文件DBF迁移
最近接手了一个以前同事遗留下来的项目,时机比较敏感,因为要召开11届全国少数名族运动会.建国70周年,以及香港暴乱,其中网站上挂载有十几个系统的入口链接,不巧的是其中一个系统存在若口令,被公安部安全局 ...
- Vue模版编译
一 模版文件 <div> <header> <h1>I'm a template!</h1> </header> <p v-if=&q ...
- kms自动激活Windows和Office
采用脚本激活 无毒无公害 下载后解压,然后双击运行即可自动激活 激活脚本点此下载
- Java基础/时间日期格式
Java时间日期格式转换 一.Date转String和String转Date 参考博客:https://www.cnblogs.com/sharpest/p/7879377.html public s ...
- 安装mysql5.6-centOs7
安装mysql mysql,下载地址:https://dev.mysql.com/downloads/mysql/ 安装参考链接:https://segmentfault.com/a/11900000 ...
- SparkStreaming DStream转换
1.无状态转换操作 (1)无状态转化操作就是把简单的RDD转化操作应用到每个批次上,也就是转换DStream中的每一个RDD. 部分无状态转化操作: (2)尽管这些函数韩起来像作用在整个流上一样,但事 ...