Problem:


Score : 1100 points

Problem Statement

Coloring of the vertices of a tree G is called a good coloring when, for every pair of two vertices u and v painted in the same color, picking u as the root and picking v as the root would result in isomorphic rooted trees.

Also, the colorfulness of G is defined as the minimum possible number of different colors used in a good coloring of G.

You are given a tree with N vertices. The vertices are numbered 1 through N, and the i-th edge connects Vertex a_i and Vertex b_i. We will construct a new tree T by repeating the following operation on this tree some number of times:

Add a new vertex to the tree by connecting it to one of the vertices in the current tree with an edge.

Find the minimum possible colorfulness of T.
Additionally, print the minimum number of leaves (vertices with degree 1) in a tree T that achieves the minimum colorfulness.

Notes

The phrase "picking u as the root and picking v as the root would result in isomorphic rooted trees" for a tree G means that there exists a bijective function f_{uv} from the vertex set of G to itself such that both of the following conditions are met:

f_{uv}(u)=v

For every pair of two vertices (a,b), edge (a,b) exists if and only if edge (f_{uv}(a),f_{uv}(b)) exists.

Constraints

2 \leq N \leq 100

1 \leq a_i,b_i \leq N(1\leq i\leq N-1)

The given graph is a tree.

Input

Input is given from Standard Input in the following format:

N
a_1 b_1
:
a_{N-1} b_{N-1}

Output

Print two integers with a space in between.
First, print the minimum possible colorfulness of a tree T that can be constructed.
Second, print the minimum number of leaves in a tree that achieves it.

It can be shown that, under the constraints of this problem, the values that should be printed fit into 64-bit signed integers.

Sample Input 1

5
1 2
2 3
3 4
3 5

Sample Output 1

2 4

If we connect a new vertex 6 to vertex 2, painting the vertices (1,4,5,6) red and painting the vertices (2,3) blue is a good coloring.
Since painting all the vertices in a single color is not a good coloring, we can see that the colorfulness of this tree is 2.
This is actually the optimal solution. There are four leaves, so we should print 2 and 4.

Sample Input 2

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

Sample Output 2

3 4

Sample Input 3

10
1 2
2 3
3 4
4 5
5 6
6 7
3 8
5 9
3 10

Sample Output 3

4 6

Sample Input 4

13
5 6
6 4
2 8
4 7
8 9
3 2
10 4
11 10
2 4
13 10
1 8
12 1

Sample Output 4

4 12



Sample input:
5
1 2
2 3
3 4
3 5


Sample output:
2 4

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(pow(10, 6))

from collections import deque

edges = []

def solve(u, v=-1):
  global edges
  que = deque([u])
  if v != -1:
    que.append(v)
  used = set([u, v])
  D = {u: 0, v: 0}
  E = {}
  if v != -1:
    E[0] = max(len(edges[v]), len(edges[u])) - 1
  else:
    E[0] = len(edges[u])
  x = v
  while que:
    v = que.popleft()
    d = D[v]
    for w in edges[v]:
      if w in used:
        continue
      D[w] = d + 1
      que.append(w)
      used.add(w)
      E[d+1] = max(E.get(d+1, 0), len(edges[w])-1)
  res = 2 if x != -1 else 1
  for v in E.values():
    if v:
      res *= v
  return res


def main():
  global edges
  n = int(input())
  edges = [[] for _ in range(n)]
  for _ in range(n-1):
    a, b = map(int, input().split())
    edges[a-1].append(b-1)
    edges[b-1].append(a-1)

  # calc diameter
  que = deque([0])
  dist = {0: 0}
  prev = {0: 0}
  while que:
    u = que.popleft()
    d = dist[u]
    for w in edges[u]:
      if w in dist:
        continue
      dist[w] = d + 1
      prev[w] = u
      que.append(w)
  w = u  # 最遠点
  que = deque([w])
  dist = {w: 0}
  prev = {w: -1}
  while que:
    u = que.popleft()
    d = dist[u]
    for w in edges[u]:
      if w in dist:
        continue
      dist[w] = d + 1
      prev[w] = u
      que.append(w)
  w = u
  D = []
  while w != -1:
    D.append(w)
    w = prev[w]

  L = len(D)  # 直径
  col = (L + 1) // 2
  if L % 2 == 1:
    ans = 10**18
    v = D[L//2]  # 中心
    for w in edges[v]:  # 中心から繋がってる部分について
      ans = min(ans, solve(v, w))
    ans = min(ans, solve(v))
  else:
    # 直径が偶数の時は中心が2個
    v = D[L//2-1]
    w = D[L//2]
    ans = solve(v, w)
  print(col, ans)


if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
N = int(input())
G = [[] for i in range(N)]
for i in range(N-1):
    a, b = map(int, input().split())
    G[a-1].append(b-1)
    G[b-1].append(a-1)

from collections import deque
def bfs(v):
    que = deque([v])
    dist = {v: 0}
    prev = {v: v}
    while que:
        u = que.popleft()
        d = dist[u]
        for w in G[u]:
            if w in dist:
                continue
            dist[w] = d + 1
            prev[w] = u
            que.append(w)
    w = u
    D = []
    while w != v:
        D.append(w)
        w = prev[w]
    D.append(v)
    return u, D

from collections import deque
def solve(u, v = -1):
    que = deque([u])
    if v != -1:
        que.append(v)
    used = set([u, v])
    D = {v: 0, u: 0}
    E = {}
    if v != -1:
        E[0] = max(len(G[v]), len(G[u]))-1
    else:
        E[0] = len(G[u])
    x = v
    while que:
        v = que.popleft()
        d = D[v]
        for w in G[v]:
            if w in used:
                continue
            D[w] = d + 1
            que.append(w)
            used.add(w)
            E[d+1] = max(E.get(d+1, 0), len(G[w])-1)
    res = 2 if x != -1 else 1
    for v in E.values():
        if v: res *= v
    return res

D = bfs(bfs(0)[0])[1]
L = len(D)

col = (L + 1) // 2

if L % 2 == 1:
    ans = 10**18
    v = D[L//2]
    for w in G[v]:
        ans = min(ans, solve(v, w))
    ans = min(ans, solve(v))
else:
    v = D[L//2-1]; w = D[L//2]
    ans = solve(v, w)
print(col, ans)

Submission status: Accepted

Solution code:
from collections import deque
from functools import reduce
from itertools import zip_longest
from operator import mul


def dfs(links):
    q = deque([(v, 0) for v in links[0]])
    v = 0
    while q:
        v, a = q.popleft()
        q.extend((u, v) for u in links[v] if u != a)

    anc = [-1] * len(links)
    q = deque([(u, v) for u in links[v]])
    u = 0, 0
    while q:
        u, a = q.popleft()
        anc[u] = a
        q.extend((w, u) for w in links[u] if w != a)

    path = [u]
    a = u
    while a != v:
        a = anc[a]
        path.append(a)
    return path


def calc(links, v, a, l):
    ans = [1] * l
    q = deque([(u, v, 1) for u in links[v] if u != a])
    ans[0] = len(q)
    while q:
        u, a, c = q.popleft()
        ans[c] = max(ans[c], len(links[u]) - 1)
        q.extend((w, u, c + 1) for w in links[u] if w != a)
    return ans


def solve(n, links):
    if n == 2:
        return 1, 2

    path = dfs(links)
    c = len(path)
    l = (len(path) + 1) // 2
    # print('path', path)

    if c % 2 == 0:
        u, v = path[c // 2 - 1], path[c // 2]
        ans1 = calc(links, u, v, l)
        ans2 = calc(links, v, u, l)
        # print('ans12', ans1, ans2)
        leaves = 1
        for a1, a2 in zip(ans1, ans2):
            leaves *= max(a1, a2)
        return len(ans1), leaves * 2
    v = path[c // 2]
    ans0 = calc(links, v, None, l)
    leaves = reduce(mul, ans0, 1)
    # print('ans0', ans0, leaves)
    for u in links[v]:
        ans1 = calc(links, u, v, l)
        ans2 = calc(links, v, u, l)
        tmp = 1
        for a1, a2 in zip_longest(ans1, ans2):
            tmp *= max(a1, a2)
        # print('ans12', ans1, ans2, tmp)
        leaves = min(leaves, tmp * 2)
    return len(ans0), leaves


n = int(input())
links = [set() for _ in range(n)]
for _ in range(n - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    links[a].add(b)
    links[b].add(a)
print(*solve(n, links))

Submission status: Accepted

Solution code:
from collections import defaultdict
def dfs(v, g, d, p=-1):
    r = v
    for u in g[v]:
        if p == u:
            continue
        d[u] = d[v] + 1
        t = dfs(u, g, d, v)
        if d[r] < d[t]:
            r = t
    return r
def main():
    n = int(raw_input())
    g = defaultdict(list)
    e = []
    for _ in xrange(n - 1):
        u, v = map(int, raw_input().split())
        u -= 1
        v -= 1
        g[u].append(v)
        g[v].append(u)
        e.append((u, v))
    for i in xrange(n):
        e.append((i, -1))
    ans = (n, 2 ** 64)
    for u, v in e:
        dc = [1] * n
        k = dfs(u, g, dc, v)
        l = dc[k]
        if v != -1:
            k = dfs(v, g, dc, u)
            if l < dc[k]:
                l = dc[k]
        ch = [0] * (l + 1)
        for i in xrange(n):
            if v == -1 and i == u:
                ch[dc[i]] = len(g[i])
            else:
                x = len(g[i]) - 1
                if ch[dc[i]] < x:
                    ch[dc[i]] = x
        p = 1 if v == -1 else 2
        for i in xrange(1, l):
            p *= ch[i]
        x = (l, p)
        if ans > x:
            ans = x
    print ans[0], ans[1]
main()

Submission status: Accepted

Solution code:
n = int(input())
sus = [[] for i in range(n)]
edges = []
for i in range(n - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    sus[a].append(b)
    sus[b].append(a)
    edges.append((a, b))

def probaj(korijen):
    bio = [False for i in range(n)]
    maks = [0 for i in range(n)]

    def dfs(x, level):
        nonlocal bio
        nonlocal maks
        bio[x] = True
        djece = 0
        for y in sus[x]:
            if not bio[y]:
                dfs(y, level + 1)
                djece += 1
        maks[level] = max(maks[level], djece)
    
    if isinstance(korijen, tuple):
        k1, k2 = korijen
        bio[k1] = bio[k2] = True
        dfs(k1, 0)
        dfs(k2, 0)
        a, b = 1, 2
    else:
        dfs(korijen, 0)
        a, b = 1, 1

    for l in range(n):
        if maks[l]:
            b *= maks[l]
            a += 1
        else:
            break
    return a, b

p1 = min(probaj(i) for i in range(n))
p2 = min(probaj(e) for e in edges)
p = min(p1, p2)
print(p[0], p[1])

Submission status: Runtime Error

Solution code:
N = int(input())
G = [[] for i in range(N)]
E9 = []
for i in range(N-1):
    a, b = map(int, input().split())
    G[a-1].append(b-1)
    G[b-1].append(a-1)
    E9.append((a-1, b-1))

def encode(v, p):
    res = []
    tmp = []
    l = 0
    P = []
    def edfs(v, p):
        res = [0]
        cs = []
        for w in G[v]:
            if w == p:
                continue
            cs.append(edfs(w, v))
        cs.sort()
        for c in cs:
            res.extend(c)
        res.append(1)
        return res
    E = edfs(v, p)
    return E

def calc(E):
    st = []
    C = [0]
    P = []
    L = len(E)
    l = 0
    for i in range(L):
        if i and E[i-1] < E[i]:
            l += 1
        if E[i] == 0:
            st.append(i)
            P.append(None)
        else:
            j = st.pop()
            P.append(i)
            #P[i] = j
            P[j] = i
        C.append(l)
    return C, P

INF = 10**9
def merge(e1, e2):
    l1 = len(e1); l2 = len(e2)
    dp = [[0] * (l1+1) for i in range(l2+1)]

    C1, P1 = calc(e1)
    C2, P2 = calc(e2)

    prev = {}
    memo = {}
    def dfs(x, y):
        if (x, y) in memo:
            return memo[x, y]
        if x == l1 and y == l2:
            return 0
        res = INF
        p = None
        if x < l1 and y < l2 and e1[x] == e2[y]:
            r = dfs(x+1, y+1) + max(C1[x+1]-C1[x], C2[y+1]-C2[y])
            if r < res:
                res = r
                p = (x+1, y+1)
        if x < l1 and e1[x] == 0:
            r = dfs(P1[x]+1, y) + C1[P1[x]+1] - C1[x]
            if r < res:
                res = r
                p = (P1[x]+1, y)
        if y < l2 and e2[y] == 0:
            r = dfs(x, P2[y]+1) + C2[P2[y]+1] - C2[y]
            if r < res:
                res = r
                p = (x, P2[y]+1)
        memo[x, y] = res
        prev[x, y] = p
        return res
    prev[0, 0] = (0, 0)
    r = dfs(0, 0)
    v = (0, 0)
    res = []
    while v != (l1, l2):
        print(v)
        px, py = v
        x, y = v = prev[v]
        if px != x and py != y:
            res.append(e1[px])
        elif px != x:
            res.extend(e1[px:x])
        else:
            res.extend(e2[py:y])
    print(res)
    return r, res

from collections import deque
def bfs(v, p=-1):
    que = deque([v])
    dist = {v: 0, p: 0}
    prev = {}
    while que:
        u = que.popleft()
        d = dist[u]
        for w in G[u]:
            if w not in dist:
                dist[w] = d + 1
                prev[w] = u
                que.append(w)
    w = u
    res = []
    while w != v:
        res.append(w)
        w = prev[w]
    res.append(v)
    return u, res
def solve(u, v):
    if bfs(u, v) > (L+1)//2 < bfs(v, u):
        return 10**9
    E1 = encode(u, v)
    E2 = encode(v, u)
    #print(u, v, E1, E2)
    return merge(E1, E2)[0]*2
def vsolve(v):
    if bfs(u) > (L+1)//2:
        return 10**9
    E0 = encode(G[v][0], v)
    for w in G[v][1:]:
        E1 = encode(w, w)
        _, E0 = merge(E0, E1)
    C, P = calc(E0)
    #print(v, G[v][0], E0, C[-1], len(G[v]))
    return C[-1]*len(G[v])
v, _ = bfs(0)
w, D = bfs(v)
L = len(D)

col = (L + 1) // 2

ans = 10**18
for a, b in E9:
    ans = min(ans, solve(a, b))
for v in range(N):
    ans = min(ans, vsolve(v))
print(col, ans)

Submission status: Runtime Error

Solution code:
import sys;raw_input = sys.stdin.readline
N = int(raw_input())
A = [None]*N
C = [0]*N
for i in xrange(N):
    A[i] = int(raw_input())
b = False
for i in xrange(1, N):
    if A[i] - A[i-1] > 1 :
        b = True
        break
if b or A[0] != 0:
    print -1
else:
    B = [0]*N
    ans = 0
    for i in xrange(1, N):
        val = B[i-1] + 1
        B[i] = val if val <= A[i] else 0
        C[i] = A[i] - B[i]
    for i in xrange(N-1, 0, -1):
        if B[i-1] == B[i]-1 :
            C[i-1] = min(C[i], C[i-1])
    C[0] = 0
    for i in xrange(1, N):
        if B[i] == B[i-1] + 1:
            assert C[i] >= C[i-1]
            C[i] = C[i-1]
            ans += (A[i] - B[i-1] - C[i] if B[i+1] == B[i] + 1 or B[i+1] <= 1 else A[i]) if i < N-1 else A[i] - B[i-1] - C[i]
        else:
            ans += A[i]
    print ans

Submission status: Runtime Error

Solution code:
import math
n = int(input())
lis = []
for i in range(n):lis.append(int(input()))
if lis[0] != 0:
    print("-1")
    exit()
cou = 0
print(lis,cou)
for k in range(n-1):
    if lis[n-(k+1)] > lis[n-(k+2)]:
        cou += math.ceil(lis[n-(k+1)] / (lis[n-(k+2)]+1))
        lis[n-(k+1)] = 0
    else:
        cou += lis[n-(k+1)]
        lis[n-(k+2)] -= lis[n-(k+1)]-1
        lis[n-(k+1)] = 0
    print(lis,cou)
print(cou)