题意

讲不太清楚,看英文吧

cf

做法

在正式开始之前,我们先来玩一玩性质

首先考虑全\(0\)的情况,即本质不同的方案数

性质1:方案数并不为(n-1)!,即方案与结果不为双射

考虑一条边将树分为两个联通块,之间是互不影响的

结论1:方案数为\(\prod\limits (deg_i!)\)

考虑节点\(u\)的父节点与其父亲这条边已经选过

对于每种除\(u\)的儿子外的边固定时,\(u\)的儿子边的全排列决定了其对子树值的影响

ps:实际上这是无根树,但并不影响映射关系

性质2:若\(n\ge 2\),\(a_i\neq i\)

结论2:若知晓所有限制,\(\sum\limits dis(i,a_i)=2(n-1)\)

具体考虑\(a_v=u\)的限制,有以下结论

  • 存在相对顺序边集\((u,t_1)(t_1,t_2)...(t_k,v)\)
  • \((u,t_1)\)为\(u\)的第一条边
  • \((t_k,v)\)为\(v\)的最后一条边
  • \((t_{i-1},t_{i})(t_i,t_{i+1})\)为\(t_i\)严格顺序的边

我们可以依此建立顺序,合法的方案,对于某个点,有以下限制

  • 不可破坏简单顺序
  • 不可生成环,即为一种非自然的复杂顺序
  • 若有严格顺序从第一条边到最后一条边,中间不可漏边

方案数,对于无限制的边随便选择顺序

题外话

这题很妙,虽然容易理解,但需要想满条件还是需要时间的,由于没出现什么算法,强行归到拓扑排序上了

code(std)

#include <bits/stdc++.h>

using namespace std;

const int mod = 1e9 + 7;
const int maxn = 500005; int n;
vector <int> adj[maxn];
int a[maxn];
int dad[maxn];
int h[maxn];
vector <pair <int, int> > conditions[maxn];
int nxt[maxn], prv[maxn], seen[maxn]; void no(int ncase) {
cerr << ncase << endl;
cout << 0 << endl;
exit(0);
} void dfs(int u) {
for (auto v: adj[u]) if (v != dad[u]) {
dad[v] = u;
h[v] = h[u] + 1;
dfs(v);
}
} int cnt; ///total distance, must not be more than 2n-2 void go(int u, int v) {
if (u == v) no(0);
vector <int> from_u, to_v;
///naive LCA works here as long as we exit upon finding a conflict
from_u.push_back(n + 1); ///first edge (fake)
to_v.push_back(n + 2); ///last edge (fake)
while (h[u] > h[v]) {
from_u.push_back(u);
u = dad[u];
}
while (h[v] > h[u]) {
to_v.push_back(v);
v = dad[v];
}
while (u != v) {
from_u.push_back(u);
u = dad[u];
to_v.push_back(v);
v = dad[v];
}
from_u.push_back(u);
from_u.insert(from_u.end(), to_v.rbegin(), to_v.rend());
for (int i = 1; i + 1 < from_u.size(); ++i)
conditions[from_u[i]].push_back({from_u[i-1], from_u[i+1]});
cnt += from_u.size() - 3;
if (cnt > 2 * n - 2) no(0); ///important break
} int main(void) {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cin >> n;
for (int i = 1; i < n; ++i) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
for (int i = 1; i <= n; ++i) {
adj[i].push_back(n + 1); ///first edge (fake)
adj[i].push_back(n + 2); ///last edge (fake)
}
for (int i = 1; i <= n; ++i) cin >> a[i]; if (n == 1) {
cout << 1 << endl;
exit(0);
} dfs(1);
for (int i = 1; i <= n; ++i) if (a[i] != 0) go(i, a[i]); ///answer 0 if:
///1. there are 2 or more incoming/outgoing
///conditions to/from an edge, or
///2. there is a cycle, or
///3. first (n+1) is connected to last (n+2),
///but does not contain all other edges.
int ans = 1;
for (int i = 1; i <= n; ++i) {
///check case 1
for (auto edge: conditions[i]) {
int u = edge.first, v = edge.second;
if (nxt[u] && nxt[u] != v) no(1);
if (prv[v] && prv[v] != u) no(1);
nxt[u] = v;
prv[v] = u;
}
///check case 2
for (auto u: adj[i]) if (!seen[u]) {
seen[u] = 1;
int cur = nxt[u];
while (cur) {
if (cur == u) no(2);
if (seen[cur]) break;
seen[cur] = 1;
cur = nxt[cur];
}
}
///check case 3
if (nxt[n+1]) {
int cur = n + 1, all = 1;
while (cur) {
if (cur == n + 2) break;
++all;
cur = nxt[cur];
}
if (cur == n + 2 && all < adj[i].size()) no(3);
}
///all good - for now
int free = 0;
for (auto u: adj[i]) if (u <= n && !prv[u]) ///fake edges doesn't count
++free;
if (prv[n+2]) --free; ///connected to last edge => not free
for (int j = 1; j <= free; ++j) ans = 1ll * ans * j % mod;
///reset
for (auto u: adj[i]) nxt[u] = prv[u] = seen[u] = 0;
} ///no conflicts
cout << ans << endl;
return 0;
}

CF1254E Send Tree to Charlie的更多相关文章

  1. 应用监控CAT之cat-client源码阅读(一)

    CAT 由大众点评开发的,基于 Java 的实时应用监控平台,包括实时应用监控,业务监控.对于及时发现线上问题非常有用.(不知道大家有没有在用) 应用自然是最初级的,用完之后,还想了解下其背后的原理, ...

  2. 深入详解美团点评CAT跨语言服务监控(三)CAT客户端原理

    cat客户端部分核心类 message目录下面有消息相关的部分接口 internal目录包含主要的CAT客户端内部实现类: io目录包含建立服务端连接.重连.消息队列监听.上报等io实现类: spi目 ...

  3. 大众点评CAT开源监控系统剖析

    参考文档: 大众点评的实时监控系统分析(一) CAT_source_analyze 透过CAT,来看分布式实时监控系统的设计与实现 深度剖析开源分布式监控CAT [分布式监控CAT] Client端源 ...

  4. [python]MS17-010自动化扫描脚本

    一种是3gstudent分享的调用Nsa泄露的smbtouch-1.1.1.exe实现验证,另一种是参考巡风的poc.这里整合学习了下两种不同的方法. import os import fileinp ...

  5. 【转】大众点评CAT开源监控系统剖析

    https://www.cnblogs.com/yeahwell/p/cat.html 参考文档: 大众点评的实时监控系统分析(一) CAT_source_analyze 透过CAT,来看分布式实时监 ...

  6. Flex之DataGrid和Tree控件的数据源XML格式

    1.flex的完整代码: <?xml version="1.0" encoding="utf-8"?> <s:Application xmln ...

  7. [Webpack 2] Tree shaking with Webpack 2

    The less code you can send to the browser, the better. The concept of tree shaking basically says th ...

  8. LINQ Expresstion Tree 表达式树

    Expression trees represent code in a tree-like data structure, where each node is an expression, for ...

  9. Show tree of processes in linux

    pstree(1): tree of processes - Linux man pagehttps://linux.die.net/man/1/pstree How to view process ...

随机推荐

  1. Go语言实现:【剑指offer】替换空格

    该题目来源于牛客网<剑指offer>专题. 请实现一个函数,将一个字符串中的每个空格替换成"%20". 例如,当字符串为We Are Happy.则经过替换之后的字符串 ...

  2. java8种基本数据类型

  3. 在C#中通过使用Newtonsoft.Json库来解析百度地图地理编码(GeoCoder)服务接口返回的Json格式的数据

    百度地图地理编码(GeoCoder)服务接口返回的Json格式的数据,如下所示: http://api.map.baidu.com/geocoding/v3/?address=**省**市**区**路 ...

  4. sparc v8 stack frame calling convention

    main.c ; int main() { int a, b; int sum; a = ; b = ; sum = add(a, b); ; } int add(int a, int b) { in ...

  5. 一步步搭建jumpserver

    测试推荐环境 CPU: 64位双核处理器 内存: 4G DDR3 数据库:mysql 版本大于等于 5.6 mariadb 版本大于等于 5.5.6 环境 系统: CentOS 7 IP: 192.1 ...

  6. 14.git的安装使用

    目录 一.版本控制器 二.git 简介 git与svn比较 git的工作流程 版本库间的通信 git分支管理 三.git使用 流程(核心总结) 安装 基础命令 将已有的文件夹 - 初始化为git仓库 ...

  7. 普通键盘Windows上虚拟Cherry机械键盘效果的方法

    草台班子--普通键盘Windows上虚拟Cherry机械键盘效果的方法    ​ 机械键盘以其独特的手感.绚丽的外形,还有那人神共愤的音效吸引着大批爱好者.最近iQQO 3的机械键盘效果更是吸引了更多 ...

  8. AMD R2600+微星B450迫击炮配置的新工作机,分享给大家

    上个月,突然觉得自己总做用的电脑有点老了,虽然很不舍陪自己战斗了3,4年的老战士,下了很大的决心,才决定搞一台新的吧,虽然新电脑的配置也不算非常高,但是用于开发的话,也算不错的选择了,特此分享一下.又 ...

  9. 删掉以前的旧Flow,创作现在的新节奏

    2017年开始实习,现已2020年.三年又三年.今天我删掉无知的从前,进入新世界. 无论活的多累 做人不进则退 只能自我激励 将这当做基地

  10. 【iOS】Spring Animations (弹性动画)

    This interface shows how a spring animation can be created by specifying a “damping” (bounciness) an ...