题意:给定一个n个点的树,该树同时也是一个二分图,问最多能添加多少条边,使添加后的图也是一个二分图。

分析:

1、通过二分图染色,将树中所有节点分成两个集合,大小分别为cnt1和cnt2。

2、两个集合间总共可以连cnt1*cnt2条边,给定的是一个树,因此已经连了n-1条边,所以最多能连cnt1*cnt2-(n-1)条边。

3、注意输出。

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<cstdlib>
  4. #include<cctype>
  5. #include<cmath>
  6. #include<iostream>
  7. #include<sstream>
  8. #include<iterator>
  9. #include<algorithm>
  10. #include<string>
  11. #include<vector>
  12. #include<set>
  13. #include<map>
  14. #include<stack>
  15. #include<deque>
  16. #include<queue>
  17. #include<list>
  18. #define lowbit(x) (x & (-x))
  19. const double eps = 1e-8;
  20. inline int dcmp(double a, double b){
  21. if(fabs(a - b) < eps) return 0;
  22. return a > b ? 1 : -1;
  23. }
  24. typedef long long LL;
  25. typedef unsigned long long ULL;
  26. const int INT_INF = 0x3f3f3f3f;
  27. const int INT_M_INF = 0x7f7f7f7f;
  28. const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
  29. const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
  30. const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
  31. const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
  32. const int MOD = 1e9 + 7;
  33. const double pi = acos(-1.0);
  34. const int MAXN = 100000 + 10;
  35. const int MAXT = 10000 + 10;
  36. using namespace std;
  37. int color[MAXN];
  38. vector<int> G[MAXN];
  39. bool dfs(int v, int c){
  40. color[v] = c;
  41. int len = G[v].size();
  42. for(int i = 0; i < len; ++i){
  43. if(color[G[v][i]] == c) return false;
  44. if(color[G[v][i]] == 0 && !dfs(G[v][i], -c)) return false;
  45. }
  46. return true;
  47. }
  48. int main(){
  49. int n;
  50. scanf("%d", &n);
  51. int a, b;
  52. for(int i = 0; i < n - 1; ++i){
  53. scanf("%d%d", &a, &b);
  54. G[a].push_back(b);
  55. G[b].push_back(a);
  56. }
  57. for(int i = 1; i <= n; ++i){
  58. if(color[i] == 0){
  59. dfs(i, 1);
  60. }
  61. }
  62. int cnt1 = 0;
  63. int cnt2 = 0;
  64. for(int i = 1; i <= n; ++i){
  65. if(color[i] == 1) ++cnt1;
  66. if(color[i] == -1) ++cnt2;
  67. }
  68. printf("%lld\n", (LL)cnt1 * (LL)cnt2 - (n - 1));
  69. return 0;
  70. }

  

CodeForces - 862B Mahmoud and Ehab and the bipartiteness(二分图染色)的更多相关文章

  1. Codeforces 862B - Mahmoud and Ehab and the bipartiteness

    862B - Mahmoud and Ehab and the bipartiteness 思路:先染色,然后找一种颜色dfs遍历每一个点求答案. 代码: #include<bits/stdc+ ...

  2. CF862B Mahmoud and Ehab and the bipartiteness 二分图染色判定

    \(\color{#0066ff}{题目描述}\) 给出n个点,n-1条边,求再最多再添加多少边使得二分图的性质成立 \(\color{#0066ff}{输入格式}\) The first line ...

  3. Coderfroces 862 B . Mahmoud and Ehab and the bipartiteness

     Mahmoud and Ehab and the bipartiteness Mahmoud and Ehab continue their adventures! As everybody in ...

  4. Codeforces 959D. Mahmoud and Ehab and another array construction task(构造, 简单数论)

    Codeforces 959D. Mahmoud and Ehab and another array construction task 题意 构造一个任意两个数都互质的序列,使其字典序大等于a序列 ...

  5. 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 ...

  6. 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 ...

  7. Codeforces 959F Mahmoud and Ehab and yet another xor task 线性基 (看题解)

    Mahmoud and Ehab and yet another xor task 存在的元素的方案数都是一样的, 啊, 我好菜啊. 离线之后用线性基取check存不存在,然后计算答案. #inclu ...

  8. Codeforces 862C - Mahmoud and Ehab and the xor

    862C - Mahmoud and Ehab and the xor 思路:找两对异或后等于(1<<17-1)的数(相当于加起来等于1<<17-1),两个再异或一下就变成0了 ...

  9. Codeforces 862D. Mahmoud and Ehab and the binary string (二分)

    题目链接:Mahmoud and Ehab and the binary string 题意: 一道交互题,首先给出一个字符串的长度l.现在让你进行提问(最多15次),每次提问提出一个字符串,会返回这 ...

随机推荐

  1. 深入理解Java虚拟机-如何利用VisualVM对高并发项目进行性能分析

    前面在学习JVM的知识的时候,一般都需要利用相关参数进行分析,而分析一般都需要用到一些分析的工具,因为一般使用IDEA,而VisualVM对于IDEA也不错,所以就选择VisualVM来分析JVM性能 ...

  2. 笔记||Python3进阶之读取和写入yaml配置文件

    yaml是专门用来写配置文件的语言,简洁强大,远比JSON格式方便,yaml在python语言中有PyYAML安装包. - 首先需要pip安装:pip install pyyaml - yaml基本语 ...

  3. Python 爬取 北京市政府首都之窗信件列表-[信息展示]

    日期:2020.01.25 博客期:133 星期六 [代码说明,如果要使用此页代码,必须在本博客页面评论区给予说明] //博客总体说明 1.准备工作 2.爬取工作 3.数据处理 4.信息展示(本期博客 ...

  4. Autoit里用多进程模拟多线程

      一直以来Autoit都不支持多线程,因此一些需要同时运行多个循环的操作也就无法实现.这个问题在其它的某些语言里也经常出现,解决的方法就是使用多进程. 所谓多进程,就是同时运行多个子进程,每个子进程 ...

  5. B. Uniqueness 删除最小区间内的元素使得剩余元素唯一

    B. Uniqueness time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  6. Kubernetes——机密数据管理

    k8s——机密数据管理1.secret2.configMap kubectl explain secret    #查看帮助手册然后将你要加密的变量值做些许处理:echo 123 | base64   ...

  7. 《算法图解》[美] Aditya Bhargava(作者)epub+mobi

    内容简介 本书示例丰富,图文并茂,以让人容易理解的方式阐释了算法,旨在帮助程序员在日常项目中更好地发挥算法的能量.书中的前三章将帮助你打下基础,带你学习二分查找.大O表示法.两种基本的数据结构以及递归 ...

  8. jquery使用css函数设置背景色无效解决办法

    外部的css样式为: #imageArea{ width: 200px; height: 300px; background-color: #eee !important; } 通过 以下代码来修改其 ...

  9. Day9 - C - Bookshelf 2 POJ - 3628

    Farmer John recently bought another bookshelf for the cow library, but the shelf is getting filled u ...

  10. Python Download Image (python + requests + BeautifulSoup)

    环境准备 1 python + requests + BeautifulSoup 页面准备 主页面: http://www.netbian.com/dongman/ 图片伪地址: http://www ...