Mahmoud and Ehab and the bipartiteness (Codeforces 862B)
Tags: Others, trees

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 ≤ n, u ≠ 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.

Note: Tree definition: https://en.wikipedia.org/wiki/Tree_(graph_theory)Bipartite graph definition: https://en.wikipedia.org/wiki/Bipartite_graphIn 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). 

Sample Inputs:
3
1 2
1 3

5
1 2
2 3
3 4
4 5

Sample Outputs:
0

2

Solution (Python):
import sys
from collections import deque
input = sys.stdin.readline
from math import *

def mrd():
    return [int(x) for x in input().split()]

def rd():
    return int(input())
MAXN = 2 * 10 ** 5 + 5
INF = 10 ** 16 * 2
mod = 10 ** 9 + 7

def solve():
    n = rd()
    adj = [[] for _ in range(n + 1)]
    for _ in range(n - 1):
        (u, v) = mrd()
        adj[u].append(v)
        adj[v].append(u)
    cnt = [0, 0]

    def dfs():
        q = deque()
        q.append((1, 0, 0))
        while q:
            (u, fa, op) = q.pop()
            cnt[op] += 1
            for v in adj[u]:
                if v == fa:
                    continue
                q.append((v, u, op ^ 1))
    dfs()
    print(cnt[0] * cnt[1] - (n - 1))
if __name__ == '__main__':
    solve()

Solution (Python):
import sys
from collections import deque
input = sys.stdin.readline
from math import *

def mrd():
    return [int(x) for x in input().split()]

def rd():
    return int(input())
MAXN = 2 * 10 ** 5 + 5
INF = 10 ** 16 * 2
mod = 10 ** 9 + 7

def solve():
    n = rd()
    adj = [[] for _ in range(n + 1)]
    for _ in range(n - 1):
        (a, b) = mrd()
        adj[a].append(b)
        adj[b].append(a)
    cnt = [0, 0]

    def dfs():
        q = deque()
        q.append((1, 0, 0))
        while q:
            (a, fa, op) = q.pop()
            cnt[op] += 1
            for b in adj[a]:
                if b == fa:
                    continue
                q.append((b, a, op ^ 1))
    dfs()
    print(cnt[0] * cnt[1] - (n - 1))
if __name__ == '__main__':
    solve()

Solution (Python):
import sys
from collections import deque
input = sys.stdin.readline
from math import *

def mrd():
    return [int(x) for x in input().split()]

def rd():
    return int(input())
MAXN = 2 * 10 ** 5 + 5
INF = 10 ** 16 * 2
mod = 10 ** 9 + 7

def solve():
    n = rd()
    adj = [[] for _ in range(n + 1)]
    for _ in range(n - 1):
        (u, v) = mrd()
        adj[u].append(v)
        adj[v].append(u)
    cnt = [0, 0]

    def dfs():
        q = deque()
        q.append((1, 0, 0))
        while q:
            (u, fa, op) = q.pop()
            cnt[op] += 1
            for v in adj[u]:
                if v == fa:
                    continue
                q.append((v, u, op ^ 1))
    dfs()
    print(cnt[0] * cnt[1] - (n - 1))
if __name__ == '__main__':
    solve()