题意:

给定一棵n个节点的树,从1到n标号。选择k个点,你需要选择一些边使得这k个点通过选择的边联通,目标是使得选择的边数最少。

现需要计算对于所有选择k个点的情况最小选择边数的总和为多少。

考虑每条边对答案的贡献,令x为这条边左边的点数,则n-x为这条边右边的点数。

满足条件的情况数=总情况数-不满足条件的情况数。即C(n,k)-(C(x,k)+C(n-x,k)).

# include <cstdio>
# include <cstring>
# include <cstdlib>
# include <iostream>
# include <vector>
# include <queue>
# include <stack>
# include <map>
# include <bitset>
# include <set>
# include <cmath>
# include <algorithm>
using namespace std;
# define lowbit(x) ((x)&(-x))
# define pi acos(-1.0)
# define eps 1e-
# define MOD
# define INF
# define mem(a,b) memset(a,b,sizeof(a))
# define FOR(i,a,n) for(int i=a; i<=n; ++i)
# define FO(i,a,n) for(int i=a; i<n; ++i)
# define bug puts("H");
# define lch p<<,l,mid
# define rch p<<|,mid+,r
# define mp make_pair
# define pb push_back
typedef pair<int,int> PII;
typedef vector<int> VI;
# pragma comment(linker, "/STACK:1024000000,1024000000")
typedef long long LL;
inline int Scan() {
int x=,f=; char ch=getchar();
while(ch<''||ch>''){if(ch=='-') f=-; ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-''; ch=getchar();}
return x*f;
}
inline void Out(int a) {
if(a<) {putchar('-'); a=-a;}
if(a>=) Out(a/);
putchar(a%+'');
}
const int N=;
//Code begin... struct Edge{int p, next;}edge[N<<];
int head[N], cnt=, siz[N];
LL fac[N], ans;
int n, k; void exgcd(LL a,LL b,LL & d,LL & x,LL & y){
if(!b) d = a, x = , y = ;
else exgcd(b, a%b, d, y, x), y -= x*(a/b);
}
LL inv(LL a, LL p){
LL d, x, y;
exgcd(a, p, d, x, y);
return d == ? (x+p)%p : -;
}
void init(){
fac[k]=;
FOR(i,k+,n) fac[i]=fac[i-]*i%MOD*inv(i-k,MOD)%MOD;
}
void add_edge(int u, int v){edge[cnt].p=v; edge[cnt].next=head[u]; head[u]=cnt++;}
void dfs(int x, int fa){
int tmpx, tmpy;
siz[x]=;
for (int i=head[x]; i; i=edge[i].next) {
int v=edge[i].p;
if (v==fa) continue;
dfs(v,x); siz[x]+=siz[v];
tmpx=siz[v]; tmpy=n-siz[v];
ans=(ans+fac[n]-fac[tmpx]-fac[tmpy])%MOD;
}
}
int main ()
{
int u, v;
n=Scan(); k=Scan();
init();
FO(i,,n) u=Scan(), v=Scan(), add_edge(u,v), add_edge(v,u);
dfs(,);
printf("%lld\n",(ans+MOD)%MOD);
return ;
}

51nod 1677 treecnt(思维)的更多相关文章

  1. 51Nod 1677 treecnt

    一道比较基础的计数题,还是一个常用的单独计算贡献的例子. 首先看题目和范围,暴力枚举肯定是不可行的,而且\(O(n\ logn)\)的算法貌似很难写. 那我们就来想\(O(n)\)的吧,我们单独考虑每 ...

  2. 1677 treecnt(贡献)

    1677 treecnt 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 给定一棵n个节点的树,从1到n标号.选择k个点,你需要选择一些边使得这k个点通过选择的边联 ...

  3. 51nod 1625 贪心/思维

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1625 1625 夹克爷发红包 基准时间限制:1 秒 空间限制:13107 ...

  4. 51nod 1099 贪心/思维

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1099 1099 任务执行顺序 基准时间限制:1 秒 空间限制:13107 ...

  5. 51nod 1554 KMP思维题

    题目为中文,因而不再解释题意. 首先遵循如下设定可以有以下几个结论:1,首先谈论下KMP的一个特殊性质:对于某一个特立独行的字符串:例如ABCDEF,在建立有限状态自动机之后,都会有,所有元素的失配边 ...

  6. 51nod 1069【思维】

    具体思路来自相关讨论 给个不太严谨的证明思路: 第一步:证明路径可逆,也就是如果(a, b) -> (x, y)可行,则(x, y) - > (a, b)可行 这个比较直观,只需要分别由( ...

  7. 51nod 1677

    考虑树上的每条边对答案的贡献--- x ----y ---若 x 左边有 a2 个点,y 的右边有 a3 个点那么改边对答案的贡献为 C(n, k) - C(a2, k) - C(a3, k)快速幂求 ...

  8. 胡小兔的OI日志3 完结版

    胡小兔的 OI 日志 3 (2017.9.1 ~ 2017.10.11) 标签: 日记 查看最新 2017-09-02 51nod 1378 夹克老爷的愤怒 | 树形DP 夹克老爷逢三抽一之后,由于采 ...

  9. Luogu P3177 [HAOI2015]树上染色

    一道有机结合了计数和贪心这一DP两大考点的神仙题,不得不说做法是很玄妙. 首先我们很容易想到DP,设\(f_{i,j}\)表示在以\(i\)为根节点的子树中选\(j\)个黑色节点的最大收益值. 然后我 ...

随机推荐

  1. 20155307 实验一《Java开发环境的熟悉》实验报告

    (一)命令行下Java程序开发 题目:实现Fibonacci数列功能,并进行测试 命令行下的程序截图 上传到了码云: 遇到的问题? 可以直接使用Javac,不加环境变量也行,但是文件的名字与类名必须一 ...

  2. linux下order by 报出ORDER BY clause is not in SELECT list

    一.问题: 在程序执行查询的时候,order by 不能找到要排序的列 二.解决: 在linux环境下,程序之前连接其他库可以正常运行,但是换了一个库后数据就不能正常的显示了,查看后台报出排序列找不到 ...

  3. Drupal8 新建第一个模块

    参考: https://www.drupal.org/developing/modules/8 https://www.drupal.org/node/1915030 https://www.drup ...

  4. 2017"百度之星"程序设计大赛 - 初赛(A) 小C的倍数问题

    谢谢帮忙刷访问量! 题解当然下考再发啦 答案为P-1的约数个数 // It is made by XZZ #include<cstdio> #include<algorithm> ...

  5. 2019年猪年颁奖典礼、公司年会、跨年晚会、科技会议、年终答谢会之幕布背景展板PSD模板-第三部分

    16套--2019年猪年颁奖典礼.公司年会.跨年晚会.科技会议.年终答谢会之幕布.背景和展板PSD模板,免费颁奖典礼PSD展板背景幕布,下载地址:百度网盘,https://pan.baidu.com/ ...

  6. tomcat配置https | 自签发证书配置

    未配置证书的访问:

  7. POJ 3579 Median 二分加判断

    Median Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12453   Accepted: 4357 Descripti ...

  8. BVT、EVT、DVT、PVT产品开发几个阶段

      EVT EVT(Engineering Verification Test) 工程验证 产品开发初期的设计验证.设计者实现样品时做初期的测试验证,包括 功能和安规测试,一般由 RD(Researc ...

  9. Cube Stacking P0J 1988(加权并查集)

    Description Farmer John and Betsy are playing a game with N (1 <= N <= 30,000)identical cubes ...

  10. Visiting a Friend(思维)

    Description Pig is visiting a friend. Pig's house is located at point 0, and his friend's house is l ...