CF862B Mahmoud and Ehab and the bipartiteness 二分图染色判定
\(\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 二分图染色判定的更多相关文章
- 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 862B - Mahmoud and Ehab and the bipartiteness
862B - Mahmoud and Ehab and the bipartiteness 思路:先染色,然后找一种颜色dfs遍历每一个点求答案. 代码: #include<bits/stdc+ ...
- CodeForces - 862B Mahmoud and Ehab and the bipartiteness(二分图染色)
题意:给定一个n个点的树,该树同时也是一个二分图,问最多能添加多少条边,使添加后的图也是一个二分图. 分析: 1.通过二分图染色,将树中所有节点分成两个集合,大小分别为cnt1和cnt2. 2.两个集 ...
- 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 862B B. Mahmoud and Ehab and the bipartiteness
http://codeforces.com/problemset/problem/862/B 题意: 给出一个有n个点的二分图和n-1条边,问现在最多可以添加多少条边使得这个图中不存在自环,重边,并且 ...
- 【Codeforces Round #435 (Div. 2) B】Mahmoud and Ehab and the bipartiteness
[链接]h在这里写链接 [题意] 让你在一棵树上,加入尽可能多的边. 使得这棵树依然是一张二分图. [题解] 让每个节点的度数,都变成二分图的对方集合中的点的个数就好. [错的次数] 0 [反思] 在 ...
- 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了 ...
随机推荐
- mybatis 学习四 (上)resutlMap
SQL 映射XML 文件是所有sql语句放置的地方.需要定义一个workspace,一般定义为对应的接口类的路径.写好SQL语句映射文件后,需要在MyBAtis配置文件mappers标签中引用,例如: ...
- linux下不用空格执行带参数的5种姿势
在搞安全的时候经常会遇到代码/命令执行,不能用空格的情况,总结了几种的绕过方法. 1.!! [root@iZ28wg1kditZ tmp]# pwd /tmp [root@iZ28wg1kditZ t ...
- oracle sql 函数
(7)查询日期之间的数据 例如查询student表中出生日期(birthday)在’2016-01-01’ 和’2017-01-01’之间的数据: select * from student wher ...
- JS中,split()用法(将字符串按指定符号分割成数组)
<!DOCTYPE html> <html> <head> <meta charset="{CHARSET}"> <title ...
- Logstash-2.4.1的下载(图文详解)
第一步:进入Elasticsearch的官网 https://www.elastic.co/ 第二步:点击downloads https://www.elastic.co/downloads 第三步: ...
- Windows10更新后无限重启
以安全模式进入系统,禁用或卸载显卡驱动. 重启后重新安装驱动.
- oracle -sqlplus 调用存储过程
sp.batsqlplus user/passwd/sid @.\sp.sql >sp.sqlexit; sp.sqlexex xxxx()quit;
- uniqid() 函数 和 microtime()函数
uniqid() 函数基于以微秒计的当前时间,生成一个唯一的 ID.语法 uniqid(prefix,more_entropy) 参数 描述prefix 可选.为 ID 规定前缀.如果 ...
- SQL IN, NOT IN, EXISTS, NOT EXISTS
IN与EXISTS执行流程 IN:在查询的时候,首先查询子查询的表,然后将内表和外表做一个笛卡尔积,然后按照条件进行筛选.所以相对内表比较小的时候,in的速度较快.(IN时不对NULL进行处理) EX ...
- .NET回归 HTML----表单元素(1)和一些常用的标记
表单就是-----用于搜集不同类型的用户输入. 表单元素指的是不同类型的 input 元素.复选框.单选按钮.提交按钮等等. 首先将表单元素分为三个类型.文本类,按钮类,选择类. 表单可以嵌套在表中, ...