当前位置:网站首页>Prufer序列
Prufer序列
2022-08-01 21:59:00 【ThXe】
Prufer性质
Prufer数列是无根树的一种数列。在组合数学中,Prufer数列由有一个对于顶点标过号的树转化来的数列,点数为n的树转化来的Prufer数列长度为n-2
1.若编号i在Prufer序列出现cnt次,那么它在的度数为cnt+1
2.n个点无标号无根树的形态有nn-2种
3.n个点有标号无根树形态有:
4:
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 5e6 + 50;
typedef long long LL;
LL N, M, Ans = 0, father[MAXN], Prufer[MAXN], du[MAXN];//father数组即题目中的f数组
LL read() {
//快读
LL cnt = 0, flag = 1;
char c = getchar();
while (c < '0' || c>'9') {
if (c == '-') flag = -1; c = getchar(); }
while (c >= '0' && c <= '9') cnt = (cnt << 1) + (cnt << 3) + (c ^ 48), c = getchar();
return flag * cnt;
}
void Tree_to_Prufer() {
for (int i = 1; i <= N - 1; i++) father[i] = read(), du[father[i]]++;//将父节点的度加一
for (int i = 1, now = 1; i <= N - 1; i++, now++) {
while (du[now]) now++;//找到编号最小的叶节点
Prufer[i] = father[now];//加入Prufer序列
while (i < N - 2 && !--du[Prufer[i]] && Prufer[i] < now) Prufer[i + 1] = father[Prufer[i]], i++;//当父亲节点编号较小时,继续将父亲节点加入Prufer序列
}
for (int i = 1; i <= N - 2; i++) Ans ^= i * Prufer[i];//计算权值
}
void Prufer_to_Tree() {
for (int i = 1; i <= N - 2; i++) Prufer[i] = read(), du[Prufer[i]]++;//将父节点的度加一
Prufer[N - 1] = N;//这里需要特别注意一下
for (int i = 1, now = 1; i <= N - 1; i++, now++) {
while (du[now]) now++;//找到编号最小的叶节点
father[now] = Prufer[i];//加入父亲数组
while (i < N - 1 && !--du[Prufer[i]] && Prufer[i] < now) father[Prufer[i]] = Prufer[i + 1], i++;//当父亲节点编号较小时,继续将父亲节点加入父亲数组
}
for (int i = 1; i <= N - 1; i++) Ans ^= i * father[i];//计算权值
}
int main() {
memset(du, 0, sizeof(du));
N = read(); M = read();
if (M == 1) Tree_to_Prufer();
else Prufer_to_Tree();
printf("%lld\n", Ans);
return 0;
}
边栏推荐
- AI应用第一课:支付宝刷脸登录
- leetcode 204. Count Primes 计数质数 (Easy)
- HCIP---Multiple Spanning Tree Protocol related knowledge points
- Delicious this year
- 用户体验 | 如何度量用户体验?
- FusionGAN:A generative adversarial network for infrared and visible image fusion article study notes
- Based on php tourism website management system acquisition (php graduation design)
- ARFoundation Getting Started Tutorial U2-AR Scene Screenshot Screenshot
- Analysis of the development trend of game metaverse
- 上传markdown文档到博客园
猜你喜欢

Homework 8.1 Orphans and Zombies

Based on php tourism website management system acquisition (php graduation design)

Unity Shader 常规光照模型代码整理

ImportError: `save_weights` requires h5py.问题解决

No more rolls!After joining ByteDance for a week, he ran decisively.

【C语言实现】两种计算平均成绩题型,博主精心整理,值得一读

scikit-learn no moudule named six

Based on php animation peripheral mall management system (php graduation design)

Small program -- subcontracting

Based on php online learning platform management system acquisition (php graduation design)
随机推荐
How to prevent governance attacks in DAOs?
render-props和高阶组件
基于php动漫周边商城管理系统(php毕业设计)
scikit-learn no moudule named six
自建 Prometheus 采集腾讯云容器服务监控数据最佳实践
ModuleNotFoundError: No module named 'yaml'
Based on php online learning platform management system acquisition (php graduation design)
使用分类权重解决数据不平衡的问题
基于php在线考试管理系统获取(php毕业设计)
【C语言】猜数字小游戏
Spark shuffle调优
groupByKey和reduceBykey的区别
SAP ABAP OData 服务如何支持删除(Delete)操作试读版
基于 OData 模型和 JSON 模型的 SAP UI5 表格控件行项目的添加和删除实现
365天挑战LeetCode1000题——Day 046 生成每种字符都是奇数个的字符串 + 两数相加 + 有效的括号
shell规范与变量
数据分析面试手册《指标篇》
第一讲 测试知多少
Based on php tourism website management system acquisition (php graduation design)
10 Practical Uses of NFTs (NFT System Development)