Coderfroces 862 B . Mahmoud and Ehab and the bipartiteness
Mahmoud and Ehab continue their adventures! As everybody in the evil land knows, Dr. Evil likes bipartite graphs, especially trees.
A tree is a connected acyclic graph. A bipartite graph is a graph, whose vertices can be partitioned into 2 sets in such a way, that for each edge (u, v) that belongs to the graph, u and v belong to different sets. You can find more formal definitions of a tree and a bipartite graph in the notes section below.
Dr. Evil gave Mahmoud and Ehab a tree consisting of n nodes and asked them to add edges to it in such a way, that the graph is still bipartite. Besides, after adding these edges the graph should be simple (doesn't contain loops or multiple edges). What is the maximum number of edges they can add?
A loop is an edge, which connects a node with itself. Graph doesn't contain multiple edges when for each pair of nodes there is no more than one edge between them. A cycle and a loop aren't the same .
The first line of input contains an integer n — the number of nodes in the tree (1 ≤ n ≤ 105).
The next n - 1 lines contain integers u and v (1 ≤ u, v ≤ n, u ≠ v) — the description of the edges of the tree.
It's guaranteed that the given graph is a tree.
Output one integer — the maximum number of edges that Mahmoud and Ehab can add to the tree while fulfilling the conditions.
3
1 2
1 3
0
5
1 2
2 3
3 4
4 5
2
Tree definition: https://en.wikipedia.org/wiki/Tree_(graph_theory)
Bipartite graph definition: https://en.wikipedia.org/wiki/Bipartite_graph
In the first test case the only edge that can be added in such a way, that graph won't contain loops or multiple edges is (2, 3), but adding this edge will make the graph non-bipartite so the answer is 0.
In the second test case Mahmoud and Ehab can add edges (1, 4) and (2, 5).
一颗子树,将其变为二分图,最大可添加的边数。将所有结点标记为1或0,则二分图的最大边数为pos=ans0*ans1,所以答案就为pos-n+1;
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#define lowbit(x) (x&(-x))
#define max(x,y) (x>=y?x:y)
#define min(x,y) (x<=y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.141592653589793238462
#define ios() ios::sync_with_stdio(true)
#define INF 1044266558
#define mem(a) (memset(a,0,sizeof(a)))
typedef long long ll;
vector<int>v[];
int vis[][],x,y;
ll ans=,n;
void dfs(int x)
{
vis[x][]=;
for(int i=;i<v[x].size();i++)
{
if(vis[v[x][i]][]) continue;
vis[v[x][i]][]=vis[x][]^;
if(vis[v[x][i]][]==) ans++;
dfs(v[x][i]);
}
}
int main()
{
scanf("%lld",&n);
for(int i=;i<n-;i++)
{
scanf("%d%d",&x,&y);
v[x].push_back(y);
v[y].push_back(x);
}
memset(vis,,sizeof(vis));
dfs();
printf("%lld\n",(n-ans)*ans-n+);
return ;
}
Coderfroces 862 B . Mahmoud and Ehab and the bipartiteness的更多相关文章
- Coderfroces 862 C. Mahmoud and Ehab and the xor
C. Mahmoud and Ehab and the xor Mahmoud and Ehab are on the third stage of their adventures now. As ...
- Codeforces 862B - Mahmoud and Ehab and the bipartiteness
862B - Mahmoud and Ehab and the bipartiteness 思路:先染色,然后找一种颜色dfs遍历每一个点求答案. 代码: #include<bits/stdc+ ...
- 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 862 C. Mahmoud and Ehab and the xor(构造)
题目链接:http://codeforces.com/contest/862/problem/C 题解:一道简单的构造题,一般构造题差不多都考自己脑补,脑洞一开就过了 由于数据x只有1e5,但是要求是 ...
- codeforces 862B B. Mahmoud and Ehab and the bipartiteness
http://codeforces.com/problemset/problem/862/B 题意: 给出一个有n个点的二分图和n-1条边,问现在最多可以添加多少条边使得这个图中不存在自环,重边,并且 ...
- 【Codeforces Round #435 (Div. 2) B】Mahmoud and Ehab and the bipartiteness
[链接]h在这里写链接 [题意] 让你在一棵树上,加入尽可能多的边. 使得这棵树依然是一张二分图. [题解] 让每个节点的度数,都变成二分图的对方集合中的点的个数就好. [错的次数] 0 [反思] 在 ...
- CodeForces - 862B Mahmoud and Ehab and the bipartiteness(二分图染色)
题意:给定一个n个点的树,该树同时也是一个二分图,问最多能添加多少条边,使添加后的图也是一个二分图. 分析: 1.通过二分图染色,将树中所有节点分成两个集合,大小分别为cnt1和cnt2. 2.两个集 ...
- 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 ...
随机推荐
- iOS基础UI控件介绍-Swift版
iOS基础UI控件总结 iOS基础控件包括以下几类: 1.继承自NSObject:(暂列为控件) UIColor //颜色 UIImage //图像 2.继承自UIView: 只能相应手势UIGest ...
- dashboard安装
1,安装程序包 # yum install -y openstack-dashboard 2,修改配置文件 # vim /etc/openstack-dashboard/local_settings ...
- MarkDown 图片大小问题
本系列文章由 @YhL_Leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/50099843 MarkDown里显示图 ...
- hadoop-13-root ssh无密码登陆
hadoop-13-root ssh无密码登陆 生产机器禁止ROOT远程SSH登录: vi /etc/ssh/sshd_config 把 PermitRootLogin yes 改为 PermitRo ...
- 基于SIP和RTP协议的开源VOIP之QuteCom简单介绍
**************************************************************************************************** ...
- invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause
Column 'dbo.tbm_vie_View.ViewID' is invalid in the select list because it is not contained in either ...
- centos7 配置redis
文件上传 yum -y install lrzsz 安装redis部署前操作 同时下载redis-.tar.gz安装包 yum -y install gcc-c++ yum -y install tc ...
- 机器学习规则:ML工程最佳实践----rules_of_ml section 1【翻译】
作者:黄永刚 机器学习规则:ML工程最佳实践 本文旨在指引具有机器学习基础知识的工程师等人,更好的从机器学习的实践中收益.介绍一些应用机器学习需要遵循的规则,类似于Google C++ 风格指南等流行 ...
- 日志文件支持unicode字符的做法
作者:朱金灿 来源:http://blog.csdn.net/clever101 开发的程序兼容多字节字符集和unicode字符集,最近发现一个问题,在unicode字符集下输出的日志文件是乱码的.显 ...
- 当fastJson邂逅大写字段时
在项目中遇到了一件令人头疼的事.使用fastJson反序列化时下面的Json时,得到对象属性总为null(如下图),可能细心的朋友一看就知道问题出在哪里,没错!问题就出在返回的字段首字母给大写了.fa ...