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 ...
随机推荐
- 洛谷 P1073 最优贸易 最短路+SPFA算法
目录 题面 题目链接 题目描述 输入输出格式 输入格式 输出格式 输入输出样例 输入样例 输出样例 说明 思路 AC代码 题面 题目链接 P1073 最优贸易 题目描述 C国有 $ n $ 个大城市和 ...
- 大数据技术之Kafka
Kafka概述 1.1 消息队列 (1)点对点模式(一对一,消费者主动拉取数据,消息收到后消息清除) 点对点模型通常是一个基于拉取或者轮询的消息传送模型,这种模型从队列中请求信息,而不是将消息 ...
- 利用UIWebView打造一个炫酷的视频背景视图(OC & Swift)
http://www.cocoachina.com/ios/20151023/13860.html 2015-10-6更新:适配 Swift2.0 如有需要,可以通过pjin.elvin@gmail. ...
- python 同名变量引用
- Person Re-identification 系列论文笔记(八):SPReID
Human Semantic Parsing for Person Re-identification Kalayeh M M, Basaran E, Gokmen M, et al. Human S ...
- Ubuntu官方源
由于国内使用官方源导致下载速度慢等各方面原因,很多人将官方源替换成阿里源等,但是某些应用安装会报错.现将官方源分享如下: 备份当前源: 执行命令:# sudo cp /etc/apt/sources. ...
- css面试题总结(转)
转自此网页http://www.cnblogs.com/YangqinCao/p/5721810.html. 1.两栏布局,左边栏宽度固定,适应父元素高度变化 首先分析两栏布局, 两栏布局两种常见方法 ...
- Chrome的使用技巧总结
设置一>” 首先设置打开特定的网页 设置--> “下载位置” (Ctrl+H),快速查找自己浏览器访问网页的历史记录 (Ctrl+D),将目前认为比较好的网页保存. ctrl+t 新建标 ...
- MUI - 关于百度定位
关于百度定位 这是官方定位的解释:geolocation 地图插件配置 我在问答里面找到了这位童鞋的百度定位,地址变更提醒 *** 不过,童鞋倒是给具体的示例啊,木有~~~~(>_<)~~ ...
- linux 系统下如何进行用户之间的切换
切换用户的命令是su,su是(switch user)切换用户的缩写.通过su命令,可以从普通用户切换到root用户,也可以从root用户切换到普通用户.从普通用户切换到root用户需要密码(该密码是 ...