\(\color{#0066ff}{题目描述}\)

给出n个点,n-1条边,求再最多再添加多少边使得二分图的性质成立

\(\color{#0066ff}{输入格式}\)

The first line of input contains an integer n — the number of nodes in the tree ( \(1<=n<=10^{5}\) ).

The next n−1 lines contain integers u and v ( \(1<=u,v<=n u≠v u≠v\) ) — the description of the edges of the tree.

It's guaranteed that the given graph is a tree.

\(\color{#0066ff}{输出格式}\)

Output one integer — the maximum number of edges that Mahmoud and Ehab can add to the tree while fulfilling the conditions.

\(\color{#0066ff}{输入样例}\)

5
1 2
2 3
3 4
4 5

\(\color{#0066ff}{输出样例}\)

2

\(\color{#0066ff}{题解}\)

先二分图染色(原图一定是二分图)

分别统计颜色为1和0的数量

开数组记录每个节点的度

显然du就是它已经连的和它颜色相反的节点的个数

因为要求做多连多少边,为了保证性质,只要颜色不同就行,所以剩下的总数-du个点都可以连边

每条边会被算两次,最后/2

#include<cstdio>
#include<queue>
#include<vector>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cctype>
#include<cmath>
#define _ 0
#define LL long long
#define Space putchar(' ')
#define Enter putchar('\n')
#define fuu(x,y,z) for(int x=(y),x##end=z;x<=x##end;x++)
#define fu(x,y,z) for(int x=(y),x##end=z;x<x##end;x++)
#define fdd(x,y,z) for(int x=(y),x##end=z;x>=x##end;x--)
#define fd(x,y,z) for(int x=(y),x##end=z;x>x##end;x--)
#define mem(x,y) memset(x,y,sizeof(x))
#ifndef olinr
inline char getc()
{
static char buf[100001],*p1=buf,*p2=buf;
return (p1==p2)&&(p2=(p1=buf)+fread(buf,1,100001,stdin),p1==p2)? EOF:*p1++;
}
#else
#define getc() getchar()
#endif
template<typename T>inline void in(T &x)
{
int f=1; char ch; x=0;
while(!isdigit(ch=getc()))(ch=='-')&&(f=-f);
while(isdigit(ch)) x=x*10+(ch^48),ch=getc();
x*=f;
}
struct node
{
int to;
node *nxt;
};
typedef node* nod;
nod head[105005];
int n;
int col[105005],du[105050],num1,num0;
LL ans;
inline void dfs(int x,int c)
{
col[x]=c;
for(nod i=head[x];i;i=i->nxt)
{
if(~col[i->to]) continue;
dfs(i->to,c^1);
}
}
inline void add(int from,int to)
{
nod t=new node();
t->to=to;
t->nxt=head[from];
head[from]=t;
}
int main()
{
int x,y;
in(n);
fuu(i,1,n) col[i]=-1;
fuu(i,1,n-1) in(x),in(y),du[x]++,du[y]++,add(x,y),add(y,x);
dfs(1,0);
fuu(i,1,n) col[i]? num1++:num0++;
fuu(i,1,n) ans+=(col[i]? num0:num1)-du[i];
printf("%lld\n",ans>>1);
return ~~(0^_^0);
}

CF862B Mahmoud and Ehab and the bipartiteness 二分图染色判定的更多相关文章

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

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

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

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

  3. CodeForces - 862B Mahmoud and Ehab and the bipartiteness(二分图染色)

    题意:给定一个n个点的树,该树同时也是一个二分图,问最多能添加多少条边,使添加后的图也是一个二分图. 分析: 1.通过二分图染色,将树中所有节点分成两个集合,大小分别为cnt1和cnt2. 2.两个集 ...

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

  5. codeforces 862B B. Mahmoud and Ehab and the bipartiteness

    http://codeforces.com/problemset/problem/862/B 题意: 给出一个有n个点的二分图和n-1条边,问现在最多可以添加多少条边使得这个图中不存在自环,重边,并且 ...

  6. 【Codeforces Round #435 (Div. 2) B】Mahmoud and Ehab and the bipartiteness

    [链接]h在这里写链接 [题意] 让你在一棵树上,加入尽可能多的边. 使得这棵树依然是一张二分图. [题解] 让每个节点的度数,都变成二分图的对方集合中的点的个数就好. [错的次数] 0 [反思] 在 ...

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

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

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

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

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

随机推荐

  1. Idea编译器 报@Override错误

    据说这是jdk的问题,@Override是JDK5就已经有了,但有个小小的Bug,就是不支持对接口的实现,认为这不是Override 而JDK6修正了这个Bug,无论是对父类的方法覆盖还是对接口的实现 ...

  2. 【render】partial及其局部变量

    原文:http://www.cnblogs.com/lwm-1988/archive/2011/09/13/2175041.html 1. partial 1.1 把partial作为view的一部分 ...

  3. c++ 图解快速排序算法

    第一.算法描述 快速排序由C. A. R. Hoare在1962年提出,该算法是目前实践中使用最频繁,实用高效的最好排序算法, 快速排序算法是采用分治思想的算法,算法分三个步骤 从数组中抽出一个元素作 ...

  4. VisualGDB系列10:快速调试Linux应用程序

    根据VisualGDB官网(https://visualgdb.com)的帮助文档大致翻译而成.主要是作为个人学习记录.有错误的地方,Robin欢迎大家指正. 本文介绍如何快速调试GCC构建的Linu ...

  5. eclipse中的TODO和FIXME

    最近使用eclipse开发代码时,公司要求按他们制定代码规范编写代码,其他都还好,因为基本都养成良好习惯了,但TODO和FIXME就有点陌生,查了一下资料,发现笔者寡闻了,果然学海无涯,好了,下边解释 ...

  6. java之链表

    链表是一种物理存储单元上非连续.非顺序的存储结构. 链表是由那几个部分组成的呢? 是由N个节点组成的       每一个节点分为两部分:                         1.数据域  ...

  7. CentOS 6.3 下编译Nginx(笔记整理)

    1. 安装关联程序 [root@localhost opt]# yum search gcc [root@localhost opt]# yum install gcc-c++ [root@local ...

  8. 【总结整理】openlayer

    实时路况 http://www.cnblogs.com/gisvip/archive/2012/11/24/2787141.html

  9. 磨刀——python及相关工具

    1.python语言包 1.1去https://www.python.org/,在download栏下载最新版python2或者python3 tips:1.点击下载会很慢,推荐:迅雷,百度云盘下载, ...

  10. SpringBoot05 数据操作01 -> JPA的基本使用、基本使用02

    前提: 创建一个springboot项目 创建一个名为springboottest的MySQL数据库 1 jar包准备 jpa的jar包 mysql驱动的jar包 druid数据库连接池的jar包 l ...