ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2019-11-01 16:06:58  阅读:398  来源: 互联网

标签:typedef loj2881 int long 偶环 dfs inline bzoj4238 define


题目传送门

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\n", 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;
}

标签:typedef,loj2881,int,long,偶环,dfs,inline,bzoj4238,define
来源: https://www.cnblogs.com/hankeke/p/bzoj4238.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有