题意

讲不太清楚,看英文吧

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. 转:RBAC如何设计一个权限系统

    前言 权限管理是所有后台系统的都会涉及的一个重要组成部分,主要目的是对不同的人访问资源进行权限的控制,避免因权限控制缺失或操作不当引发的风险问题,如操作错误,隐私数据泄露等问题.目前在公司负责权限这块 ...

  2. ELF文件之九——使用链接脚本-2个函数-data-bss-temp-call-debug信息-struct

    main.c int enable; ; struct aaa { int membera; char memberb; }s_aaa; int main() { int temp; add(); d ...

  3. tomcat 安装在 linux

    简单说下什么是tomcat?它与apache web服务器的关系? Apache是web服务器(静态解析,如HTML),tomcat是java应用服务器(动态解析,如JSP.PHP) Tomcat只是 ...

  4. pt-query-digest 慢日志监控

    一.安装percona-toolkit,以centos为例 yum -y install https://repo.percona.com/yum/percona-release-latest.noa ...

  5. getElementsByName和getElementById获取控件

    js对控件的操作通常使用getElementsByName或getElementById来获取不同的控件进行操作 getElementsByName() 得到的是一个array, 不能直接设value ...

  6. Mysql无法启动、闪退

    一.mysql下载完成后,运行bin目录下的mysql.exe,提示文件缺失   二.此报错为VC运行库不全或没有安装导致,百度搜索“微软常用运行库合集”进行下载安装即可解决   三.运行时闪退,无法 ...

  7. debian 10安装英伟达独显驱动

    我的显卡是GTX1050TI,刚安装好Debian 10的时候启动会黑屏,无法进入系统,解决办法是在grub界面,按e修改启动参数,在启动参数那一行(一般会包含quiet)后面加上 nouveau.m ...

  8. C#设计模式学习笔记:(13)模板方法模式

    本笔记摘抄自:https://www.cnblogs.com/PatrickLiu/p/7837716.html,记录一下学习过程以备后续查用. 一.引言 今天我们要讲行为型设计模式的第一个模式--模 ...

  9. 正则表达式过滤html注释内容

    Regex.Replace("<!--(.|[\r\n])*?-->",string.Empty)

  10. 医院信息集成平台(ESB)实施、建设方案

    医院信息集成平台(ESB)实施.建设方案 基于中立.标准.开放的IT架构和数据标准,打造插拔式医院应用生态. 解决方案 基于ESB集成总线,构建医院信息化建设顶层设计.                 ...