LuoTianyi and the Floating Islands (Hard Version) (Codeforces 1824B2)
Tags: Math, probabilities

This is the hard version of the problem. The only difference is that in this version $$$k\le n$$$. You can make hacks only if both versions of the problem are solved.   Chtholly and the floating islands. LuoTianyi now lives in a world with $$$n$$$ floating islands. The floating islands are connected by $$$n-1$$$ undirected air routes, and any two of them can reach each other by passing the routes. That means, the $$$n$$$ floating islands form a tree.One day, LuoTianyi wants to meet her friends: Chtholly, Nephren, William, .... Totally, she wants to meet $$$k$$$ people. She doesn't know the exact positions of them, but she knows that they are in pairwise distinct islands. She define an island is good if and only if the sum of the distances$$$^{\dagger}$$$ from it to the islands with $$$k$$$ people is the minimal among all the $$$n$$$ islands.Now, LuoTianyi wants to know that, if the $$$k$$$ people are randomly set in $$$k$$$ distinct of the $$$n$$$ islands, then what is the expect number of the good islands? You just need to tell her the expect number modulo $$$10^9+7$$$.$$$^{\dagger}$$$The distance between two islands is the minimum number of air routes you need to take to get from one island to the other.

Input: The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1\le k \le n \le 2\cdot 10^5$$$) — the number of the islands and people respectively.Next $$$n−1$$$ lines describe the air routes. The $$$i$$$-th of them contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 \le u_i,v_i \le n, u_i \neq v_i$$$) — the islands connected by the $$$i$$$-th air route.

Output: Print a single integer — the expect number of the good islands modulo $$$10^9 + 7$$$.Formally, let $$$M = 10^9 + 7$$$. It can be shown that the answer can be expressed as an irreducible fraction $$$\frac{p}{q}$$$, where $$$p$$$ and $$$q$$$ are integers and $$$q \not \equiv 0$$$ ($$$\operatorname{mod} M$$$). Output the integer equal to $$$p \cdot q^{-1}$$$ $$$\operatorname{mod} M$$$. In other words, output such an integer $$$x$$$ that $$$0 \le x < M$$$ and $$$x \cdot q \equiv p$$$ ($$$\operatorname{mod} M$$$).

Note: In the first example the air routes form the following tree:   If the people are in the islands $$$1$$$ and $$$2$$$, then islands $$$1$$$ and $$$2$$$ will be good.The sum of the distances from island $$$1$$$ or $$$2$$$ to all the people is $$$1+0=1$$$, which is the minimal. While the sum of the distances from island $$$3$$$ to all the people is $$$2+1=3$$$, which is greater than $$$1$$$.Like this, when the people are in island $$$1$$$ and $$$3$$$, then islands $$$1,2$$$ and $$$3$$$ will be good.When the people are in islands $$$1$$$ and $$$4$$$, then islands $$$1,2,3$$$ and $$$4$$$ will be good.When the people are in islands $$$2$$$ and $$$3$$$, then islands $$$2$$$ and $$$3$$$ will be good.When the people are in islands $$$2$$$ and $$$4$$$, then islands $$$2,3$$$ and $$$4$$$ will be good.When the people are in islands $$$3$$$ and $$$4$$$, then islands $$$3$$$ and $$$4$$$ will be good.So the expect of the number of the good islands is $$$\frac{16}{6}$$$, which equals to $$$666666674$$$ modulo $$$10^9+7$$$.In the second example the air routes form the following tree:  We can see that there is one person in each island, and only the island $$$3$$$ is good. So the expect number is $$$1$$$.

Sample Inputs:
4 2
1 2
2 3
3 4

5 5
1 2
2 3
3 4
3 5

Sample Outputs:
666666674

1

Solution (Python):
import io, os
from collections import deque
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def fastfrac(a, b, M):
    invb = pow(b, M - 2, M)
    return a * invb % M

def getans(neigh, k, M):
    n = len(neigh)
    children = [[] for _ in range(n)]
    parent = [-2] * n
    parent[0] = -1
    layer = [0] * n
    subnum = [0] * n
    ans = [0] * n
    seq = []
    queue = deque()
    queue.append(0)
    while queue:
        index = queue.popleft()
        seq.append(index)
        for nextindex in neigh[index]:
            if parent[nextindex] > -2:
                continue
            parent[nextindex] = index
            layer[nextindex] = layer[index] + 1
            ans[0] += layer[nextindex]
            children[index].append(nextindex)
            queue.append(nextindex)
    for index in seq[::-1]:
        subnum[index] = 1
        for nextindex in children[index]:
            subnum[index] += subnum[nextindex]
    permu = [1]
    for _ in range(n):
        permu.append(permu[-1] * len(permu) % M)
    combk2 = [0] * (n + 1)
    fk2 = permu[k // 2]
    tot = fastfrac(permu[n], permu[k] * permu[n - k] % M, M)
    for i in range(n + 1):
        if i >= k // 2:
            div = fk2 * permu[i - k // 2] % M
            num = permu[i]
            combk2[i] = fastfrac(num, div, M)
    ans = 0
    for i in range(1, n):
        num1 = subnum[i]
        num2 = n - subnum[i]
        if min(num1, num2) >= k // 2 - 1:
            ans += fastfrac(combk2[num1] * combk2[num2] % M, tot, M)
            ans %= M
    return (ans + 1) % M

def main(t):
    (n, k) = map(int, input().split())
    neigh = [[] for _ in range(n)]
    for i in range(n - 1):
        (u, v) = map(int, input().split())
        neigh[u - 1].append(v - 1)
        neigh[v - 1].append(u - 1)
    if k % 2 == 1:
        print(1)
        return
    M = 10 ** 9 + 7
    ans = getans(neigh, k, M)
    print(ans)
T = 1
t = 1
while t <= T:
    main(t)
    t += 1

Solution (Python):
import io, os
from collections import deque
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def fastfrac(a, b, M):
    invb = pow(b, M - 2, M)
    return a * invb % M

def getans(neigh, k, M):
    n = len(neigh)
    children = [[] for _ in range(n)]
    parent = [-2] * n
    parent[0] = -1
    layer = [0] * n
    subnum = [0] * n
    ans = [0] * n
    seq = []
    queue = deque()
    queue.append(0)
    while queue:
        index = queue.popleft()
        seq.append(index)
        for nextindex in neigh[index]:
            if parent[nextindex] > -2:
                continue
            parent[nextindex] = index
            layer[nextindex] = layer[index] + 1
            ans[0] += layer[nextindex]
            children[index].append(nextindex)
            queue.append(nextindex)
    for index in seq[::-1]:
        subnum[index] = 1
        for nextindex in children[index]:
            subnum[index] += subnum[nextindex]
    permu = [1]
    for _ in range(n):
        permu.append(permu[-1] * len(permu) % M)
    combk2 = [0] * (n + 1)
    combk21 = [0] * (n + 1)
    fk2 = permu[k // 2]
    fk21 = permu[k // 2 - 1]
    tot = fastfrac(permu[n], permu[k] * permu[n - k] % M, M)
    for i in range(n + 1):
        if i >= k // 2:
            div = fk2 * permu[i - k // 2] % M
            num = permu[i]
            combk2[i] = fastfrac(num, div, M)
        if i >= k // 2 - 1:
            div = fk21 * permu[i - k // 2 + 1] % M
            num = permu[i]
            combk21[i] = fastfrac(num, div, M)
    ans = 0
    for i in range(1, n):
        num1 = subnum[i]
        num2 = n - subnum[i]
        if min(num1, num2) >= k // 2 - 1:
            ans += fastfrac(combk2[num1] * combk2[num2] % M, tot, M)
            ans %= M
    return (ans + 1) % M

def main(t):
    (n, k) = map(int, input().split())
    neigh = [[] for _ in range(n)]
    for i in range(n - 1):
        (u, v) = map(int, input().split())
        neigh[u - 1].append(v - 1)
        neigh[v - 1].append(u - 1)
    if k % 2 == 1:
        print(1)
        return
    M = 10 ** 9 + 7
    ans = getans(neigh, k, M)
    print(ans)
T = 1
t = 1
while t <= T:
    main(t)
    t += 1

Solution (Python):
import sys
input = sys.stdin.buffer.readline
m = 10 ** 9 + 7
(n, k) = map(int, input().split())
adj = [[] for _ in range(n + 1)]
for i in range(n - 1):
    (u, v) = map(int, input().split())
    adj[u].append(v)
    adj[v].append(u)
fac = [1] * (n + 1)
ifac = [0] * (n + 1)
for i in range(2, n + 1):
    fac[i] = fac[i - 1] * i % m
ifac[n] = pow(fac[n], -1, m)
for i in range(n - 1, -1, -1):
    ifac[i] = ifac[i + 1] * (i + 1) % m

def ch(n, k):
    if k > n:
        return 0
    return fac[n] * ifac[k] % m * ifac[n - k] % m
need = (k + 2) // 2
cnt = [0] * (n + 1)
for i in range(need, n + 1):
    cnt[i] = cnt[i - 1] + ch(i - 1, need - 1) * ch(n - i, k - need)
    cnt[i] %= m
ans = 0
sub = [0] * (n + 1)
s = [1]
vis = [0] * (n + 1)
true_s = []
while s:
    c = s[-1]
    if not vis[c]:
        vis[c] = 1
        true_s.append(c)
        s.extend(adj[c])
    else:
        sub[c] = 1
        if true_s and true_s[-1] == c:
            p = -1
            if len(true_s) >= 2:
                p = true_s[-2]
            ans += ch(n, k)
            for ne in adj[c]:
                if ne != p:
                    sub[c] += sub[ne]
                    ans -= cnt[sub[ne]]
            ans -= cnt[n - sub[c]]
            true_s.pop()
        s.pop()
print(ans % m * pow(ch(n, k), -1, m) % m)