E - Tunnel Network ZOJ - 3604 Cayley's Theorem also known as Cayley's Theorem

Country Far-Far-Away is a big country with N cities. But it is now under a civil war. The rebel uses the ancient tunnel network which connects all N cities with N-1 inter-city tunnels for transportation. The government army want to destroy these tunnels one by one. After several months fighting, some tunnels have been destoryed. According to the intel, the tunnel network have excatly S connected components now. And what government army further knows is that city 1, 2, ... , S belong to each of the S connected components. Since the government have little knowledge about the remaining tunnels, they ask you to calculate the number of possible networks of remaining tunnels.


Input

There are multiple test cases. The first line of input contains an integer T (T ≤ 500) indicating the number of test cases. Then T test cases follow.

Each case contains one line containing two integer N (2 ≤ N ≤ 2000) and S (2 ≤ S ≤ N), as described above.

Output

The number of possible networks now. Since the number might be very large, you should output the answer after modulo 1000000007.

Sample Input
4
3 2
4 2
5 3
100 50
Sample Output
2
8
15
113366355
#include<bits/stdc++.h>

using namespace std;
typedef long long ll;

const int mod=1e9+7;

intmain()
{
	int T;
	scanf("%d",&T);
	while(T--){
		ll n,s;
		scanf("%lld %lld",&n,&s);
	 	ll ans=s;
	 	for(ll i=1;i<n-s;i++){
	 		ans=(ans*n)%mod;
	 	}
	 	if(n==s)ans=1;
	 	printf("%lld\n",ans);
	}
	return 0;
}

The gist of the title: Given a point numbered 1-n, and a connected graph with a given number 1-S, at the beginning, the connected graph No. 1 has only one vertex, which is the vertex numbered 1, and the connected graph No. 2 also has only one vertex. , the vertex numbered 2, similarly 3, 4, 5 know s;
the remaining vertices have s+1, s+2, s+3, .... to n;
you can randomly assign these points to connect edges The way to join the S Unicom map,
now ask. . . The final forest consists of N points. . Types, how many plans?

Let's first look at the concept of purfer sequence.
http://baike.baidu.com/link?url=3OoqRmUscw8cg7MuKij1NbTizP_uNRiF-et8Dh1-10IUFqlZXrttHQK3tZ2wQx-MgTD3k_ayw3g0XdLWVSVyOK
That is to say, a tree of n points can be uniquely mapped into a sequence with n-2 items.
! ! ! But the graph formed by the title is a forest, not a tree. It doesn't matter, just add a node 0 and connect it.
Then, as long as we know how many kinds of sequences there are, then the problem is solved!

Image from the link above

we know. . The prufer sequence for this (tree) is. {3,5,1,3}.
If we swap the positions of point 4 and point 6 in the figure, the new sequence is {3, 3, 1, 3} Well. . What does it mean, nothing. . Just want to say 3 seems to appear many times. .

Conjecture ing, for N nodes, S connected graphs. Forest,.
The 1st to ns-1 of the purfer sequence can take any number from 1-n, and the ns-th must take the point of 1-S, because they are located in the first layer, and borrow a graph:

other people's pictures 

The connection method of 4, 6, 5, 7, determines who of 1, 2, and 3 will join the sequence in the ns. The order of
the n-s+1, n-s+2, to n-2 items is determined Yes, as far as the above picture is concerned, 1, 2, 3. . Degrees are all 1. .

So the final number of solutions is that the 1st to ns-1 items can be taken from 1 to n, the ns-th item can be taken from 1 to s, the n-s+1 to n-2 are fixed, the
answer === s * n^(ns-1) ;

code

Finally, it leads
to Cayley's theorem, also known as Cayley's
theorem . The number of trees with n marked vertices is equal to n^(n-2).


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325901251&siteId=291194637