codeforces 862B B. Mahmoud and Ehab and the bipartiteness
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的更多相关文章
- Codeforces 862B - Mahmoud and Ehab and the bipartiteness
862B - Mahmoud and Ehab and the bipartiteness 思路:先染色,然后找一种颜色dfs遍历每一个点求答案. 代码: #include<bits/stdc+ ...
- Codeforces 959 F. Mahmoud and Ehab and yet another xor task
\(>Codeforces\space959 F. Mahmoud\ and\ Ehab\ and\ yet\ another\ xor\ task<\) 题目大意 : 给出一个长度为 \ ...
- Coderfroces 862 B . Mahmoud and Ehab and the bipartiteness
Mahmoud and Ehab and the bipartiteness Mahmoud and Ehab continue their adventures! As everybody in ...
- 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 ...
- CF862B Mahmoud and Ehab and the bipartiteness 二分图染色判定
\(\color{#0066ff}{题目描述}\) 给出n个点,n-1条边,求再最多再添加多少边使得二分图的性质成立 \(\color{#0066ff}{输入格式}\) The first line ...
- CodeForces - 862B Mahmoud and Ehab and the bipartiteness(二分图染色)
题意:给定一个n个点的树,该树同时也是一个二分图,问最多能添加多少条边,使添加后的图也是一个二分图. 分析: 1.通过二分图染色,将树中所有节点分成两个集合,大小分别为cnt1和cnt2. 2.两个集 ...
- 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 ...
- 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 ...
- codeforces 862 C. Mahmoud and Ehab and the xor(构造)
题目链接:http://codeforces.com/contest/862/problem/C 题解:一道简单的构造题,一般构造题差不多都考自己脑补,脑洞一开就过了 由于数据x只有1e5,但是要求是 ...
随机推荐
- flannel 概述 - 每天5分钟玩转 Docker 容器技术(58)
flannel 是 CoreOS 开发的容器网络解决方案.flannel 为每个 host 分配一个 subnet,容器从此 subnet 中分配 IP,这些 IP 可以在 host 间路由,容器间无 ...
- 第9天:CSS精灵图
今天重点学习了CSS精灵图. "CSS精灵",英语css sprite,所以也叫做"CSS雪碧"技术.是一种CSS图像合并技术,该方法是将小图标和背景图像合并到 ...
- angularjs select标签中参数的传递
今天做的一个demo中需要一个下拉选择框,并根据所选择的内容向服务器发送请求. 首先百度了一下angularjs关于select的使用,一种采用ng-repeat的方式. <select ng- ...
- 使用Visual Studio 2017开发python,并在iis上部署Python Django
作为宇宙第一IDE,怎么可以不支持python开发呢? 1.Visual Studio Installer 扩展Python开发 开始菜单中打开Visual Studio Installer,点修改. ...
- 关于批量插入数据之我见(100万级别的数据,mysql) (转)
因前段时间去面试,问到如何高效向数据库插入10万条记录,之前没处理过类似问题,也没看过相关资料,结果没答上来,今天就查了些资料,总结出三种方法: 测试数据库为MySQL!!! 方法一: public ...
- 高CPU业务场景下的任务分发方案Gearman搭建一览
Gearman是当年LiveJournal用来做图片resize的,大家也明白图片resize是一个高CPU的操作,如果让web网站去做这个高CPU的功能,有可能会拖垮你的 web应用,那本篇我们 ...
- 写给后端的前端笔记:浮动(float)布局
写给后端的前端笔记:浮动(float)布局 这篇文章主要面向后端人员,对前端有深刻了解的各位不喜勿喷. 起因 前一阵子我一个后端的伙伴问我,"前端的左飘怎么做?",我立马就懵了,& ...
- Java基础---继承、抽象、接口
一.概述 继承是面向对象的一个重要特征.当多个类中存在相同属性和行为时,将这些内容抽取到单独一个类中,那么多个类无需再定义这些属性和行为,只要继那个类即可.这时,多个类可以称为子类,单 ...
- 使用jmeter进行批量数据创建
背景: 测试环境需要创建大量的测试数据,进行功能和性能的测试 测试数据创建接口是HTTP请求方式 测试数据要求不同类型的数据,要有字段进行关联,且单据的编号在DB中唯一不可重复,此外测试数据的时间类参 ...
- 为Ext添加下拉框和日期组件
Ext.onReady(function(){ var config = { fields:['module'], data:[['新建'],['删除'],['增加']}; var store = n ...