862B.md (2199B)
1 Source: https://codeforces.com/problemset/problem/862/B 2 3 B. Mahmoud and Ehab and the bipartiteness 4 5 time limit per test 6 7 2 seconds 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 Mahmoud and Ehab continue their adventures! As everybody in the evil land knows, Dr. Evil likes bipartite graphs, especially trees. 22 23 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. 24 25 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? 26 27 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 . 28 29 Input 30 31 The first line of input contains an integer *n* — the number of nodes in the tree (1 ≤ *n* ≤ 105). 32 33 The next *n* - 1 lines contain integers *u* and *v* (1 ≤ *u*, *v* ≤ *n*, *u* ≠ *v*) — the description of the edges of the tree. 34 35 It's guaranteed that the given graph is a tree. 36 37 Output 38 39 Output one integer — the maximum number of edges that Mahmoud and Ehab can add to the tree while fulfilling the conditions. 40 41 Examples 42 43 Input 44 45 ``` 46 3 47 1 2 48 1 3 49 ``` 50 51 Output 52 53 ``` 54 0 55 ``` 56 57 Input 58 59 ``` 60 5 61 1 2 62 2 3 63 3 4 64 4 5 65 ``` 66 67 Output 68 69 ``` 70 2 71 ``` 72 73 Note 74 75 Tree definition: <https://en.wikipedia.org/wiki/Tree_(graph_theory)> 76 77 Bipartite graph definition: <https://en.wikipedia.org/wiki/Bipartite_graph> 78 79 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. 80 81 In the second test case Mahmoud and Ehab can add edges (1, 4) and (2, 5).