Codeforces Round #435 (Div. 2)B. Mahmoud and Ehab and the bipartiteness(补)

来源:互联网 发布:福建网络服务公司 编辑:程序博客网 时间:2024/05/20 13:20

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

题目大概:

有一颗二分树,给出一些边,问可以添加的最大变的数量。

思路:

二分图的最大匹配问题,分为两个集合x y,集合内的边不会相连,两集合的最大边数是  x内点的数量*y内点的数量。已经给出了n-1,所以最大边数是 x内点的数量*y内点的数量-n+1.

这个类型的题,我记得前几次网络赛中遇见过,那一个题没有拐弯抹角,直接就明说了两个集合,两个集合可以相互连边,具体的忘了,能够找到规律。

代码:

#include <iostream>#include <cstdio>#include <vector>using namespace std;vector<int>a[100005];long long b[2];int dfs(int poin,int rpoin,int co){    b[co]++;    for(int i=0;i<a[poin].size();i++)    {        if(a[poin][i]!=rpoin)        dfs(a[poin][i],poin,!co);    }return 0;}int main(){   long long n;   scanf("%I64d",&n);   for(int i=1;i<n;i++)   {       int q,w;       scanf("%d%d",&q,&w);       a[q].push_back(w);       a[w].push_back(q);   }   dfs(1,0,0);   printf("%I64d\n",b[0]*b[1]-n+1);    return 0;}




阅读全文
0 0