• bzoj4025 二分图 LCT + 最小生成树


    题目传送门

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

    题解

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

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

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


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

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

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

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

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


    细节非常多:

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

    时间复杂度 (O(mlog n)),被线段树分治+并查集的 (O(mlog^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;
    }
    
  • 相关阅读:
    iOS获取设备UUID和IDFA
    iOS之Xcode提交App中断出现:Cannot proceed with delivery: an existing transporter instance is currently uploading this package
    iOS 打测试包给测试人员测试,两种安装方式
    ios APP进程杀死之后和APP在后台接收到推送点击跳转到任意界面处理
    iOS中统计平台的使用
    Android APK反编译
    Linux客户端、服务器、窗口管理器的关系
    Linux中ctrl-c, ctrl-z, ctrl-d 区别
    如何下载百度网盘已失效资源链接
    svn 批量更新 bat脚本
  • 原文地址:https://www.cnblogs.com/hankeke/p/bzoj4025.html
Copyright © 2020-2023  润新知