{"problem":{"name":"B. Mahmoud and Ehab and the bipartiteness","description":{"content":"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, ","description_type":"Markdown"},"platform":"Codeforces","limit":{"time_limit":2000,"memory_limit":262144},"difficulty":"None","is_remote":true,"is_sync":true,"sync_url":null,"sign":"CF862B"},"statements":[{"statement_type":"Markdown","content":"Mahmoud and Ehab continue their adventures! As everybody in the evil land knows, Dr. Evil likes bipartite graphs, especially trees.\n\nA 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.\n\nDr. 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?\n\nA 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** .\n\n## Input\n\nThe first line of input contains an integer _n_ — the number of nodes in the tree (1 ≤ _n_ ≤ 105).\n\nThe next _n_ - 1 lines contain integers _u_ and _v_ (1 ≤ _u_, _v_ ≤ _n_, _u_ ≠ _v_) — the description of the edges of the tree.\n\nIt's guaranteed that the given graph is a tree.\n\n## Output\n\nOutput one integer — the maximum number of edges that Mahmoud and Ehab can add to the tree while fulfilling the conditions.\n\n[samples]\n\n## Note\n\nTree definition: [https://en.wikipedia.org/wiki/Tree_(graph_theory)](https://en.wikipedia.org/wiki/Tree_(graph_theory))\n\nBipartite graph definition: [https://en.wikipedia.org/wiki/Bipartite_graph](https://en.wikipedia.org/wiki/Bipartite_graph)\n\nIn 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.\n\nIn the second test case Mahmoud and Ehab can add edges (1, 4) and (2, 5).","is_translate":false,"language":"English"},{"statement_type":"Markdown","content":"Mahmoud 和 Ehab 继续他们的冒险！正如邪恶大陆中的每个人都知道的那样，Dr. Evil 喜欢二分图，尤其是树。\n\n树是一种连通的无环图。二分图是一种图，其顶点可以被划分为 #cf_span[2] 个集合，使得对于图中的每条边 #cf_span[(u, v)]，#cf_span[u] 和 #cf_span[v] 分别属于不同的集合。你可以在下面的注释部分找到树和二分图的更正式定义。\n\nDr. Evil 给了 Mahmoud 和 Ehab 一棵包含 #cf_span[n] 个节点的树，并要求他们向其中添加边，使得图仍然是二分图。此外，添加这些边后，图必须是简单图（不包含自环或多重边）。他们最多能添加多少条边？\n\n自环是连接一个节点与自身的边。当每对节点之间至多只有一条边时，图不包含多重边。*环和自环不是同一概念*。\n\n输入的第一行包含一个整数 #cf_span[n] —— 树中的节点数（#cf_span[1 ≤ n ≤ 105]）。\n\n接下来的 #cf_span[n - 1] 行每行包含两个整数 #cf_span[u] 和 #cf_span[v]（#cf_span[1 ≤ u, v ≤ n]，#cf_span[u ≠ v]）—— 树的边的描述。\n\n保证给定的图是一棵树。\n\n请输出一个整数 —— Mahmoud 和 Ehab 在满足条件的前提下，能向树中添加的最大边数。\n\n树的定义：https://en.wikipedia.org/wiki/Tree_(graph_theory)\n\n二分图的定义：https://en.wikipedia.org/wiki/Bipartite_graph\n\n在第一个测试用例中，唯一可以添加且不会产生自环或多重边的边是 #cf_span[(2, 3)]，但添加这条边会使图不再是二分图，因此答案为 0。\n\n在第二个测试用例中，Mahmoud 和 Ehab 可以添加边 #cf_span[(1, 4)] 和 #cf_span[(2, 5)]。\n\n## Input\n\n输入的第一行包含一个整数 #cf_span[n] —— 树中的节点数（#cf_span[1 ≤ n ≤ 105]）。接下来的 #cf_span[n - 1] 行每行包含两个整数 #cf_span[u] 和 #cf_span[v]（#cf_span[1 ≤ u, v ≤ n]，#cf_span[u ≠ v]）—— 树的边的描述。保证给定的图是一棵树。\n\n## Output\n\n请输出一个整数 —— Mahmoud 和 Ehab 在满足条件的前提下，能向树中添加的最大边数。\n\n[samples]\n\n## Note\n\n树的定义：https://en.wikipedia.org/wiki/Tree_(graph_theory)\n二分图的定义：https://en.wikipedia.org/wiki/Bipartite_graph\n在第一个测试用例中，唯一可以添加且不会产生自环或多重边的边是 #cf_span[(2, 3)]，但添加这条边会使图不再是二分图，因此答案为 0。\n在第二个测试用例中，Mahmoud 和 Ehab 可以添加边 #cf_span[(1, 4)] 和 #cf_span[(2, 5)]。","is_translate":true,"language":"Chinese"},{"statement_type":"Markdown","content":"**Definitions**  \nLet $ G = (V, E) $ be a tree with $ n = |V| $ vertices and $ n - 1 $ edges.  \nSince $ G $ is a tree, it is bipartite. Let $ (L, R) $ be a bipartition of $ V $ such that every edge in $ E $ connects a vertex in $ L $ to one in $ R $.  \n\n**Constraints**  \n1. $ 1 \\leq n \\leq 10^5 $  \n2. The graph is a tree (connected, acyclic, undirected).  \n3. Added edges must:  \n   - Preserve bipartiteness.  \n   - Be simple (no loops, no multiple edges).  \n\n**Objective**  \nCompute the maximum number of edges that can be added to $ G $ such that the resulting graph remains simple and bipartite.  \n\nThe maximum number of edges in a simple bipartite graph with bipartition $ (L, R) $ is $ |L| \\cdot |R| $.  \nSince $ G $ already has $ n - 1 $ edges, the maximum number of addable edges is:  \n$$\n|L| \\cdot |R| - (n - 1)\n$$","is_translate":false,"language":"Formal"}],"meta":{"iden":"CF862B","tags":["dfs and similar","graphs","trees"],"sample_group":[["3\n1 2\n1 3","0"],["5\n1 2\n2 3\n3 4\n4 5","2"]],"created_at":"2026-03-03 11:00:39"}}