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,但是要求是 ...
随机推荐
- 设计模式原则(2)--Liskov Substitution Principle(LSP)--里氏替换原则
1.定义: 所有引用基类(父类)的地方必须能透明地使用其子类的对象.这一原则与继承紧密相关.如果对每一个类型为 T1的对象 o1,都有类型为 T2 的对象o2,使得以 T1定义的所有程序 P 在所有的 ...
- async简单使用
node的异步io虽然好用,但是控制异步流程确实一个比较麻烦的事情,比如在爬虫中控制并发数量,避免并发过大导致网站宕机或被加入黑名单.因此需要一个工具来控制并发,这个工具可以自己写或者使用async( ...
- Yield Usage Understanding
When would I use Task.Yield()? http://stackoverflow.com/questions/22645024/when-would-i-use-task-yie ...
- ACL权限控制列表
1.查看文件系统是否支持ACL (xfs文件系统强制开启ACL,如果该分区已格式化为xfs,在使用dumpe2fs会看不到superblocks信息)zheng@ubuntu16:~$ sudo du ...
- 【Centos7】安装mongodb 使用yum源
根据mongodb官网提供的教程安装: 1.创建mongdb-org-3.4.repo 2.使得selinux的config为disabled 3.yum -y install mongodbxxxx ...
- 不安装vc2015 Redistributable解决api-ms-win-crt-runtime-l1-1-0.dll丢失错误
一.背景 最近用到python下的cx_Oracle模块,开发环境中测试正常,用cx_freeze打包后,到用户机器上一部署,各种奇奇怪怪的问题频出,运行环境如下: a.开发环境:64位win10操作 ...
- 借助扩展事件查看SQL 2016备份和还原操作的内幕
当遇到备份或者还原操作占用较长时间时,很多人会问: 备份/还原是不是僵死了?要不要kill掉,再重来? 到底是哪一个部分的操作占用较长时间? 到底现在进行到什么阶段了? 在SQL 2016 之前,要回 ...
- 七牛云数据存储Demo
利用七牛云的Python SDK实现文件上传.下载等操作. import os import requests import qiniu from qiniu import BucketManager ...
- Cannot convert 0 of type class java.lang.Integer to class java.lang.Boolean
org.apache.catalina.core.ApplicationDispatcher invoke SEVERE: Servlet.service() for servlet jsp thre ...
- HTML的基本标签
整理一下这一周学习的一些知识. 首先是一些基本标签. <!DOCTYPE HTML><html> 文档类型声明: 让浏览器,按照html5的标准对代码进行解释与执行.文档类型声 ...