ورودی
سطر اول ورودی شامل عدد طبیعی،
n
n، تعداد جاسوسها است.
در هر یک از
n
−
1
n−1 سطر بعدی به ترتیب دو عدد
u
u و
v
v آمده است که به معنای این است که جاسوس
u
u و
v
v میتوانند باهم ارتباط تلفنی برقرار کنند و همچنین کد جاسوس
u
u باید کمتر از کد جاسوس
v
v باشد.
1
≤
n
≤
3
000
1≤n≤3 000
u
≠
v
,
1
≤
u
,
v
≤
n
u
=v,1≤u,v≤n
خروجی
در تنها سطر خروجی، باقیماندهی تعداد روشهای کدگذاری جاسوسها بر
1
0
9
+
7
10
9
+7 را چاپ کنید.
Assistant
The problem requires calculating the number of ways to assign codes to spies such that for each pair (u, v) given, the code of spy u is less than the code of spy v. This is essentially counting the number of permutations of the spies that satisfy the given set of inequalities.
Here's a detailed step-by-step solution:
Understand the Problem as a DAG:
The spies and the inequalities can be represented as a Directed Acyclic Graph (DAG).
Each inequality code[u] < code[v] is an edge from u to v.
Recognize the Tree Structure:
Since there are n spies and n - 1 inequalities, and the graph is connected, the DAG forms a tree.
This tree has a root (spy with an in-degree of 0), and every other spy is connected via the inequalities.
Recursive Approach:
Base Case: For a leaf node (spy with no outgoing edges), there's only one way to assign codes.
Recursive Case: For any internal node u, the number of ways to assign codes is calculated based on its children's assignments.
Calculating the Number of Ways:
Subtree Sizes: Calculate the size of the subtree rooted at each node (number of spies in the subtree).
Multinomial Coefficient: For node u with children of sizes s1, s2, ..., sk, the number of ways to arrange the spies is:
[
f(u) = \frac{(S - 1)!}{s1! \times s2! \times \dots \times sk!} \times f(v1) \times f(v2) \times \dots \times f(vk)
]
where ( S = s1 + s2 + \dots + sk + 1 ) (total number of spies including u), and ( f(vi) ) is the number of ways for child vi.
Implementing Factorials Modulo ( 10^9 + 7 ):
Precompute factorials and inverse factorials modulo ( 10^9 + 7 ) to handle large numbers and perform calculations efficiently.
Depth-First Search (DFS) Traversal:
Perform DFS starting from the root to compute the subtree sizes and number of ways recursively.
Use the computed factorials to calculate the multinomial coefficients at each node.
Handling Large Numbers:
Use modular arithmetic to ensure that all calculations are within the bounds of ( 10^9 + 7 ).
Output:
The required answer is the number of ways to assign the codes for the entire tree rooted at the root spy.
Here's the implementation based on the above approach:
python
MOD = 10**9 + 7
import sys
import threading
def main():
import sys
sys.setrecursionlimit(1 << 25)
n = int(sys.stdin.readline())
indegree = [0] * (n+1)
adj = [[] for _ in range(n+1)] # 1-based indexing
for _ in range(n-1):
u, v = map(int, sys.stdin.readline().split())
adj[u].append(v)
indegree[v] += 1
root = None
for i in range(1, n+1):
if indegree[i] == 0:
root = i
break
factorial = [1] * (n+2)
inv_factorial = [1] * (n+2)
for i in range(1, n+2):
factorial[i] = factorial[i-1] * i % MOD
inv_factorial[n+1] = pow(factorial[n+1], MOD-2, MOD)
for i in range(n+1, 0, -1):
inv_factorial[i-1] = inv_factorial[i] * i % MOD
def comb(n, k):
if k < 0 or k > n:
return 0
return factorial[n] * inv_factorial[k] % MOD * inv_factorial[n-k] % MOD
size = [0] * (n+1)
f = [0] * (n+1)
def dfs(u):
size[u] = 1
f[u] = 1
for v in adj[u]:
dfs(v)
size[u] += size[v]
total = size[u] - 1 # excluding u
f[u] = 1
for v in adj[u]:
c = size[v]
f[u] = f[u] * f[v] % MOD
f[u] = f[u] * comb(total, c) % MOD
total -= c
dfs(root)
print(f[root])
threading.Thread(target=main).start()
The above code reads the input, constructs the tree based on the given inequalities, performs the necessary computations using DFS, and finally prints the number of ways modulo ( 10^9 + 7 ).