862B Mahmoud and Ehab and the bipartiteness (黑白染色)
来源:互联网 发布:淘宝联盟省钱购买 编辑:程序博客网 时间:2024/06/01 08:05
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 into2 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.
31 21 3
0
51 22 33 44 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).
题意:
给你n个点n-1条边的树,问你最多加几条边,让这个图不存在环,重边,并且是一个二分图
思路:由于题目限制,我们可以首先对他进行黑白染色,把这棵树变成一个二分图,那么我们就能得到两个点集,假设我们的点集分别是 ans1,ans2,那么我们总共可以连接 ans1*ans2条边,然后减去我们已经有的边n+1,就是答案,当时在写的时候 ,题意读不懂,,,卒。。。比较坑的一点是需要开longlong,而且点数为1e5所以需要用链式向前星去存图
#include<iostream>
#includ<stdio.h>#include<algorithm>#include<string.h>using namespace std;const int maxn = 1e6;struct node{int to,next;}edg[maxn];int cnt=0,head[maxn*2],vis[2*maxn];void add(int u,int v){edg[cnt].to=v;edg[cnt].next=head[u]++;head[u]=cnt++;}void dfs(int x,int flag){for(int i=head[x];i!=-1;i=edg[i].next){int v=edg[i].to;if(vis[v]==-1){vis[v]=flag;dfs(v,!flag);}}}int main(){int n,a,b;cin>>n;cnt=0;memset(head,-1,sizeof(head));memset(vis,-1,sizeof(vis));for(int i=0;i<n-1;i++){cin>>a>>b;add(a,b);add(b,a);}dfs(1,0);long long ans1=0,ans2=0;for(int i=1;i<=n;i++){if(vis[i]==1){ans1++;}if(vis[i]==0){ans2++;}}long long ans=(ans1*ans2-n+1);cout<<ans;}
- 862B Mahmoud and Ehab and the bipartiteness (黑白染色)
- Codeforces 862 B Mahmoud and Ehab and the bipartiteness(二分图染色)
- Round #435 (Div. 2)B. Mahmoud and Ehab and the bipartiteness(二分图染色)
- codeforces 862B B. Mahmoud and Ehab and the bipartiteness
- Codeforce 862B Mahmoud and Ehab and the bipartiteness (二分图染色 + 链式前向星)
- codeforces 862B. Mahmoud and Ehab and the bipartiteness
- Codeforces Round #435 (Div. 2)B. Mahmoud and Ehab and the bipartiteness(二分图,染色法)
- codeforces 862B (二分图 dfs )之 Mahmoud and Ehab and the bipartiteness
- CF 863.B Mahmoud and Ehab and the bipartiteness【DFS】
- Mahmoud and Ehab and the bipartiteness CodeForces
- codeforces Mahmoud and Ehab and the bipartiteness
- Codeforces Round #435 (Div. 2)B. Mahmoud and Ehab and the bipartiteness(补)
- Codeforces Round #435 (Div. 2)-B. Mahmoud and Ehab and the bipartiteness
- Codeforces Round #435 (Div. 2) B. Mahmoud and Ehab and the bipartiteness
- 862A Mahmoud and Ehab and the MEX (水)
- CF 862C Mahmoud and Ehab and the xor 构造
- codeforces 862A Mahmoud and Ehab and the MEX
- Codeforces 862C. Mahmoud and Ehab and the xor
- 4.dubbo源码分析 之 与Spring 集成
- eclipse4.7上的hibernate tools的离线安装问题。
- Spring IOC/BeanFactory/ApplicationContext的工作流程/实现原理/初始化/依赖注入源码详解
- 【练习】Mybatis简单配置(NetBeans下)
- 宏定义中的 ## ... __VA_ARGS__
- 862B Mahmoud and Ehab and the bipartiteness (黑白染色)
- Android使用iconfont
- TCP 三次握手和四次分手简析
- C++调用libusb,继电器(usbrelay)开发
- sublime text3 Build3143 破解
- binary string codeforces 862D 交互题,二分答案
- Node.JS——基础知识
- C语言——字符串指针-strcpy
- L1正则和L2正则的比较分析详解