codeforces Mahmoud and Ehab and the bipartiteness
来源:互联网 发布:网络代理兼职加盟 编辑:程序博客网 时间:2024/06/03 05:51
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 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 ofn 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 integersu 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,就是答案
代码:
#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<cmath>#include<algorithm>using namespace std;#define N 1000005struct data{ int to; int next;}p[N];int cnt=0;int fa[N<<1];int vis[N<<1];void add(int u,int v){ p[cnt].to=v; //指向的元素 p[cnt].next=fa[u]++; //下一个实际上是指向他的上一个,fa[]表示他的上一个所在位置 fa[u]=cnt++;}void dfs(int n,int f){ for(int i=fa[n]; i!=-1 ;i=p[i].next) { int k=p[i].to; if(vis[k]==-1) { vis[k]=f; dfs(k,!f); } }}int main(){ int n; cin>>n; memset(vis,-1,sizeof(vis)); memset(fa,-1,sizeof(fa)); for(int i=0;i<n-1;i++) { int a,b; cin>>a>>b; add(a,b); add(b,a); } dfs(1,0); long long a=0,b=0; for(int i=1;i<=n;i++) { if(vis[i]==1) a++; else if(vis[i]==0) b++; } long long ans=a*b-(n-1); cout<<ans<<endl; return 0;}
- Mahmoud and Ehab and the bipartiteness CodeForces
- codeforces Mahmoud and Ehab and the bipartiteness
- codeforces 862B B. Mahmoud and Ehab and the bipartiteness
- codeforces 862B. Mahmoud and Ehab and the bipartiteness
- CF 863.B Mahmoud and Ehab and the bipartiteness【DFS】
- 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(补)
- Codeforces Round #435 (Div. 2)B. Mahmoud and Ehab and the bipartiteness(二分图,染色法)
- codeforces 862B (二分图 dfs )之 Mahmoud and Ehab and the bipartiteness
- Codeforces 862 B Mahmoud and Ehab and the bipartiteness(二分图染色)
- Mahmoud and Ehab and the xor CodeForces
- Mahmoud and Ehab and the xor CodeForces
- Codeforces --- Mahmoud and Ehab and the MEX
- 862B Mahmoud and Ehab and the bipartiteness (黑白染色)
- Round #435 (Div. 2)B. Mahmoud and Ehab and the bipartiteness(二分图染色)
- Codeforces Round #435 A. Mahmoud and Ehab and the MEX
- Codeforces Round #435 A. Mahmoud and Ehab and the MEX
- 91. Decode Ways(M)
- vim使用技巧
- 行人再识别研究者
- java基础【5】
- 设计模式——速记
- codeforces Mahmoud and Ehab and the bipartiteness
- Linux下修改mysql密码以及忘记密码重置
- 优雅的Spring框架介绍
- BZOJ 4756 [Usaco2017 Jan]Promotion Counting dfs序+主席树
- 微信小程序-表单form
- jsp实现字母和数字验证码功能
- 程序时间复杂度的几种表示
- POJ1753:Flip Game(BFS、枚举、位运算)
- http如何使用POST,$ajax向provider传递数据并获取数据(以在浏览器上画圆为例)