CF1109D.Sasha and Interesting Fact from Graph Theory
提高+/省选-
通过率:0%
时间限制:2.50s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Once, during a lesson, Sasha got bored and decided to talk with his friends. Suddenly, he saw Kefa. Since we can talk endlessly about Kefa, we won't even start doing that. The conversation turned to graphs. Kefa promised Sasha to tell him about one interesting fact from graph theory if Sasha helps Kefa to count the number of beautiful trees.
In this task, a tree is a weighted connected graph, consisting of n vertices and n−1 edges, and weights of edges are integers from 1 to m. Kefa determines the beauty of a tree as follows: he finds in the tree his two favorite vertices — vertices with numbers a and b, and counts the distance between them. The distance between two vertices x and y is the sum of weights of edges on the simple path from x to y. If the distance between two vertices a and b is equal to m, then the tree is beautiful.
Sasha likes graph theory, and even more, Sasha likes interesting facts, that's why he agreed to help Kefa. Luckily, Sasha is familiar with you the best programmer in Byteland. Help Sasha to count the number of beautiful trees for Kefa. Two trees are considered to be distinct if there is an edge that occurs in one of them and doesn't occur in the other one. Edge's weight matters.
Kefa warned Sasha, that there can be too many beautiful trees, so it will be enough to count the number modulo 109+7.
有一次,在上课期间,萨沙感到无聊,决定和朋友们聊天。突然,他看到了凯法。由于关于凯法的话题可以无穷无尽地聊下去,我们甚至不会开始聊这个。谈话转向了图论。凯法向萨沙承诺:如果萨沙能帮他计算出“优美树”的数量,他就告诉萨沙一个图论中的有趣事实。
在本题中,一棵树是指一个带权的连通图,它包含 n 个顶点和 n−1 条边,且每条边的权值为 1 到 m 之间的整数。凯法按如下方式定义一棵树的“优美性”:他在树中找出他最喜欢的两个顶点——编号分别为 a 和 b 的顶点,并计算它们之间的距离。两个顶点 x 和 y 之间的距离定义为从 x 到 y 的唯一简单路径上所有边的权值之和。若顶点 a 与 b 之间的距离恰好等于 m,则称该树为“优美树”。
萨沙喜欢图论,更喜欢那些有趣的事实,因此他欣然答应帮助凯法。幸运的是,萨沙最熟悉你——比特兰(Byteland)最棒的程序员。请帮萨沙为凯法计算优美树的数量。若两棵树存在某条边仅出现在其中一棵中(即边集不同),或某条边在两棵树中端点相同但权值不同,则认为这两棵树互不相同。(注意:边的权值是区分树的重要因素。)
凯法提醒萨沙,优美树的数量可能非常大,因此只需输出答案对 109+7 取模的结果即可。
输入格式
The first line contains four integers n, m, a, b (2≤n≤106, 1≤m≤106, 1≤a,b≤n, a=b) — the number of vertices in the tree, the maximum weight of an edge and two Kefa's favorite vertices.
第一行包含四个整数 n、m、a、b(2≤n≤106,1≤m≤106,1≤a,b≤n,a=b)—— 分别表示树中顶点的数量、边的最大权重以及 Kefa 最喜欢的两个顶点。
输出格式
Print one integer — the number of beautiful trees modulo 109+7.
输出一个整数——美丽树的数目对 109+7 取模的结果。
输入输出样例
输入#1
3 2 1 3
输出#1
5
输入#2
3 1 1 2
输出#2
2
输入#3
5 15 1 5
输出#3
345444
说明/提示
There are 5 beautiful trees in the first example:

In the second example the following trees are beautiful:

第一个示例中有 5 棵优美的树:

在第二个示例中,以下这些树是优美的:

输入解题思路,AI测评打分。不知道怎么写?