Edge Weight Assignment (Codeforces 1338B)
Tags: Greedy, bitmasks

You have unweighted tree of $$$n$$$ vertices. You have to assign a positive weight to each edge so that the following condition would hold:  For every two different leaves $$$v_{1}$$$ and $$$v_{2}$$$ of this tree, bitwise XOR of weights of all edges on the simple path between $$$v_{1}$$$ and $$$v_{2}$$$ has to be equal to $$$0$$$. Note that you can put very large positive integers (like $$$10^{(10^{10})}$$$).It's guaranteed that such assignment always exists under given constraints. Now let's define $$$f$$$ as the number of distinct weights in assignment.  In this example, assignment is valid, because bitwise XOR of all edge weights between every pair of leaves is $$$0$$$. $$$f$$$ value is $$$2$$$ here, because there are $$$2$$$ distinct edge weights($$$4$$$ and $$$5$$$). In this example, assignment is invalid, because bitwise XOR of all edge weights between vertex $$$1$$$ and vertex $$$6$$$ ($$$3, 4, 5, 4$$$) is not $$$0$$$. What are the minimum and the maximum possible values of $$$f$$$ for the given tree? Find and print both.

Input: The first line contains integer $$$n$$$ ($$$3 \le n \le 10^{5}$$$) — the number of vertices in given tree.The $$$i$$$-th of the next $$$n-1$$$ lines contains two integers $$$a_{i}$$$ and $$$b_{i}$$$ ($$$1 \le a_{i} \lt b_{i} \le n$$$) — it means there is an edge between $$$a_{i}$$$ and $$$b_{i}$$$. It is guaranteed that given graph forms tree of $$$n$$$ vertices.

Output: Print two integers — the minimum and maximum possible value of $$$f$$$ can be made from valid assignment of given tree. Note that it's always possible to make an assignment under given constraints.

Note: In the first example, possible assignments for each minimum and maximum are described in picture below. Of course, there are multiple possible assignments for each minimum and maximum.   In the second example, possible assignments for each minimum and maximum are described in picture below. The $$$f$$$ value of valid assignment of this tree is always $$$3$$$.   In the third example, possible assignments for each minimum and maximum are described in picture below. Of course, there are multiple possible assignments for each minimum and maximum.   

Sample Inputs:
6
1 3
2 3
3 4
4 5
5 6

6
1 3
2 3
3 4
4 5
4 6

7
1 2
2 7
3 4
4 7
5 6
6 7

Sample Outputs:
1 4

3 3

1 6

Solution (Python):
import sys
from collections import *
input = lambda : sys.stdin.readline().rstrip()
sint = lambda : int(input())
mint = lambda : map(int, input().split())
ints = lambda : list(map(int, input().split()))

def solve() -> None:
    n = sint()
    deg = [0] * n
    g = [[] for _ in range(n)]
    for _ in range(n - 1):
        (u, v) = mint()
        u -= 1
        v -= 1
        deg[u] += 1
        deg[v] += 1
        g[u].append(v)
        g[v].append(u)
    dis = [0] * n
    (mn, mx) = (0, n - 1)
    q = deque([(0, -1)])
    while q:
        (x, p) = q.popleft()
        for y in g[x]:
            if y == p:
                continue
            dis[y] = dis[x] + 1
            q.append((y, x))
            if deg[x] == 1:
                deg[y] = 0
            if deg[y] == 1:
                deg[x] = 0
        if deg[x] == 1:
            mn |= 1 << (dis[x] & 1)
            mx -= 1
    if mn < 3:
        mn = 1
    mx += deg.count(0)
    print(mn, mx)
solve()

Solution (Python):
from sys import stdin, stderr

def rl():
    return [int(w) for w in stdin.readline().split()]
(n,) = rl()
a = [[] for _ in range(n)]
for _ in range(n - 1):
    (ai, bi) = rl()
    a[ai - 1].append(bi - 1)
    a[bi - 1].append(ai - 1)
for v in range(n):
    if len(a[v]) == 1:
        f = [(v, a[v][0])]
        break
d = 2
f0 = 1
f1 = 0
while f:
    nf = []
    for (u, v) in f:
        df1 = 1
        for w in a[v]:
            if w != u:
                if len(a[w]) > 1:
                    nf.append((v, w))
                elif d > 2:
                    df1 = 2
                    if d % 2:
                        f0 = 3
        f1 += df1
    f = nf
    d += 1
print(f0, f1)

Solution (Python):
import math
import sys
from typing import List
import heapq

def bfs(g: List[List[int]], v: int) -> List[int]:
    d = [1000000] * len(g)
    deq = [v]
    d[v] = 0
    for i in range(len(g) - 1):
        u = deq[i]
        for t in g[u]:
            if d[t] == 1000000:
                d[t] = d[u] + 1
                deq.append(t)
    return d

def solve():
    n = int(input())
    g = [[] for _ in range(n)]
    d = [0] * n
    for i in range(n - 1):
        (a, b) = [int(x) for x in input().split()]
        a -= 1
        b -= 1
        g[a].append(b)
        g[b].append(a)
        d[a] += 1
        d[b] += 1
    dd = bfs(g, 0)
    even = 0
    odd = 0
    for (index, c) in enumerate(dd):
        even += (d[index] == 1) * (c % 2 == 0)
        odd += (d[index] == 1) * (c % 2 == 1)
    bads = 0
    for a in range(n):
        if d[a] != 1:
            counter = 0
            for b in g[a]:
                counter += int(d[b] == 1)
            bads += max(0, counter - 1)
    print(1 + 2 * int(even >= 1 and odd >= 1), n - 1 - bads)

def main():
    t = 1
    for _ in range(t):
        solve()
if __name__ == '__main__':
    main()