• bzoj4238 & loj2881 电压 二分图判定+dfs树


    题目传送门

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

    https://loj.ac/problem/2881

    题解

    如果想要让每一条边都有电流,那么就是说这个图必须是个二分图。换句话说,整张图中必须只有偶环。

    图中环的问题考虑 dfs 树。

    对于一条非树边,想要让这条边融合以后只有偶环,那么需要整张图中的环只有这条边带来的。

    对于一条树边,那么要求就是覆盖这条边的没有偶环,并且所有的奇环都经过这条边。

    于是维护一下经过每条边的奇环的数量,偶环的数量,就可以了。


    警告:原图不连通!!!

    时间复杂度 (O(n))

    #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;
    
    int n, m, cnt;
    int dep[N], v[N], p[N];
    
    struct Edge { int to, ne; } g[M << 1]; int head[N], tot = 1;
    inline void addedge(int x, int y) { g[++tot].to = y, g[tot].ne = head[x], head[x] = tot; }
    inline void adde(int x, int y) { addedge(x, y), addedge(y, x); }
    
    inline void dfs(int x, int fa = 0, int fr = 0) {
    	dep[x] = dep[fa] + 1;
    	for fec(i, x, y) if ((i ^ fr) != 1) {
    		if (!dep[y]) dfs(y, x, i), v[x] += v[y], p[x] += p[y];
    		else if (dep[y] < dep[x] && (dep[x] - dep[y]) % 2 == 0) ++v[x], --v[y], ++cnt;
    		else if (dep[y] < dep[x]) ++p[x], --p[y];
    	}
    }
    
    inline void work() {
    	for (int i = 1; i <= n; ++i) if (!dep[i]) dfs(i);
    	int ans = 0;
    	if (cnt == 1) ++ans;
    	for (int i = 1; i <= n; ++i) if (dep[i] > 1 && !p[i] && v[i] == cnt) ++ans;
    	printf("%d
    ", ans);
    }
    
    inline void init() {
    	read(n), read(m);
    	int x, y;
    	for (int i = 1; i <= m; ++i) read(x), read(y), adde(x, y);
    }
    
    int main() {
    #ifdef hzhkk
    	freopen("hkk.in", "r", stdin);
    #endif
    	init();
    	work();
    	fclose(stdin), fclose(stdout);
    	return 0;
    }
    
  • 相关阅读:
    [转]C# 文本框只能输入数字
    [转]C# dataGridview 报“索引-1没有值”的解决办法
    配置<authorization>节(配置文件)
    [转]javascript的urlencode
    SSM框架搭建问题
    web server
    mysql 系列
    UI 框架、ORM、权限系统
    一个基于 .NET Core 2.0 开发的简单易用的快速开发框架
    DotNetty 版 mqtt 开源客户端 (MqttFx)
  • 原文地址:https://www.cnblogs.com/hankeke/p/bzoj4238.html
Copyright © 2020-2023  润新知