http://codeforces.com/problemset/problem/862/B

题意:

给出一个有n个点的二分图和n-1条边,问现在最多可以添加多少条边使得这个图中不存在自环,重边,并且此图还是一个二分图。

思路:

想得比较复杂了。。。。其实既然已经给出了二分图并且有n-1条边,那么我们就一定可以用染色法对每一个点进行染色,从而将点划分为两个集合,然后从两个集合中分别任意选择一个点连边就行了。

一开始考虑二分图的基本属性是不存在奇数条边的环。。。并不需要这样,因为两个集合是分开的,从两个中分别任意选择一个连边,是肯定不会造成同一集合中的两点有边相连的。

代码:

 #include <stdio.h>
#include <vector>
using namespace std; vector<int> v[];
bool vis[]; int color[]; void dfs(int s)
{
vis[s] = ; if (color[s] == ) color[s] = ; for (int i = ;i < v[s].size();i++)
{
int to = v[s][i]; if (!vis[to])
{
if (color[s] == ) color[to] = ;
else color[to] = ; vis[to] = ;
dfs(to);
}
}
} int main()
{
int n; scanf("%d",&n); for (int i = ;i < n - ;i++)
{
int x,y; scanf("%d%d",&x,&y); v[x].push_back(y);
v[y].push_back(x);
} dfs(); long long cnt1 = ,cnt2 = ; for (int i = ;i <= n;i++)
{
if (color[i] == ) cnt1++;
else cnt2++;
} printf("%I64d\n",cnt1 * cnt2 - (n - )); return ;
}

PS:记得要用long long,要不会wa。

codeforces 862B B. 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. Codeforces 959 F. Mahmoud and Ehab and yet another xor task

    \(>Codeforces\space959 F. Mahmoud\ and\ Ehab\ and\ yet\ another\ xor\ task<\) 题目大意 : 给出一个长度为 \ ...

  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. 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. CF862B Mahmoud and Ehab and the bipartiteness 二分图染色判定

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

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

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

  7. Codeforces 959 D Mahmoud and Ehab and another array construction task

    Discription Mahmoud has an array a consisting of n integers. He asked Ehab to find another arrayb of ...

  8. Codeforces 959 E Mahmoud and Ehab and the xor-MST

    Discription Ehab is interested in the bitwise-xor operation and the special graphs. Mahmoud gave him ...

  9. codeforces 862 C. Mahmoud and Ehab and the xor(构造)

    题目链接:http://codeforces.com/contest/862/problem/C 题解:一道简单的构造题,一般构造题差不多都考自己脑补,脑洞一开就过了 由于数据x只有1e5,但是要求是 ...

随机推荐

  1. CentOS 7 服务器配置--安装Redis

    #下载Redis wget -r -np -nd http://download.redis.io/releases/redis-3.2.8.tar.gz #解压文件 tar zxvf redis-3 ...

  2. easyUI自带的时间插件日期选择、月份选择、时间选择的使用(转)

    1.日期选择 只要将class设置成easyui-datebox就可以了,当然前提是已经应用了easyui的js <input type="text" class=" ...

  3. Netty SSL安全配置

    Netty SSL安全配置 [TOC] 摘要 在研发蜂鸟部署平台的过程中,涉及到平台网关和前置agent的通信加密,虽然目前软件在内网中,但是由于蜂鸟平台和agent的特殊性,一旦被控制,部署的软件就 ...

  4. 用NETSH WINSOCK RESET命令修复网络

    之前遇到一种情况,QQ可以正常使用,浏览器无法访问网页.在CMD下ping 主机也提示超时. 最后通过NETSH WINSOCK RESET命令重置WINSOCK目录,成功修复了网络连接. Netsh ...

  5. 线性代数-矩阵-【1】矩阵汇总 C和C++的实现

    矩阵的知识点之多足以写成一本线性代数. 在C++中,我们把矩阵封装成类.. 程序清单: Matrix.h//未完待续 #ifndef _MATRIX_H #define _MATRIX_H #incl ...

  6. javaScript 设计模式系列之三:代理模式

    介绍 代理模式为其他对象提供一种代理以控制对这个对象的访问. 根据代理模式的使用目的不同,代理模式又可以分为多种类型: 远程代理(Remote Proxy) 虚拟代理(Virtual Proxy)如需 ...

  7. uva11991 Easy Problem from Rujia Liu?

    Though Rujia Liu usually sets hard problems for contests (for example, regional contests like Xi'an ...

  8. Linux企业运维人员最常用150个命令汇总

    命令 功能说明 线上查询及帮助命令(2个) man 查看命令帮助,命令的词典,更复杂的还有info,但不常用. help 查看Linux内置命令的帮助,比如cd命令. 文件和目录操作命令(18个) l ...

  9. 欠了好久的CRM帖子,双11来读。

    又一年双11了,觉得天猫双11越来越没特色了. 从折扣,音符旋律到红包,今年15年却找不出往年的热度,只是商家还是一样的急,备着活动目标计划,做着库存价格打标视觉设计这种苦逼的日子. 欠了好久的CRM ...

  10. 第一行代码_activity生命周期

    这类文章基本上已经是烂大街了,不过我今天仍然要给自己做一遍梳理,因为通过昨天的项目我发现自己还是不太懂activity的各个生命周期;各位看官勿喷; 七个生命周期及其作用 oncreat 完成初始化操 ...