E - Tunnel Network ZOJ - 3604 Cayley定理又称凯莱定理

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;

int main()
{
	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;
}

题目大意: 给定编号1-n的点,和给定编号1-S 的联通图,刚开始1号联通图只有 1个顶点,就是编号为1的顶点,2号联通图也只有1个顶点,编号为2的顶点,同理 3,4,5知道s;
剩下的顶点还有 s+1,s+2,s+3,….到n;
这些点你可以随机分配,以连边的方式加入到S个联通图里,
现在问。。。最后由N个点构成的森林。。种类,方案数有多少?

先看看purfer 数列这个概念吧。
http://baike.baidu.com/link?url=3OoqRmUscw8cg7MuKij1NbTizP_uNRiF-et8Dh1-10IUFqlZXrttHQK3tZ2wQx-MgTD3k_ayw3g0XdLWVSVyOK
就是说,一个n个点构成树都可以唯一映射成有n-2项的数列。
!!!可是题目上构成的图是森林,不是树,没关系,加个0号节点,连接起来就是啦。
那么,只要我们知道数列有多少种,那么问题迎刃而 !

上面链接出现的图

我们知道。。这个(树) 的prufer序列是。{3,5,1,3}。
如果我们把图中的 4号,和6号点交换位置,新的序列就是{3,3,1,3} 嗯。。意味着什么,,没什么。。就是想说 3 好像出现的很多次。。

猜想ing,对于 N个节点,S 个联通图的。森林,。
purfer序列 第1到n-s-1可以取 1-n的任意一个数,第n-s 个 必须取 1-S 的点,,因为它们位于第一层,,借个图:

别人的图 

4,6,5,7,的连法决定了1,2,3中的谁会在第n-s中加入序列
第 n-s+1,n-s+2,到 n-2项的顺序是确定的,就上面的图来说,1,2,3。。度数都为1。。

所以 最终的方案数为  第1到n-s-1项可以1到n任取 ,第n-s项可以1到s 任取,第n-s+1到n-2 是固定的,
答案 === s * n^(n-s-1) ;

代码啦

最后引出
Cayley定理又称凯莱定理
过n个有标志顶点的树的数目等于n^(n-2)。


猜你喜欢

转载自blog.csdn.net/qq_36424540/article/details/80043433