题目传送门

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;
}

bzoj4238 & loj2881 电压 二分图判定+dfs树的更多相关文章

  1. 二部图(二分图判定--dfs)

    题目链接:二部图 二部图 时间限制:1000 ms  |  内存限制:65535 KB 难度:1 描述 二 部图又叫二分图,我们不是求它的二分图最大匹配,也不是完美匹配,也不是多重匹配,而是证明一个图 ...

  2. 【BZOJ4238】电压 DFS树

    [BZOJ4238]电压 Description 你知道Just Odd Inventions社吗?这个公司的业务是“只不过是奇妙的发明(Just Odd Inventions)”.这里简称为JOI社 ...

  3. BZOJ_4238_电压_树上差分+dfs树

    BZOJ_4238_电压_树上差分+dfs树 Description 你知道Just Odd Inventions社吗?这个公司的业务是“只不过是奇妙的发明(Just Odd Inventions)” ...

  4. DFS的运用(二分图判定、无向图的割顶和桥,双连通分量,有向图的强连通分量)

    一.dfs框架: vector<int>G[maxn]; //存图 int vis[maxn]; //节点访问标记 void dfs(int u) { vis[u] = ; PREVISI ...

  5. 7.9 NOI模拟赛 A.图 构造 dfs树 二分图

    啥都想不出来的我是不是废了/dk 这道题考的主要是构造 而我想的主要是乱搞. 一个很假很假的做法:直接暴力4种颜色染色 我也不知道对不对.. 不过成功的话一定是对的. 然后考虑奇环的问题 一个很假很假 ...

  6. Spoj 2878 KNIGHTS - Knights of the Round Table | 双联通分量 二分图判定

    题目链接 考虑建立原图的补图,即如果两个骑士不互相憎恨,就在他们之间连一条无向边. 显而易见的是,如果若干个骑士在同一个点数为奇数的环上时,他们就可以在一起开会.换句话说,如果一个骑士被一个奇环包含, ...

  7. CF687A. NP-Hard Problem[二分图判定]

    A. NP-Hard Problem time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  8. hdoj 3478 Catch(二分图判定+并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3478 思路分析:该问题需要求是否存在某一个时刻,thief可能存在图中没一个点:将该问题转换为图论问题 ...

  9. poj2942 Knights of the Round Table,无向图点双联通,二分图判定

    点击打开链接 无向图点双联通.二分图判定 <span style="font-size:18px;">#include <cstdio> #include ...

随机推荐

  1. zay大爷的神仙题目 D1T1-大美江湖

    在前几天的时候,千古神犇zay(吊打zhx那个)出了一套神仙题目,所以我得来分析分析QWQ 先补个网易云链接QWQ 毕竟是T1嘛,还算是比较简单的,那道题,读完题目就发现是个中等模拟(猪国杀算大模拟的 ...

  2. 吃着火锅唱着歌学会Docker

    第一篇:Docker概述 第二篇:Docker之版本与安装 第三篇:Docker之镜像 第四篇:Docker之容器 第五篇:Dcoker之容器与镜像 第六篇:Docker之网络管理 第七篇:Docke ...

  3. freemarker程序开发

    1.程序开发入门 1.1 创建配置实例 首先,你应该创建一个freemarker.template.Configuration的实例,然后调整它的设置.Configuration实例是存储FreeMa ...

  4. Octavia 创建 loadbalancer 的实现与分析

    目录 文章目录 目录 从 Octavia API 看起 Octavia Controller Worker database_tasks.MapLoadbalancerToAmphora comput ...

  5. mybatis参数传参、取值处理等

    单个参数:mybatis不会做特殊处理 取值方式:#{参数名} 这里参数名不必与方法的形参名称一致,可以用任意参数名来接受实参 例子:方法:update(Integer id) sql映射文件取值#{ ...

  6. C++:输入n个数,通过气泡法从小到大排列顺序(掌握不熟,还请谅解)

    #include<iostream> using namespace std; int main() { int n; cin>>n; int a[n]; int i,j,t; ...

  7. 【Qt开发】V4L2 API详解 Buffer的准备和数据读取

    前面主要介绍的是:V4L2 的一些设置接口,如亮度,饱和度,曝光时间,帧数,增益,白平衡等.今天看看V4L2 得到数据的几个关键ioctl,Buffer的申请和数据的抓取. 1. 初始化 Memory ...

  8. C#银行卡号每隔4位数字加一个空格

    1.填写银行卡号每隔4位数字加一个空格 Regex.Replace(dic["BankCardNo"].ToString(), @"(\d{4}(?!$))", ...

  9. python可视化:matplotlib系列

    matplotlib 的官方文档: https://matplotlib.org/users/index.html 1 子图布局管理 布局参数 紧密布局的方法 坐标轴的公用和隐藏 2 直方图bar和b ...

  10. [19/09/08-星期日] Python的几个概念和语法

    一.表达式.语句.程序.函数 1.表达式 就是一个类似于数学公式的东西 ,比如:10 + 5 8 - 4:表达式一般仅仅用了计算一些结果,不会对程序产生实质性的影响 如果在交互模式中输入一个表达式,解 ...