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. css3中空格和>的区别

    A>B 表示选择A元素的所有子B元素. 与A B的区别在于,A B选择所有后代元素,而A>B只选择一代. A+B表示HTML中紧随A的B元素(A与B是同级元素).

  2. MarkdownPad2的密钥

    MarkdownPad2的密钥 经本人试用 邮箱: Soar360@live.com 授权秘钥: GBPduHjWfJU1mZqcPM3BikjYKF6xKhlKIys3i1MU2eJHqWGImDH ...

  3. 第4天:JS入门-给div设置宽高背景色

    今天学习了js入门课程,听的不多,做了个小练习,给div设置宽高.背景色.一点点都是进步.核心代码如下: <!DOCTYPE html><html lang="en&quo ...

  4. 使用jquery.PrintArea.js打印网页的样式问题

    在使用jquery.PrintArea.js打印局部网页样式的时候,发现样式打印不出来,在网上找了好多资料,整理一下分享给大家 一.先看看css的引用文件方式 1.直接在内部的元素中使用”style” ...

  5. [js高手之路]搞清楚面向对象,必须要理解对象在创建过程中的内存表示

    javascript面向对象编程方式,对于初学者来说,会比较难懂. 要学会面向对象以及使用面向对象编程,理解对象的创建在内存中的表示,至关重要. 首先,我们来一段简单的对象创建代码 var obj = ...

  6. 【Tomcat】Invalid character found in the request target

    Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC ...

  7. iOS 主题/皮肤之 SakuraKit

    前言 目前市场上很多 App 都有主题变更.皮肤切换的功能.随着项目代码量的不断增长,业务不断完善,功能性代码逐渐趋于模块化,尤其是在多人协作开发同一个项目时,模块解耦尤为重要,同时,公共基础库的功能 ...

  8. GBK和UTF8的区别

    GBK的文字编码是双字节来表示的,即不论中.英文字符均使用双字节来表示,只不过为区分中文,将其最高位都定成1. UTF-8编码则是用以解决国际上字符的一种多字节编码,它对英文使用8位(即一个字节),中 ...

  9. jmeter响应断言

    jmeter提供了很多种断言,本文我就介绍下我们经常使用的响应断言! 响应断言 :对服务器的响应进行断言校验 (1).应用范围Apply to : Main sample and sub-sample ...

  10. springboot 入门一 hello world!

    微服务框架springboot,目的是用来简化新Spring应用的初始搭建以及开发过程.简化的代价,就是约定俗成很多规则,比如默认读取的配置文件名是application.properties 必需在 ...