\(\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. stdin和STDIN_FILENO的区别

    STDIN_FILENO与stdin的区别: STDIN_FILENO: 1).数据类型:int 2).层次:系统级的API,是一个文件句柄,定义在<unistd.h>中. 3).相应的函 ...

  2. 编译适用于TP-Link WR703N的OpenWRT固件

    编译适用于TP-Link WR703N TP-Link MR11U 以及使用AR9331芯片组的单WAN/LAN复用口的路由. 注:刷机有风险,刷机需谨慎.一般情况下是不会失败的,若无法通过捅Rese ...

  3. DVWA平台v1.9-Command Injection

    命令拼接: &:简单的拼接,第一条命令和第二条命令间没有什么制约关系 &&:第一条命令执行成功了,才会执行第二条命令 |:第一条命令的输出作为第二条命令的输入 ||:第一条命令 ...

  4. Delphi IOS (二)

    1.Mac 中 simulator模拟器Home快捷键:command(Win键盘,Ctrl与Alt之间的键)+shift+h来代替,也可以点击菜单>HardWare>Home 2.iPh ...

  5. Delphi 询问框 汉化

    Delphi 询问框 汉化 d:\program files (x86)\embarcadero\studio\17.0\source\fmx\FMX.Consts.pas add this file ...

  6. LNMP 1.5 php-fpm配置文件

    php-fpm配置文件: /usr/local/php/etc/php-fpm.conf    :php-fpm服务的配置文件 /usr/local/php/etc/php.ini       :ph ...

  7. 用sass的minix定义一些代码片段,且可传参数

    /** *@module功能 *@description生成全屏方法 *@method fullscreen *@version 1.7.0 *@param{Integer}$z-index 指定层叠 ...

  8. python+requests+excel 接口测试

    1.EXCEL文件接口保存方式,如图. 2.然后就是读取EXCEL文件中的数据方法,如下: import xlrd class readExcel(object): def __init__(self ...

  9. session和cookie个字消除的方法

    session消除的方法就是: session_destroy(); cookie消除的方法就是setcookie()函数的时间设为当前时间即可 if(isset($_COOKIE['adminId' ...

  10. Tensorflow学习练习-卷积神经网络应用于手写数字数据集训练

    # coding: utf-8 import tensorflow as tffrom tensorflow.examples.tutorials.mnist import input_data mn ...