CodeForces - 862B Mahmoud and Ehab and the bipartiteness(二分图染色)
题意:给定一个n个点的树,该树同时也是一个二分图,问最多能添加多少条边,使添加后的图也是一个二分图。
分析:
1、通过二分图染色,将树中所有节点分成两个集合,大小分别为cnt1和cnt2。
2、两个集合间总共可以连cnt1*cnt2条边,给定的是一个树,因此已经连了n-1条边,所以最多能连cnt1*cnt2-(n-1)条边。
3、注意输出。
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define lowbit(x) (x & (-x))
const double eps = 1e-8;
inline int dcmp(double a, double b){
if(fabs(a - b) < eps) return 0;
return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 100000 + 10;
const int MAXT = 10000 + 10;
using namespace std;
int color[MAXN];
vector<int> G[MAXN];
bool dfs(int v, int c){
color[v] = c;
int len = G[v].size();
for(int i = 0; i < len; ++i){
if(color[G[v][i]] == c) return false;
if(color[G[v][i]] == 0 && !dfs(G[v][i], -c)) return false;
}
return true;
}
int main(){
int n;
scanf("%d", &n);
int a, b;
for(int i = 0; i < n - 1; ++i){
scanf("%d%d", &a, &b);
G[a].push_back(b);
G[b].push_back(a);
}
for(int i = 1; i <= n; ++i){
if(color[i] == 0){
dfs(i, 1);
}
}
int cnt1 = 0;
int cnt2 = 0;
for(int i = 1; i <= n; ++i){
if(color[i] == 1) ++cnt1;
if(color[i] == -1) ++cnt2;
}
printf("%lld\n", (LL)cnt1 * (LL)cnt2 - (n - 1));
return 0;
}
CodeForces - 862B Mahmoud and Ehab and the bipartiteness(二分图染色)的更多相关文章
- Codeforces 862B - Mahmoud and Ehab and the bipartiteness
862B - Mahmoud and Ehab and the bipartiteness 思路:先染色,然后找一种颜色dfs遍历每一个点求答案. 代码: #include<bits/stdc+ ...
- CF862B Mahmoud and Ehab and the bipartiteness 二分图染色判定
\(\color{#0066ff}{题目描述}\) 给出n个点,n-1条边,求再最多再添加多少边使得二分图的性质成立 \(\color{#0066ff}{输入格式}\) The first line ...
- Coderfroces 862 B . Mahmoud and Ehab and the bipartiteness
Mahmoud and Ehab and the bipartiteness Mahmoud and Ehab continue their adventures! As everybody in ...
- Codeforces 959D. Mahmoud and Ehab and another array construction task(构造, 简单数论)
Codeforces 959D. Mahmoud and Ehab and another array construction task 题意 构造一个任意两个数都互质的序列,使其字典序大等于a序列 ...
- E - Mahmoud and Ehab and the bipartiteness CodeForces - 862B (dfs黑白染色)
Mahmoud and Ehab continue their adventures! As everybody in the evil land knows, Dr. Evil likes bipa ...
- Codeforces 862A Mahmoud and Ehab and the MEX
传送门:CF-862A A. Mahmoud and Ehab and the MEX time limit per test 2 seconds memory limit per test 256 ...
- Codeforces 959F Mahmoud and Ehab and yet another xor task 线性基 (看题解)
Mahmoud and Ehab and yet another xor task 存在的元素的方案数都是一样的, 啊, 我好菜啊. 离线之后用线性基取check存不存在,然后计算答案. #inclu ...
- Codeforces 862C - Mahmoud and Ehab and the xor
862C - Mahmoud and Ehab and the xor 思路:找两对异或后等于(1<<17-1)的数(相当于加起来等于1<<17-1),两个再异或一下就变成0了 ...
- Codeforces 862D. Mahmoud and Ehab and the binary string (二分)
题目链接:Mahmoud and Ehab and the binary string 题意: 一道交互题,首先给出一个字符串的长度l.现在让你进行提问(最多15次),每次提问提出一个字符串,会返回这 ...
随机推荐
- Python的常用库
读者您好.今天我将介绍20个属于我常用工具的Python库,我相信你看完之后也会觉得离不开它们.他们是: Requests.Kenneth Reitz写的最富盛名的http库.每个Python程序员都 ...
- 解决IE打开时,弹出的提示调用active的问题,阻止js运行。
在html和head中间加上: <!-- saved from url=(0014)about:internet -->
- SpringSecurity-权限关联与控制
6.3 服务器端方法级权限控制 在服务器端我们可以通过Spring security提供的注解对方法来进行权限控制.Spring Security在方法的权限控制上 支持三种类型的注解,JSR-250 ...
- 设计模式课程 设计模式精讲 17-2 模板方法模式coding
1 代码演练 1.1 代码演练1 1.2 代码演练2(后端课程子类运用钩子方法,加入写手记的方法) 1.3 代码演练3(前端有多个子类,有得需要写手记,有得不需要写,如何实现?) 1 代码演练 1.1 ...
- Python使用正则表达式(Regular Expression)超详细
一.导入re库 python使用正则表达式要导入re库. import re在re库中.正则表达式通常被用来检索查找.替换那些符合某个模式(规则)的文本.ps:另外很多人在学习Python的过程中,往 ...
- 数字对象NSNumber的使用
先简述下关于NSNumber的信息 NSNumber的存在就相当于java中的装箱与拆箱.只不过java中的装箱拆箱过程,使用的是对应的类型,比如基本数据类型是int.double类型,装箱时就得对应 ...
- Django线上部署代码修改失效问题
记一次django项目的线上部署维护问题,django+nginx 关于nginx反向代理服务器的介绍这里有一篇博客介绍的比较好:nginx的相关介绍 以及当一次客户端请求发出后,uwsig以及uWS ...
- 吴裕雄--天生自然HADOOP学习笔记:基本环境配置
实验目的 学习安装Java 学习配置环境变量 学习设置免密码登陆的方法 掌握Linux环境下时间同步的配置 实验原理 1.Java的安装 java是大数据的黄金语言,这和java跨平台的特性是密不可分 ...
- java学习-循环结构-递归练习1-汉诺塔问题
相传在印度圣庙中,有一种被称为汉诺塔(Hanoi)的游戏.该游戏是在一块铜板装置上,有三根杆(编号A.B.C),在A杆自下而上.由大到小按顺序放置64个金盘(如下图).游戏的目标:把A杆上的金盘全部移 ...
- eclipse中从数据库生成hibernate实体类
为什么写这篇BLOG,是因为经常有同事或网友问起我hiberante实体类的生成问题.所以下次再有人问我可以省一堆的话了,其实这个真的是很简单. 现在hibernate在项目中的应用是越 ...