题意:给定一棵树,树上有 n 个节点。问有多少种方案,使得在每个节点上依次放置数 1~n 后,每个节点的儿子节点上的数连续(比如 1 为根,有1-2,1-3,1-4,则令2,3,4上的数连续),每个子树上的数连续。

注释在代码里。

#pragma comment(linker, "/STACK:102400000,102400000")
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;
const int maxn=;
const int mod=1e9+;
vector<int>g[maxn];
int n,u,v,t,vis[maxn],flag;
long long ans;
void dfs(int u){
if(!flag) return;
int son=g[u].size(),cnt=;
vis[u]=;
for(int i=;i<son;i++){
if(vis[g[u][i]]) continue;
if(g[g[u][i]].size()==&&vis[g[u][i]]==) cnt++;//cnt为当前节点的叶子节点的总数
else dfs(g[u][i]);
}
if(u!=) son--;//存图方式为无向,因此除1以外都会多一个父亲节点,故减去
if(son-cnt>){//如果非叶子节点总数大于2,则无解
flag=;return;
}
if(cnt!=son)//如果存在非叶子节点
ans=ans*%mod;
while(cnt>){//叶子节点的阶乘
ans=ans*cnt%mod;
cnt--;
}
}
int main(){
scanf("%d",&t);
for(int c=;c<=t;c++){
flag=;
for(int i=;i<=n;i++) g[i].clear();
memset(vis,,sizeof(vis));
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%d%d",&u,&v);
g[u].push_back(v);
g[v].push_back(u);
}
ans=;//初始至少有两种方案,即一个根节点与其一个子节点
if(n!=) dfs();
else ans=;//节点数为1则只可能有一种方案
if(!flag) ans=;
printf("Case #%d: %lld\n",c,ans%mod);
}
return ;
}

2015 多校赛 第七场 1011 (hdu 5379)的更多相关文章

  1. 2015 多校赛 第五场 1010 (hdu 5352)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5352 看题看得心好累. 题目大意: 给出 n 个点,依次执行 m 次操作:输入“1 x”时,表示将与 ...

  2. 2015 多校赛 第五场 1006 (hdu 5348)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5348 题目大意:给出一幅无向图,问是否存在一种方案,使得给每条边赋予方向后,每个点的入度与出度之差小于 ...

  3. 2015 多校赛 第四场 1010 (hdu 5336)

    Problem Description XYZ is playing an interesting game called "drops". It is played on a r ...

  4. 2015 多校赛 第四场 1009 (hdu 5335)

    Problem Description In an n∗m maze, the right-bottom corner is the exit (position (n,m) is the exit) ...

  5. 2015 多校赛 第三场 1002 (hdu 5317)

    Description Mr. Hdu is interested in Greatest Common Divisor (GCD). He wants to find more and more i ...

  6. hdu5379||2015多校联合第7场1011 树形统计

    pid=5379">http://acm.hdu.edu.cn/showproblem.php? pid=5379 Problem Description Little sun is ...

  7. hdu 5373 The shortest problem(杭电多校赛第七场)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5373 The shortest problem Time Limit: 3000/1500 MS (J ...

  8. 牛客网多校赛第七场J--Sudoku Subrectangle

    链接:https://www.nowcoder.com/acm/contest/145/J 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言6553 ...

  9. 牛客网多校赛第七场--C Bit Compression【位运算】【暴力】

    链接:https://www.nowcoder.com/acm/contest/145/C 来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 262144K,其他语言524 ...

随机推荐

  1. 用u盘安装黑苹果10.12.3

    链接: https://pan.baidu.com/s/1eR9GgwE 密码: rubh 主机和显示器必须是数字口连接,如dvi.displayport,VGA不能进安装界面 下载苹果镜像文件10. ...

  2. 【Linq】

    " }; var l1 = strs.ToLookup(a => "a"); //Key=a elements=1,3 var l2 = strs.ToLookup ...

  3. C# 泛基

    1 你有时候希望在父类规定一些行为,让子类无法修改,但是这些实现是依赖一个子类才能获取的值,你又不可能知道所有的子类 ,没办法替它在父类里面初始化,这时候就需要在父类里面定义一个每个子类一个的,但又是 ...

  4. Silverlight之我见——数据批示(2)

    接着上一回的话题,继续来研究数据批示特性,先拿简单的RageAttribute来弄弄,接着上次的示例,添加一个Age属性,并加上RangeAttribute. [Range(20,60,ErrorMe ...

  5. 修改bash命令提示符

    说明:PS1是主要的提示符设置,在ubuntu一般为: ${debian_chroot:+($debian_chroot)}\u@\h:\w\$ 具体的提示符,按分类含义如下: 主要信息: \u 当前 ...

  6. 【郑轻邀请赛 A】tmk射气球

    [题目链接]:https://acm.zzuli.edu.cn/zzuliacm/problem.php?id=2127 [题意] [题解] 把气球和飞艇所代表的直线投影到xoy面上 设气球所在位置为 ...

  7. 常用Git命令大全思维导图

    开发中代码管理少不了使用Git,对于初学者来说Git命令的学习是一个难过的坎,为了帮助大家记忆并快速掌握Git的基本使用,我把常用的Git命令整理成思维导图,分享给大家. 高清大图在线预览 http: ...

  8. Python 2 声明变量 输入输出 练习

    变量: 代指,用于将具体信息对应到一个值,便于反复使用时方便调用.例如  name = ("斯诺登")   变量声明规则:以字母开头的 字母数字下划线的组合.且不能是python代 ...

  9. [bzoj1923]外星千足虫[高斯消元]

    高斯消元解异或方程组 #include <iostream> #include <algorithm> #include <cstdio> #include < ...

  10. 【DEBUG】 Visual Studio 2005 DEBUG集

    一. fatal error C1083: 无法打开包括文件:"stdint.h": No such file or directory stdint.h是c99标准的头文件,vc ...