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次),每次提问提出一个字符串,会返回这 ...
随机推荐
- 深入理解Java虚拟机-如何利用VisualVM对高并发项目进行性能分析
前面在学习JVM的知识的时候,一般都需要利用相关参数进行分析,而分析一般都需要用到一些分析的工具,因为一般使用IDEA,而VisualVM对于IDEA也不错,所以就选择VisualVM来分析JVM性能 ...
- 笔记||Python3进阶之读取和写入yaml配置文件
yaml是专门用来写配置文件的语言,简洁强大,远比JSON格式方便,yaml在python语言中有PyYAML安装包. - 首先需要pip安装:pip install pyyaml - yaml基本语 ...
- Python 爬取 北京市政府首都之窗信件列表-[信息展示]
日期:2020.01.25 博客期:133 星期六 [代码说明,如果要使用此页代码,必须在本博客页面评论区给予说明] //博客总体说明 1.准备工作 2.爬取工作 3.数据处理 4.信息展示(本期博客 ...
- Autoit里用多进程模拟多线程
一直以来Autoit都不支持多线程,因此一些需要同时运行多个循环的操作也就无法实现.这个问题在其它的某些语言里也经常出现,解决的方法就是使用多进程. 所谓多进程,就是同时运行多个子进程,每个子进程 ...
- B. Uniqueness 删除最小区间内的元素使得剩余元素唯一
B. Uniqueness time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- Kubernetes——机密数据管理
k8s——机密数据管理1.secret2.configMap kubectl explain secret #查看帮助手册然后将你要加密的变量值做些许处理:echo 123 | base64 ...
- 《算法图解》[美] Aditya Bhargava(作者)epub+mobi
内容简介 本书示例丰富,图文并茂,以让人容易理解的方式阐释了算法,旨在帮助程序员在日常项目中更好地发挥算法的能量.书中的前三章将帮助你打下基础,带你学习二分查找.大O表示法.两种基本的数据结构以及递归 ...
- jquery使用css函数设置背景色无效解决办法
外部的css样式为: #imageArea{ width: 200px; height: 300px; background-color: #eee !important; } 通过 以下代码来修改其 ...
- Day9 - C - Bookshelf 2 POJ - 3628
Farmer John recently bought another bookshelf for the cow library, but the shelf is getting filled u ...
- Python Download Image (python + requests + BeautifulSoup)
环境准备 1 python + requests + BeautifulSoup 页面准备 主页面: http://www.netbian.com/dongman/ 图片伪地址: http://www ...