CF 863.B Mahmoud and Ehab and the bipartiteness【DFS】
来源:互联网 发布:accorpus数据库 编辑:程序博客网 时间:2024/06/05 15:14
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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 .
Input
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
Output one integer — the maximum number of edges that Mahmoud and Ehab can add to the tree while fulfilling the conditions.
Examples
input
3
1 2
1 3
output
0
input
5
1 2
2 3
3 4
4 5
output
2
Note
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).
#include<iostream>#include<cstring>#include<vector>using namespace std;vector<int> v[100005];long long int cnt[2];void dfs(int node, int pnode, int color){ cnt[color]++; for (int i = 0; i < v[node].size(); i++) { if (v[node][i] != pnode) dfs(v[node][i],node, !color); }}int main(){ int n; cin >> n; for(int i = 1; i < n; i++) { int a, b; cin >> a >> b; v[a].push_back(b); v[b].push_back(a); } dfs(1, 0, 0); printf("%I64d", cnt[0] * cnt[1] - n + 1); return 0;}
- CF 863.B Mahmoud and Ehab and the bipartiteness【DFS】
- codeforces 862B B. Mahmoud and Ehab and the bipartiteness
- codeforces 862B. Mahmoud and Ehab and the bipartiteness
- codeforces 862B (二分图 dfs )之 Mahmoud and Ehab and the bipartiteness
- 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
- 862B 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(补)
- Round #435 (Div. 2)B. Mahmoud and Ehab and the bipartiteness(二分图染色)
- Codeforces Round #435 (Div. 2)B. Mahmoud and Ehab and the bipartiteness(二分图,染色法)
- Codeforces 862 B Mahmoud and Ehab and the bipartiteness(二分图染色)
- Codeforce 862B Mahmoud and Ehab and the bipartiteness (二分图染色 + 链式前向星)
- CF C. Mahmoud and Ehab and the xor
- CF 862C Mahmoud and Ehab and the xor 构造
- CF 862A. Mahmoud and Ehab and the MEX
- Mahmoud and Ehab and the xor CodeForces
- ssh login server without password
- 太上感应篇0032
- 太上感应篇0033
- 太上感应篇0034
- Java-反射(学习笔记)
- CF 863.B Mahmoud and Ehab and the bipartiteness【DFS】
- [bzoj2709][Violet 1]迷宫花园(二分+spfa)
- CSS基础(三)--清除浮动的影响
- bzoj 1830: [AHOI2008]Y型项链 贪心
- CF 862 C. Mahmoud and Ehab and the xor【异或】
- 静态成员变量和静态成员函数在C++类中的作用
- LeetCode上面一道算法题目:79. Word Search
- 数据库
- centos7.3+ mysql5.6 + mysqludf安装