Problem:
 Score : 600 points    Problem Statement  We have a tree with N vertices numbered 1 to N .
The i -th edge in this tree connects Vertex a_i and Vertex b_i . Consider painting each of these edges white or black. There are 2^{N-1} such ways to paint the edges. Among them, how many satisfy all of the following M restrictions?   The i -th (1 \leq i \leq M) restriction is represented by two integers u_i and v_i , which mean that the path connecting Vertex u_i and Vertex v_i must contain at least one edge painted black.       Constraints   2 \leq N \leq 50  1 \leq a_i,b_i \leq N  The graph given in input is a tree.  1 \leq M \leq \min(20,\frac{N(N-1)}{2})  1 \leq u_i < v_i \leq N  If i \not= j , either u_i \not=u_j or v_i\not=v_j  All values in input are integers.         Input  Input is given from Standard Input in the following format:  N  a_1  b_1  :  a_{N-1}  b_{N-1}  M  u_1  v_1  :  u_M  v_M       Output  Print the number of ways to paint the edges that satisfy all of the M conditions.        Sample Input 1 3
1 2
2 3
1
1 3      Sample Output 1 3  The tree in this input is shown below:     All of the M restrictions will be satisfied if Edge 1 and 2 are respectively painted (white, black), (black, white), or (black, black), so the answer is 3 .       Sample Input 2 2
1 2
1
1 2      Sample Output 2 1  The tree in this input is shown below:     All of the M restrictions will be satisfied only if Edge 1 is painted black, so the answer is 1 .       Sample Input 3 5
1 2
3 2
3 4
5 3
3
1 3
2 4
2 5      Sample Output 3 9  The tree in this input is shown below:           Sample Input 4 8
1 2
2 3
4 3
2 5
6 3
6 7
8 6
5
2 7
3 5
1 6
2 8
7 8      Sample Output 4 62  The tree in this input is shown below:

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

import numpy as np

N = int(readline())
AB = list(list(map(int,readline().split())) for _ in range(N-1))
M = int(readline())
UV = list(list(map(int,readline().split())) for _ in range(M))

graph = [[] for _ in range(N+1)]
for i, (a,b) in enumerate(AB):
    graph[a].append((b,i))
    graph[b].append((a,i))

def get_path(U,V):
    INF = 100
    visited = [False] * (N+1)
    pred = [None] * (N+1)
    stack = [U]
    visited[U] = True
    while stack:
        v = stack.pop()
        for w, i in graph[v]:
            if visited[w]:
                continue
            visited[w] = True
            pred[w] = (v,i)
            stack.append(w)
    path = 0
    w = V
    while w != U:
        v,i = pred[w]
        w = v
        path += 1<<i
    return path

condition_path = [get_path(u,v) for u,v in UV]

S = np.zeros(1<<M,np.int64)
sgn = np.ones(1<<M,np.int64)
for i in range(M):
    S[1<<i:1<<(i+1)] = S[:1<<i] | condition_path[i]
    sgn[1<<i:1<<(i+1)] = -sgn[:1<<i]

def popcnt(n):
    c = (n & 0x5555555555555555) + ((n>>1) & 0x5555555555555555)
    c = (c & 0x3333333333333333) + ((c>>2) & 0x3333333333333333)
    c = (c & 0x0f0f0f0f0f0f0f0f) + ((c>>4) & 0x0f0f0f0f0f0f0f0f)
    c = (c & 0x00ff00ff00ff00ff) + ((c>>8) & 0x00ff00ff00ff00ff)
    c = (c & 0x0000ffff0000ffff) + ((c>>16) & 0x0000ffff0000ffff)
    c = (c & 0x00000000ffffffff) + ((c>>32) & 0x00000000ffffffff)
    return c

n_edges = popcnt(S)

x = 1<<(N-1-n_edges)
answer = (x * sgn).sum()
print(answer)

Solution (Python):
def popcnt(n):
    c = (n & 0x5555555555555555) + ((n>>1) & 0x5555555555555555)
    c = (c & 0x3333333333333333) + ((c>>2) & 0x3333333333333333)
    c = (c & 0x0f0f0f0f0f0f0f0f) + ((c>>4) & 0x0f0f0f0f0f0f0f0f)
    c = (c & 0x00ff00ff00ff00ff) + ((c>>8) & 0x00ff00ff00ff00ff)
    c = (c & 0x0000ffff0000ffff) + ((c>>16) & 0x0000ffff0000ffff)
    c = (c & 0x00000000ffffffff) + ((c>>32) & 0x00000000ffffffff)
    return c

def dfs(g,root,w):
    parent = [-1]*n
    enum = [-1]*n
    q = [root]
    while q:
        v = q.pop()
        for c,i in g[v]:
            if c != parent[v]:
                parent[c] = v
                enum[c] = i
                q.append(c)
    a = 0
    
    while w != root:
        a |= 1<<enum[w]
        w = parent[w]
    return a
    




import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline
read = sys.stdin.read



MOD = 10**9+7

n = int(input())
g = [[] for _ in range(n)]
for i in range(n-1):
    a,b = [int(i)-1 for i in readline().split()]
    g[a].append((b,i))
    g[b].append((a,i))

m = int(input())


res = [0]*m
for i in range(m):
    v,w = [int(i)-1 for i in readline().split()]
    res[i] = dfs(g,v,w)

ans = 1<<(n-1)
M = 1<<m

for mask in range(1,M):
    r = 0
    for j in range(m):
        if mask&(1<<j): r |= res[j]
    
    r = popcnt(r)
    c = popcnt(mask)
    if c%2:
        ans -= 1<<(n-1-r)
    else:
        ans += 1<<(n-1-r)

print(ans)

Solution (Python):
from copy import copy

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

edge_set = [set() for i in range(n)]
edge_id = 0

def dfs(p,v,se):
    global edge_id
    edge_set[v] = se
    for to in G[v]:
        if to != p:
            nset = copy(se)
            nset.add(edge_id)
            edge_id += 1
            dfs(v,to,nset)

dfs(-1,0,set())

m = int(input())
edge_set2 = []
for i in range(m):
    a,b = map(int, input().split())
    a,b = a-1,b-1
    edge_set2.append(edge_set[a]^edge_set[b])

from itertools import combinations
ans = 0
for i in range(m):
    op = -1 if i%2 else 1
    for c in combinations(range(m), i+1):
        s = set()
        for j in c:
            s |= edge_set2[j]
        rem = n-1 - len(s)
        ans += op*(1<<rem)
ans = 2**(n-1) - ans
print(ans)

Solution (Python):
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  

n = ni()
g = [[] for _ in range(n)]
for i in range(n-1):
    a, b = na()
    g[a-1].append(b-1)
    g[b-1].append(a-1)

def dfs(cur, pre, g, par, od):
    par[cur] = pre
    od.append(cur)
    for e in g[cur]:
        if e == pre: continue
        dfs(e, cur, g, par, od)

par = [-1] * n
od = []
dfs(0, -1, g, par, od)

def lca(a, b, par):
    ps = set()
    q = a
    while q != -1:
        ps.add(q)
        q = par[q]
    while b != -1:
        if b in ps:
            return b
        b = par[b]
    raise

m = ni()
cons = [0] * n
for i in range(m):
    u, v = na()
    u -= 1
    v -= 1
    lc = lca(u, v, par)
    while u != lc:
        cons[u] |= 1<<i
        u = par[u]
    while v != lc:
        cons[v] |= 1<<i
        v = par[v]

def st(b, m):
    
    for i in range(m):
        for j in range(1<<m):
            if (j>>i&1) == 0:
                b[j|1<<i] += b[j]
    return b

def ist(b, m):
    
    for i in range(m):
        for j in range(1<<m):
            if (j>>i&1) == 0:
                b[j|1<<i] -= b[j]
    return b

dp = [1] * (1<<m)
for cur in range(1,n):
    for i in range(1<<m):
        if (i&cons[cur]) == cons[cur]:
            dp[i] *= 2
















    

dp = ist(dp, m)

print(dp[(1<<m)-1])

Solution (Python):
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,copy,time
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())

N = inp()
lines = [[] for _ in range(N)]
for i in range(N-1):
    a,b = inpl()
    a-=1; b-=1
    lines[a].append((b,i))
    lines[b].append((a,i))


def dfs(s,b,v):
    if s == v:
        return set()
    else:
        for t,i in lines[s]:
            if t == b:
                continue
            ret = dfs(t,s,v)
            if ret is not None:
                ret.add(i)
                return ret

        return None


M = inp()
mm = [] 
for m in range(M):
    u,v = inpl()
    u-=1; v-=1
    mm.append(dfs(u,-1,v))



dp = [0] * (1<<M)
dp[0] = 1

for i in range(N-1):
    
    
    
    tmp = 0
    for b,m in enumerate(mm):
        if i in m:
            tmp += (1<<b)

    for b in reversed(range(1<<M)):
        dp[b | tmp] += dp[b]



print(dp[-1])