CF862B.Mahmoud and Ehab and the bipartiteness

普及-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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 .

马哈茂德和埃哈布继续他们的冒险!正如邪恶之地的每个人所知,邪恶博士钟爱二分图,尤其是树。

树是一种连通且无环的图。二分图是一种图,其顶点可被划分为两个集合,使得图中每条边 (u, v)(u, v) 的两个端点 uu 和 vv 分别属于不同的集合。关于树与二分图更严格的定义,请参见下方“注释”部分。

邪恶博士给了马哈茂德和埃哈布一棵包含 nn 个节点的树,并要求他们在保持图仍为二分图的前提下添加若干条边。此外,添加边之后的图必须是简单图(即不含自环或重边)。他们最多能添加多少条边?

自环是指连接一个节点与其自身的边;当任意一对节点之间至多只有一条边时,该图即不含重边。注意:环(cycle)与自环(loop)并非同一概念。

输入格式

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.

输入的第一行包含一个整数 nn —— 树中节点的数量(1 ≤ n ≤ 1051 \leq n \leq 10^5)。

接下来的 n − 1n - 1 行每行包含两个整数 uu 和 vv(1 ≤ u, v ≤ n1 \leq u, v \leq n,且 u ≠ vu \neq v)—— 描述树的边。

保证给定的图是一棵树。

输出格式

Output one integer — the maximum number of edges that Mahmoud and Ehab can add to the tree while fulfilling the conditions.

输出一个整数——Mahmoud 和 Ehab 在满足条件的前提下可以向该树添加的最多边数。

输入输出样例

  • 输入#1

    3
    1 2
    1 3

    输出#1

    0
  • 输入#2

    5
    1 2
    2 3
    3 4
    4 5

    输出#2

    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).

树的定义:https://en.wikipedia.org/wiki/Tree_(graph_theory)

二分图的定义:https://en.wikipedia.org/wiki/Bipartite_graph

在第一个测试用例中,唯一能添加且不产生环或重边的边是 (2, 3)(2,\,3),但添加该边会使图变为非二分图,因此答案为 00。

在第二个测试用例中,Mahmoud 和 Ehab 可以添加边 (1, 4)(1,\,4) 和 (2, 5)(2,\,5)。

输入解题思路,AI测评打分。不知道怎么写?

首页