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 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?≤?nu?≠?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

31 21 3

Output

0

Input

51 22 33 44 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).

题意:给一个n个节点的,n-1条边,现在规定这个图拆成二分图,然后让你添加边,使他依然是二分图,问最多可以添加多少

思路:首先看下图

原文地址:https://www.cnblogs.com/Lis-/p/9807433.html

时间: 2024-11-05 12:29:44

E - Mahmoud and Ehab and the bipartiteness CodeForces - 862B (dfs黑白染色)的相关文章

Codeforces 862B - Mahmoud and Ehab and the bipartiteness

862B - Mahmoud and Ehab and the bipartiteness 思路:先染色,然后找一种颜色dfs遍历每一个点求答案. 代码: #include<bits/stdc++.h> using namespace std; #define ll long long #define pb push_back #define mem(a,b) memset(a,b,sizeof(a)) const int N=1e6+5; bool color[N]; vector<i

codeforces 862B B. Mahmoud and Ehab and the bipartiteness

http://codeforces.com/problemset/problem/862/B 题意: 给出一个有n个点的二分图和n-1条边,问现在最多可以添加多少条边使得这个图中不存在自环,重边,并且此图还是一个二分图. 思路: 想得比较复杂了....其实既然已经给出了二分图并且有n-1条边,那么我们就一定可以用染色法对每一个点进行染色,从而将点划分为两个集合,然后从两个集合中分别任意选择一个点连边就行了. 一开始考虑二分图的基本属性是不存在奇数条边的环...并不需要这样,因为两个集合是分开的,

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<

E. Mahmoud and Ehab and the function Codeforces Round #435 (Div. 2)

http://codeforces.com/contest/862/problem/E 二分答案 一个数与数组中的哪个数最接近: 先对数组中的数排序,然后lower_bound 1 #include <cstdio> 2 #include <cstdlib> 3 #include <cmath> 4 #include <cstring> 5 #include <time.h> 6 #include <string> 7 #includ

Codeforces 862C - Mahmoud and Ehab and the xor

862C - Mahmoud and Ehab and the xor 思路:找两对异或后等于(1<<17-1)的数(相当于加起来等于1<<17-1),两个再异或一下就变成0了,0异或x等于x.所以只要把剩下的异或起来变成x就可以了.如果剩下来有3个,那么,这3个数可以是x^i^j,i,j. 代码: #include<bits/stdc++.h> using namespace std; #define ll long long #define pb push_back

CF 862C Mahmoud and Ehab and the xor(异或)

题目链接:http://codeforces.com/problemset/problem/862/C 题目: Mahmoud and Ehab are on the third stage of their adventures now. As you know, Dr. Evil likes sets. This time he won't show them any set from his large collection, but will ask them to create a n

Codeforces 405E DFS

这个题目要求把一个无向连通图里面的所有边,分成 两个一对,只能出现一次,而且一对边必须是连在一起的,点可以复用  但边不可复用 可解条件很易得,因为图是连通的,只要边数为偶数即可. 一开始我借着做欧拉回路的方法,直接DFS暴搜,沿路做标记,遇到未标记的连续两条边 输出即可 不过 事实证明这个算法是错的 暴搜能成立只是建立在图上的边可以存在很多个边对里,但肯定有图不满足这种条件 其实解决方法也就是在DFS的基础上对特殊边进行下考虑即可 即每次对某个点,对子节点进行dfs,如果发现子节点下面有落单的

Codeforces 959 D Mahmoud and Ehab and another array construction task

Discription Mahmoud has an array a consisting of n integers. He asked Ehab to find another arrayb of the same length such that: b is lexicographically greater than or equal to a. bi?≥?2. b is pairwise coprime: for every 1?≤?i?<?j?≤?n, bi and bj are c

Codeforces Round #435 (Div. 2) D. Mahmoud and Ehab and the binary string[二分]

题目:http://codeforces.com/problemset/problem/862/D 题意:交互题,询问15次以内Hamming distance,输出一个二进制串里任意一个0或1的位置 题解:极简单的二分,从最后一位先判断一个,然后二分 根据上次和本次的距离差是否等于二分长度判断在左端还是右端有需要寻找的值寻找另一个. 1 #define _CRT_SECURE_NO_DEPRECATE 2 #pragma comment(linker, "/STACK:102400000,10