Codeforces Round #564 (Div. 2) D. Nauuo and Circle(树形DP)
D. Nauuo and Circle
•参考资料
[1]:https://www.cnblogs.com/wyxdrqc/p/10990378.html
•题意
给出你一个包含 n 个点的树,这 n 个点编号为 1~n;
给出一个圆,圆上放置 n 个位置,第 i 个位置对应树中的某个节点,并且不重复;
求在圆上还原这棵树后,使得边不相交的总方案数;
•题解
①为何每一颗子树一定是连续的一段圆弧?
假设不是连续的圆弧,如图所示:
为了使 x 接到树上,必然会有 x-y 或 x-z 相连的边,这样就会出现交点;
②对于以 u 为根的子树,假设 u 有两个儿子 a,b,那么,需要找连续的 x+y+1 个位置放置这些节点;
(x:以a为根节点的子树节点个数,y:以b为根节点的子数的节点个数)
也就是图中的sum1,sum2,sum3位置;
u可以放在这三个位置的任意一个位置,a 从剩余的两个位置中选,b只有一个位置可选;
总的方案数为 3!;
但是每个方案中 a,b 都有排列方案,故需要乘上 fa×fb;
对于树的根节点 1,一共有 n 个位置可放,求出其中一个的方案数 f1,答案就是 n×f1;
•Code
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define memF(a,b,n) for(int i=0;i <= n;a[i++]=b);
const int maxn=2e5+;
const int MOD=; int n;
int num;
int head[maxn];
struct Edge
{
int to,next;
}G[maxn<<];
void addEdge(int u,int v)
{
G[num]=Edge{v,head[u]};
head[u]=num++;
}
ll fact[maxn];
ll dp[maxn];///与f函数功能相同
vector<int >son[maxn];
void DFS(int u,int f)
{
for(int i=head[u];~i;i=G[i].next)
{
int v=G[i].to;
if(v == f)
continue; son[u].push_back(v);
DFS(v,u);
} int k=son[u].size()+(u != ? :);///如果u=1就不用再找u可放置的位置,因为1已经被固定了
dp[u]=fact[k];
for(int i=;i < son[u].size();++i)
dp[u]=dp[u]*dp[son[u][i]]%MOD;
}
ll Solve()
{
for(int i=;i <= n;++i)
son[i].clear(); DFS(,); return dp[]*n%MOD;
}
void Init()
{
num=;
memF(head,-,n);
fact[]=;
for(int i=;i <= n;++i)
fact[i]=(i*fact[i-])%MOD;
}
int main()
{
scanf("%d",&n);
Init();
for(int i=;i < n;++i)
{
int u,v;
scanf("%d%d",&u,&v);
addEdge(u,v);
addEdge(v,u);
}
printf("%lld\n",Solve());
return ;
}
Codeforces Round #564 (Div. 2) D. Nauuo and Circle(树形DP)的更多相关文章
- Codeforces Round #564 (Div. 2) C. Nauuo and Cards
链接:https://codeforces.com/contest/1173/problem/C 题意: Nauuo is a girl who loves playing cards. One da ...
- Codeforces Round #564 (Div. 2) B. Nauuo and Chess
链接:https://codeforces.com/contest/1173/problem/B 题意: Nauuo is a girl who loves playing chess. One da ...
- Codeforces Round #564 (Div. 2) A. Nauuo and Votes
链接:https://codeforces.com/contest/1173/problem/A 题意: Nauuo is a girl who loves writing comments. One ...
- Codeforces Round #196 (Div. 2) D. Book of Evil 树形dp
题目链接: http://codeforces.com/problemset/problem/337/D D. Book of Evil time limit per test2 secondsmem ...
- Codeforces Round #382 (Div. 2) 继续python作死 含树形DP
A - Ostap and Grasshopper zz题能不能跳到 每次只能跳K步 不能跳到# 问能不能T-G 随便跳跳就可以了 第一次居然跳越界0.0 傻子哦 WA1 n,k = map ...
- Codeforces Round #263 Div.1 B Appleman and Tree --树形DP【转】
题意:给了一棵树以及每个节点的颜色,1代表黑,0代表白,求将这棵树拆成k棵树,使得每棵树恰好有一个黑色节点的方法数 解法:树形DP问题.定义: dp[u][0]表示以u为根的子树对父亲的贡献为0 dp ...
- Codeforces Round #419 (Div. 1) C. Karen and Supermarket 树形DP
C. Karen and Supermarket On the way home, Karen decided to stop by the supermarket to buy some g ...
- Codeforces Round #564 (Div. 1)
Codeforces Round #564 (Div. 1) A Nauuo and Cards 首先如果牌库中最后的牌是\(1,2,\cdots, k\),那么就模拟一下能不能每次打出第\(k+i\ ...
- Codeforces Round #267 (Div. 2) C. George and Job(DP)补题
Codeforces Round #267 (Div. 2) C. George and Job题目链接请点击~ The new ITone 6 has been released recently ...
随机推荐
- JSP Web第七章整理复习 Servlet基础知识
P206-208 Servlet项目的创建,web.xml的配置及标签含义,相关程序 创建:new 一个Servlet类,继承自javax.servlet.http.HttpServlet; 写doG ...
- input的表单验证(不断更新中~~)
1 手机号验证 <input type="tel" id="phone" name="phone" placeholder=" ...
- JavaScript--函数对象的属性caller与callee
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- MAC+VS Code+Python+Markdown调试配置
目录 VS Code官网下载 VS Code插件推荐 VS Code Python环境配置 Markdown配置 VS Code官方文档 VS Code官网下载 VS Code官网下载地址 VS Co ...
- MySQL非安装版安装
1 数据库的打开方式(非安装版本) 1.解压mysql-5.7.12-winx64.zip到一个路径上没有空格没有汉字的目录中 2.复制my-default.ini重命名为my.ini 3.命令行进入 ...
- C++ 引用#include<math.h> 找不到动态库
问题: 使用g++ 编译C++文件报错了,无法识别abs,可是我这文件中已经添加了#include<math.h>? 于是在指令中加入-lm g++ main.cpp AStar.cpp ...
- 使用php实现单点登录实例详解
1.首先准备两个虚拟域名 127.0.0.1 www.openpoor.com 127.0.0.1 www.myspace.com 2.在openpoor的根目录下创建以下文件 index.php文件 ...
- SWF在线绘本批量制作高质量PDF的新方法(重点在批量制作)
SWF在线绘本批量制作高质量PDF的新方法(重点在批量制作) 2012-12-21 未来决定... http://www.ebama.net/thread-107643-1-1.html ...
- day4_python-之装饰器、迭代器、生成器
一.装饰器 1.为何要用装饰器 #开放封闭原则:对修改封闭,对扩展开放 2. 什么是装饰器 装饰器他人的器具,本身可以是任意可调用对象,被装饰者也可以是任意可调用对象. 强调装饰器的原则:1 不修改被 ...
- 纯CSS3打造圆形菜单
原理是使用相对定位和绝对定位确定圆形菜单位置. 使用伪类选择器E:hover确定悬浮时候的效果,动画效果用CSS3的transition属性. 大概代码如下. html: <div id=&qu ...


