题意:给定一棵树,树上有 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. android studio 创建第一个app之hello world

    android studio 创建第一个app之hello world 想要用studio创建一个简单的app,结果遇到各种问题,application就是允许不起来,后来在专业人的帮助下,删除了一些 ...

  2. openoffice启动服务并将office文件转换为pdf文件

    1.首先下载最新版的openoffice工具,安装完成之后安装服务,, win+r打开命令提示符 输入cmd,cd C:\Program Files (x86)\OpenOffice 4\progra ...

  3. generating multiple ordered files in python

    Goal: To generate =35= files named 'capitalsquiz1.txt', 'capitalsquiz2.txt'...'capitalsquiz35.txt' * ...

  4. PAT 1111 Online Map

    Input our current position and a destination, an online map can recommend several paths. Now your jo ...

  5. TortoiseGit配置密钥的方法

    TortoiseGit 使用扩展名为ppk的密钥,而不是ssh-keygen生成的rsa密钥.使用命令ssh-keygen -C "邮箱地址" -t rsa产生的密钥在Tortoi ...

  6. 【codeforces 767D】Cartons of milk

    [题目链接]:http://codeforces.com/problemset/problem/767/D [题意] 每个牛奶都有最晚可以喝的时间; 每天喝K瓶牛奶; 你有n瓶牛奶->已知每个牛 ...

  7. RestEasy 用户指南---第11章 @FormParam

    转载说明出处:http://blog.csdn.net/nndtdx/article/details/6870391 原文地址 http://docs.jboss.org/resteasy/docs/ ...

  8. hdu 4171 最短路

    #include<stdio.h> #include<string.h> #include<queue> #include<iostream> usin ...

  9. 用循环链表实现Josephus问题

    Josephus问题:设有n个人围坐在一个圆桌周围,现从第s个人开始报数,数到第m的人出列,然后从出列的下一个人重新开始报数,数到第m的人又出列.如此反复直到所有的人全部出列为止. 思路:构建一个没有 ...

  10. Mac OSX:最简单的安装ant方法

    安装ant最简单的方法就是通过brew.步骤如下:1. 安装brew(如果已经安装可以跳过这步).ruby -e "$(curl -fsSL https://raw.github.com/m ...