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)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. Codeforces Round #382 (Div. 2) 继续python作死 含树形DP

    A - Ostap and Grasshopper zz题能不能跳到  每次只能跳K步 不能跳到# 问能不能T-G  随便跳跳就可以了  第一次居然跳越界0.0  傻子哦  WA1 n,k = map ...

  6. Codeforces Round #263 Div.1 B Appleman and Tree --树形DP【转】

    题意:给了一棵树以及每个节点的颜色,1代表黑,0代表白,求将这棵树拆成k棵树,使得每棵树恰好有一个黑色节点的方法数 解法:树形DP问题.定义: dp[u][0]表示以u为根的子树对父亲的贡献为0 dp ...

  7. 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 ...

  8. Codeforces Round #564 (Div. 1)

    Codeforces Round #564 (Div. 1) A Nauuo and Cards 首先如果牌库中最后的牌是\(1,2,\cdots, k\),那么就模拟一下能不能每次打出第\(k+i\ ...

  9. 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 ...

随机推荐

  1. ios开发ARC,IBOutlets之strong与weak

    今天在写程序的时候,用IBOutlets连了一个自定义的控件,出现了问题,后面访问的时候,控件里有些subviews没有初始化好,取到的时候为nil, 程序里用了ARC, IBOutlets一连接上, ...

  2. jq处理JSON数据, jq Manual (development version)

    jq 允许你直接在命令行下对 JSON 进行操作,包括分片.过滤.转换等等.让我们通过几个例子来说明 jq 的功能:一.输出格式化,漂亮的打印效果如果我们用文本编辑器打开 JSON,有时候可能看起来会 ...

  3. DELL(linux 系统里系统掉盘)(阵列Foreign)命令行里重做阵列

    故障现象 (阵列Foreign 系统下重做阵列) 例:四合一机器(DELL_XENCOMPA09) 有四个硬盘 如图 少认到一个 df  -h fdisk -l 2>/dev/null | gr ...

  4. Directx教程(22) 简单的光照模型(1)

    原文:Directx教程(22) 简单的光照模型(1)      在前面的教程中,我们在顶点属性中直接给顶点赋颜色,这样生成的三维物体缺乏真实感,如下图中两个立方体,左边的是通过光照生成物体表面颜色的 ...

  5. vuxdemo1

    //main.js import Vue from 'vue' import router from './router' import store from './store' import axi ...

  6. RNN与 LSTM 网络

    循环神经网络(RNN) 人们的每次思考并不都是从零开始的.比如说你在阅读这篇文章时,你基于对前面的文字的理解来理解你目前阅读到的文字,而不是每读到一个文字时,都抛弃掉前面的思考,从头开始.你的记忆是有 ...

  7. python 缓冲区 subprocess 黏包 黏包解决方案

    一.缓冲区 二.两种黏包现象 两种黏包现象: 1 连续的小包可能会被优化算法给组合到一起进行发送 黏包现象1客户端 import socket BUFSIZE = 1024 ip_prort = (' ...

  8. a configuration error occured during startup.please verify the preference field with the prompt:

    Window->Preferences->MyEclipse Enterprice Workbench->Servers->Tomcat->选择你的Tomcat(比如To ...

  9. UVA_401:Palindromes

    AC:Time(29ms)     C++ 4.8.2 #include<stdio.h> #include<string.h> char * mirstr = "A ...

  10. 用GitHub Pages搭了个博客,欢迎来玩~

    Welcome to visit my new blog https://luoxiaolei.github.io/ Ps. 后续的blog会优先更新到GitHub Pages上.