bzoj4238 & loj2881 电压 二分图判定+dfs树
题目传送门
https://lydsy.com/JudgeOnline/problem.php?id=4238
题解
如果想要让每一条边都有电流,那么就是说这个图必须是个二分图。换句话说,整张图中必须只有偶环。
图中环的问题考虑 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树的更多相关文章
- 二部图(二分图判定--dfs)
题目链接:二部图 二部图 时间限制:1000 ms | 内存限制:65535 KB 难度:1 描述 二 部图又叫二分图,我们不是求它的二分图最大匹配,也不是完美匹配,也不是多重匹配,而是证明一个图 ...
- 【BZOJ4238】电压 DFS树
[BZOJ4238]电压 Description 你知道Just Odd Inventions社吗?这个公司的业务是“只不过是奇妙的发明(Just Odd Inventions)”.这里简称为JOI社 ...
- BZOJ_4238_电压_树上差分+dfs树
BZOJ_4238_电压_树上差分+dfs树 Description 你知道Just Odd Inventions社吗?这个公司的业务是“只不过是奇妙的发明(Just Odd Inventions)” ...
- DFS的运用(二分图判定、无向图的割顶和桥,双连通分量,有向图的强连通分量)
一.dfs框架: vector<int>G[maxn]; //存图 int vis[maxn]; //节点访问标记 void dfs(int u) { vis[u] = ; PREVISI ...
- 7.9 NOI模拟赛 A.图 构造 dfs树 二分图
啥都想不出来的我是不是废了/dk 这道题考的主要是构造 而我想的主要是乱搞. 一个很假很假的做法:直接暴力4种颜色染色 我也不知道对不对.. 不过成功的话一定是对的. 然后考虑奇环的问题 一个很假很假 ...
- Spoj 2878 KNIGHTS - Knights of the Round Table | 双联通分量 二分图判定
题目链接 考虑建立原图的补图,即如果两个骑士不互相憎恨,就在他们之间连一条无向边. 显而易见的是,如果若干个骑士在同一个点数为奇数的环上时,他们就可以在一起开会.换句话说,如果一个骑士被一个奇环包含, ...
- CF687A. NP-Hard Problem[二分图判定]
A. NP-Hard Problem time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- hdoj 3478 Catch(二分图判定+并查集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3478 思路分析:该问题需要求是否存在某一个时刻,thief可能存在图中没一个点:将该问题转换为图论问题 ...
- poj2942 Knights of the Round Table,无向图点双联通,二分图判定
点击打开链接 无向图点双联通.二分图判定 <span style="font-size:18px;">#include <cstdio> #include ...
随机推荐
- Mac OS 10安装CocoaPods流程
一.简介 什么是CocoaPods CocoaPods是OS X和iOS下的一个第三类库管理工具,通过CocoaPods工具我们可以为项目添加被称为“Pods”的依赖库(这些类库必须是CocoaPod ...
- WPF使用BindingListCollectionView 和 ListCollectionView 视图排序集合
当将一个集合或DataTable绑定到ItemsControl控件时,在后对台会自动地创建数据视图,这个视图位于数据源和绑定的控件之间,数据视图是进行数据源的窗口.它跟踪当前项,并且提供各种功能,如排 ...
- maven 配置参数详解!
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- Delphi回车键切换焦点
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...
- Java程序员面试题集(151-180)
Java面试题集(151-180) 摘要:这部分包含了Spring.Spring MVC以及Spring和其他框架整合以及测试相关的内容,除此之外还包含了大型网站技术架构相关面试内容. 151. Sp ...
- 【web前端】面题整理(2)
今天是520,单身狗在这里祝各位520快乐! DOM节点统计 DOM 的体积过大会影响页面性能,假如你想在用户关闭页面时统计(计算并反馈给服务器)当前页面中元素节点的数量总和.元素节点的最大嵌套深度以 ...
- FacertGrid()的使用
查看数据的前五行 tips = sns.load_dataset("tips") tips.head() 引入数据,布置横向画布 g = sns.FacetGrid(tips, c ...
- scala加载spark MLlib等所有相关jar的问题
1.找到spark安装目录 E:\spackLearn\spark-2.3.3-bin-hadoop2.7\jars 里面放的是spark的所有依赖jar包 2.从idea里面javalib导入即可调 ...
- [Web 前端] 023 js 的流程控制、循环和元素的获取、操作
1. Javascript 流程控制 用于"基于不同条件执行不同的动作"的场合 1.1 if 语句 三种形式 // 第一种 if... // 第二种 if... else ... ...
- 关于Java多线程的一些面试问题
1.ArrayList和Vecoter区别? Array和ArrayList的异同点一.Array和ArrayList的区别#1. Array类型的变量在声明的同时必须进行实例化(至少得初始化数组的大 ...