题意:给定一棵树,树上有 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. table头部固定,内容滚动

    可以设置两个table,th,td得设置宽度:     <table>       <thead>          <tr><th></th&g ...

  2. CAD绘一个文字自动剧中的标注 (com接口)

    主要用到函数说明: _DMxDrawX::DrawDimRotated 绘制一个线型标注.详细说明如下: 参数 说明 DOUBLE dExtLine1PointX 输入第一条界线的起始点X值 DOUB ...

  3. HTML5轻松实现全屏视频背景

    想在你的网页首页中全屏播放一段视频吗?而这段视频是作为网页的背景,不影响网页内容的正常浏览.那么我告诉你有一款Javascript库正合你意,它就是Bideo.js. 参考网址: https://ww ...

  4. Luogu P2068 统计和

    P2068 统计和 题目描述 给定一个长度为n(n<=100000),初始值都为0的序列,x(x<=10000)次的修改某些位置上的数字,每次加上一个数,然后提出y (y<=1000 ...

  5. opcache的配置

    ; Enable Zend OPcache extension module zend_extension=opcache.so ; Determines if Zend OPCache is ena ...

  6. gitblit 搭建本地git服务器

    本文主要描述gitblit搭建本地服务器

  7. Android传递Bitmap的两种简单方式及其缺陷

    Android传递Bitmap的几种简单方式 一,通过Intent的Bundle. 比如有两个activity,A,B,从A进入B.先在A中将Bitmap写进去: Resources res=getR ...

  8. HyperLedger项目以及社区

    本文不涉及任何技术开发的内容,仅供你跟同学.同事吹牛B之用.就像很多牛人总爱讲历史典故一样. 一.诞生与现状 HyperLedger 诞生于2015年12月17日,HyperLedger 追寻Apac ...

  9. LucaCanali --DTRACE AND SYSTEMTAP 脚本工具

    https://github.com/LucaCanali http://cern.ch/canali/

  10. Atom编辑器折腾记_(15)JS代码片段补全(插件:javascript-snippets)

    题外话 这款插件就比較重量级了-.用熟悉了写原生JS的效率要提升非常多--并且,不仅支持JS还包括了nodejs snippet javascript-snippets 插件作者: zenorocha ...