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 ...
随机推荐
- [已转移]js事件流之事件冒泡的应用----事件委托
该文章已转移到博客:https://cynthia0329.github.io/ 什么是事件委托? 它还有一个名字叫事件代理. JavaScript高级程序设计上讲: 事件委托就是利用事件冒泡,只指定 ...
- iOS 9 学习系列:Split Screen Multitasking
http://www.cocoachina.com/ios/20151010/13601.html iOS 9 的一个重大变化就是增加了多任务,这个多任务允许用户在屏幕上同时运行多个 app.有两种形 ...
- AGC029 E: Wandering TKHS
E: Wandering TKHS - AtCoder Grand Contest 029 | AtCoder 分类讨论好题(也不太算分类讨论) 方法:感受过程手玩,考虑能不能提前预算一些东西,或者 ...
- flask 与celery
在flask 中使用celery 是特别简单的,celery官网都没有特别介绍如何使用. 使用celery首先要知道怎么配置celery. 1. 实例化celery celery = Celery ...
- VS中,卸载,移除,删除项目的区别
不说话,上图.
- MyEclipse代码提示功能和自动提示功能
1.菜单window->Preferences->Java->Editor->Content Assist->Enable auto activation 选项要打上勾 ...
- Java面向对象----接口概念
接口语法 interface 接口名{ //静态常量,抽象方法 } 特点 接口中只能存放静态常量和抽象方法 java接口是对功能的扩展 通过实现接口,java类可以实现多实现 一个类可以同时继承(ex ...
- Laravel 发送邮件(最简单的讲解!)
Laravel集成了SwiftMailer库进行邮件发送,邮件配置文件位于config/mail.php:. return [ 'driver' => env('MAIL_DRIVER', 's ...
- javascript实现html中关键字查询
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- iptables 限制访问规则
iptables -I INPUT 1 -m state --state RELATED,ESTABLISHED -j ACCEPT把这条语句插在input链的最前面(第一条),对状态为ESTABLI ...


