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 ...
随机推荐
- ICML 2019 分析
ICML 2019 分析 Word Embeddings Understanding the Origins of Bias in Word Embeddings Popular word embed ...
- SpringBoot -- 配置mysql、hibernate
# application.properties# Server settings (ServerProperties)server.port=8081server.address=127.0.0.1 ...
- 自定义配置节点configSections的使用
//App.config <?xml version="1.0" encoding="utf-8" ?><configuration> ...
- Unity Mathf And Transform Compent(一)
Mathf类部分变量 辐射到度的转化函数,能够将弧度转化成度. Abs 能够求出绝对值 Atan 求出正切值x/y的弧度 Transform 组件中带有local 以父物体为坐标原点 global以世 ...
- DELPHI中函数、过程变量的声明与应用
Procedure型变量: 在DELPHI中,函数.过程的地址可以赋给一个特殊类型的变量,变量可用如下方式声明: var p : procedure(num:integer); //过程 或: var ...
- Scratch少儿编程系列:(二)界面介绍及相关概念
本系列后续所有Scratch的讲解均基于2.0版本介绍.系统启动后,界面如下: Scratch主要包括6个区域: 1. 菜单:新建.打开.保存 Scratch文件,2.0版本文件后缀名为 .sb2 2 ...
- 网络编程.TCP分块接收数据(AIO)(IOCP)
1.前提:每次投递的接收缓冲区 在它返回后 就不再用它进行2次投递了 于是 接收缓冲区 在返回的时候,数据都是 从接收缓冲区的偏移[0]处开始填充的,且 接收缓冲区 可能被填满 也可能未被填满. 1. ...
- HTTPS测试
1.首先理解HTTPS的含义,清楚http与HTTPS的区别 2.相对于http而言,https更加安全,因 3.使用数字证书使传输更安全,数字证书使用keytool工具生成 测试准备: 创建公钥和秘 ...
- HashMap源码分析-jdk1.7
注:转载请注明出处!!!!!!!这里咱们看的是JDK1.7版本的HashMap 学习HashMap前先知道熟悉运算符合 *左移 << :就是该数对应二进制码整体左移,左边超出的部分舍弃,右 ...
- P1168 中位数 堆
题目描述 给出一个长度为NN的非负整数序列A_iAi,对于所有1 ≤ k ≤ (N + 1) / 21≤k≤(N+1)/2,输出A_1, A_3, …, A_{2k - 1}A1,A3,…,A2 ...