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. AC自动机fail树小结

    建议大家学过AC自动机之后再来看这篇小结 fail树就是讲fail指针看做一条边连成的树形结构 fail指针在AC自动机中的含义是指以x为结尾的后缀在其他模式串中所能匹配的最长前缀的长度 所以在模式串 ...

  2. 【风马一族_SQL Server】

    原文来自:http://www.cnblogs.com/sows/p/6097684.html (博客园的)风马一族 侵犯版本,后果自负 2016-11-24  14:25:45 命令行方式处理服务管 ...

  3. Linux常用命令4 帮助命令

    1.帮助命令:man 命令英文原意:manual 命令所在路径:/usr/bin/man    执行权限:所有用户 语法:man [命令或配置文件] 功能描述:获得命令或者配置文件的帮助信息 例如:m ...

  4. C++:static类

    static自我理解 static使得数据成员或者函数生命周期为整个文件所在程序的生命周期, 在C中还可以用它避免被其它文件使用为外部成员 static类 明确:类的静态数据成员它被所有类对象共享,但 ...

  5. Linux终端常用命令(一)

    基本操作 展示全部的环境变量 export 搜索可执行文件.源文件 whereis ls 在环境变量中搜索可执行文件,并打印完整路径 which ls 展示用户命令,系统调用.库函数等 whatis ...

  6. poj 2385【动态规划】

    poj 2385 Apple Catching Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14007   Accepte ...

  7. postman测试接口各种类型传值

    postman测试接口各种类型传值 标签: postman测试 json串 Map 2018年01月27日 02:32:00 145人阅读 评论(0) 收藏 举报 1.Map类型或实体类类型传值,即j ...

  8. HZOJ 连连看

    考场几乎想到了正解,然而我也不知道当时在想啥,在没有证伪的情况下只是觉得无法实现就否了…… 最后打的好象是达哥说的O(4*15*n*m),复杂度不是很会证反正T成了暴力…… 题解: 对于测试点8,9, ...

  9. AutoCAD安装失败怎样卸载重新安装AutoCAD,解决AutoCAD安装失败的方法总结

    技术帖:AutoCAD没有按照正确方式卸载,导致AutoCAD安装失败.楼主也查过网上关于如何解决AutoCAD安装失败的一些文章,是说删除几个AutoCAD文件和AutoCAD软件注册表就可以解决A ...

  10. Istio on ACK集成生态(2): 扩展AlertManager集成钉钉助力可观测性监控能力

    阿里云容器服务Kubernetes(简称ACK)支持一键部署Istio,可以参考文档在ACK上部署使用Isito.Istio on ACK提供了丰富的监控能力,为网格中的服务收集遥测数据,其中Mixe ...