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,但是要求是 ...
随机推荐
- 神经网络与深度学习笔记 Chapter 6之卷积神经网络
深度学习 Introducing convolutional networks:卷积神经网络介绍 卷积神经网络中有三个基本的概念:局部感受野(local receptive fields), 共享权重 ...
- 解决打包时出现的Failed to verify bitcode
今天给一个老项目打包在导出包的时候报错,可能是因为xcode升级造成的 Failed to verify bitcode in Fuzi.framework/Fuzi: error: Bundle o ...
- C#使用Xamarin开发可移植移动应用进阶篇(6.使用渲染器针对单个平台自定义控件..很很很很重要..),附源码
前言 系列目录 C#使用Xamarin开发可移植移动应用目录 源码地址:https://github.com/l2999019/DemoApp 可以Star一下,随意 - - 说点什么.. 本篇..基 ...
- 关于如何在highchart上获取后台返回的值一些问题。
项目开发过程中有用到highchart图表进行项目的开发.一个比较常规的需求就是通过点击图表上的模块进行明细的查看. 1.比如坐标一月.二月.三月.四月.....有对应的值01,02,03,04... ...
- 【Weblogic】在linux创建domain过慢的解决方法
修改Linux上Weblogic使用的jdk $JAVA_HOME/jre/lib/security/java.security 文件 将 securerandom.source=file:/dev/ ...
- Java项目访问resources文件
最近在对接支付宝支付的开发,需要取到支付的RSA公钥和私钥.于是把公钥和私钥加到resources文件夹里.但是不知道怎么读到这两个文件,也就是不知道路径怎么写.于是网上搜索了下如何获取工作路径,Sy ...
- TensorFlow框架(4)之CNN卷积神经网络
1. 卷积神经网络 1.1 多层前馈神经网络 多层前馈神经网络是指在多层的神经网络中,每层神经元与下一层神经元完全互连,神经元之间不存在同层连接,也不存在跨层连接的情况,如图 11所示. 图 11 对 ...
- python 爬取天猫美的评论数据
笔者最近迷上了数据挖掘和机器学习,要做数据分析首先得有数据才行.对于我等平民来说,最廉价的获取数据的方法,应该是用爬虫在网络上爬取数据了.本文记录一下笔者爬取天猫某商品的全过程,淘宝上面的店铺也是类似 ...
- ReactNative环境搭建扩展篇——安装后报错解决方案
之前一篇写了<逻辑性最强的React Native环境搭建与调试>说了RN的安装,今天在这里做一个复盘,让我们能够更直观更深入的了解React Native(以下简称RN),这一篇重点来说 ...
- [2014-08-18]Mac OSX 命令行快捷键
系统:OSX 10.9.4 将光标移动到行首:ctrl + a 将光标移动到行尾:ctrl + e 清除屏幕: ctrl + l 搜索以前使用命令:ctrl + r 清除当前行: ctrl + u 清 ...