CodeForces - 862B Mahmoud and Ehab and the bipartiteness(二分图染色)
题意:给定一个n个点的树,该树同时也是一个二分图,问最多能添加多少条边,使添加后的图也是一个二分图。
分析:
1、通过二分图染色,将树中所有节点分成两个集合,大小分别为cnt1和cnt2。
2、两个集合间总共可以连cnt1*cnt2条边,给定的是一个树,因此已经连了n-1条边,所以最多能连cnt1*cnt2-(n-1)条边。
3、注意输出。
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define lowbit(x) (x & (-x))
const double eps = 1e-8;
inline int dcmp(double a, double b){
if(fabs(a - b) < eps) return 0;
return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 100000 + 10;
const int MAXT = 10000 + 10;
using namespace std;
int color[MAXN];
vector<int> G[MAXN];
bool dfs(int v, int c){
color[v] = c;
int len = G[v].size();
for(int i = 0; i < len; ++i){
if(color[G[v][i]] == c) return false;
if(color[G[v][i]] == 0 && !dfs(G[v][i], -c)) return false;
}
return true;
}
int main(){
int n;
scanf("%d", &n);
int a, b;
for(int i = 0; i < n - 1; ++i){
scanf("%d%d", &a, &b);
G[a].push_back(b);
G[b].push_back(a);
}
for(int i = 1; i <= n; ++i){
if(color[i] == 0){
dfs(i, 1);
}
}
int cnt1 = 0;
int cnt2 = 0;
for(int i = 1; i <= n; ++i){
if(color[i] == 1) ++cnt1;
if(color[i] == -1) ++cnt2;
}
printf("%lld\n", (LL)cnt1 * (LL)cnt2 - (n - 1));
return 0;
}
CodeForces - 862B 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+ ...
- CF862B Mahmoud and Ehab and the bipartiteness 二分图染色判定
\(\color{#0066ff}{题目描述}\) 给出n个点,n-1条边,求再最多再添加多少边使得二分图的性质成立 \(\color{#0066ff}{输入格式}\) The first line ...
- Coderfroces 862 B . Mahmoud and Ehab and the bipartiteness
Mahmoud and Ehab and the bipartiteness Mahmoud and Ehab continue their adventures! As everybody in ...
- Codeforces 959D. Mahmoud and Ehab and another array construction task(构造, 简单数论)
Codeforces 959D. Mahmoud and Ehab and another array construction task 题意 构造一个任意两个数都互质的序列,使其字典序大等于a序列 ...
- 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 ...
- Codeforces 862A Mahmoud and Ehab and the MEX
传送门:CF-862A A. Mahmoud and Ehab and the MEX time limit per test 2 seconds memory limit per test 256 ...
- Codeforces 959F Mahmoud and Ehab and yet another xor task 线性基 (看题解)
Mahmoud and Ehab and yet another xor task 存在的元素的方案数都是一样的, 啊, 我好菜啊. 离线之后用线性基取check存不存在,然后计算答案. #inclu ...
- Codeforces 862C - Mahmoud and Ehab and the xor
862C - Mahmoud and Ehab and the xor 思路:找两对异或后等于(1<<17-1)的数(相当于加起来等于1<<17-1),两个再异或一下就变成0了 ...
- Codeforces 862D. Mahmoud and Ehab and the binary string (二分)
题目链接:Mahmoud and Ehab and the binary string 题意: 一道交互题,首先给出一个字符串的长度l.现在让你进行提问(最多15次),每次提问提出一个字符串,会返回这 ...
随机推荐
- MAC97A6检测
- js中各种类型转换为Boolean类型
数据类型 转换为true的值 转换为false的值 Boolean true false String 任何非空字符串 空字符串 Number 任何非零数字值(包括无穷大) 0和null ...
- 深入理解Java虚拟机-如何利用VisualVM对高并发项目进行性能分析
前面在学习JVM的知识的时候,一般都需要利用相关参数进行分析,而分析一般都需要用到一些分析的工具,因为一般使用IDEA,而VisualVM对于IDEA也不错,所以就选择VisualVM来分析JVM性能 ...
- core版本使用ef连接数据库(一)
参考 参考代码 sqlserver数据库:①Nuget: Microsoft.EntityFrameworkCore.SqlServer ORACLE数据库:①Nuget: Oracle.Entity ...
- LibreOJ #6001. 「网络流 24 题」太空飞行计划
\(\quad\) 与网络流有关的最值有三个:最大流,最小费用,最小割.这道题是最小割.想了好久,终于想明白最小割应该怎么用. \(\quad\) 先找出矛盾的事物.在这道题中,两件事是矛盾的:做实验 ...
- 吴裕雄--天生自然PYTHON爬虫:使用BeautifulSoup解析中国旅游网页数据
import requests from bs4 import BeautifulSoup url = "http://www.cntour.cn/" strhtml = requ ...
- c# 事件3
1.什么是事件,使对象或者类具有通知功能的成员.//为了解决字段在外部被滥用,推出了事件 事件的功能能=通知+可选的事件参数(具体的详细信息,包括谁发送了消息,发送的什么消息) 使用:用于对象或者类件 ...
- MyBatis 学习二之简单练习巩固
1.新建一个maven项目并在pom.xml中添加依赖 2.项目架构 配置文件:SqlMapConfig.xml <?xml version="1.0" encoding ...
- 解决css中display:inline-block的兼容问题
*display:inline; *zoom:1; 不多说,ie6/7直接在元素添加以上的属性即可.
- 「BJWC2012」冻结
传送门 Luogu 解题思路 分层图最短路,层与层之间的边的边权减半,然后就是板子了. 细节注意事项 咕咕咕. 参考代码 #include <algorithm> #include < ...