当前位置:网站首页>Prufer sequence
Prufer sequence
2022-08-01 22:07:00 【ThXe】
Prufer性质
Prufer数列是无根树的一种数列.在组合数学中,Prufer数列由有一个对于顶点标过号的树转化来的数列,点数为n的树转化来的Prufer数列长度为n-2
1.若编号i在Prufersequence appearscnt次,Then it is in degreescnt+1
2.nThe shape of a point unlabeled unrooted tree hasnn-2种
3.nEach point has a labelled unrooted tree shape:
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];//fatherThe array is in the titlef数组
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]]++;//Increase the degree of the parent node by one
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++;//When the parent node number is smaller,Continue to add the parent nodePrufer序列
}
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]]++;//Increase the degree of the parent node by one
Prufer[N - 1] = N;//这里需要特别注意一下
for (int i = 1, now = 1; i <= N - 1; i++, now++) {
while (du[now]) now++;//找到编号最小的叶节点
father[now] = Prufer[i];//Join the parent array
while (i < N - 1 && !--du[Prufer[i]] && Prufer[i] < now) father[Prufer[i]] = Prufer[i + 1], i++;//When the parent node number is smaller,Continue adding the parent node to the parent array
}
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;
}
边栏推荐
- MySQL related knowledge
- Safe fifth after-school exercise
- 微软校园大使喊你来秋招啦!
- shell programming conventions and variables
- 你居然不懂Bitmap和Drawable? 相关知识大扫盲
- 03、GO语言变量定义、函数
- Based on php hotel online reservation management system acquisition (php graduation project)
- 9. SAP ABAP OData 服务如何支持删除(Delete)操作
- No more rolls!After joining ByteDance for a week, he ran decisively.
- 求解多元多次方程解的个数
猜你喜欢

HCIP---Multiple Spanning Tree Protocol related knowledge points

xctf攻防世界 Web高手进阶区 web2

Kubernetes第零篇:认识kubernetes

使用Jenkins做持续集成,这个知识点必须要掌握

Based on php online music website management system acquisition (php graduation design)

KMP 字符串匹配问题

Centos7--MySQL的安装

【C语言实现】最大公约数的3种求法

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

小程序中的多表联合查询
随机推荐
Spark practice questions + answers
9. SAP ABAP OData 服务如何支持删除(Delete)操作
迁移学习——Discriminative Transfer Subspace Learning via Low-Rank and Sparse Representation
Based on php animation peripheral mall management system (php graduation design)
网络水军第一课:手写自动弹幕
SAP Spartacus Accessibility E2E 端到端测试
今日睡眠质量记录74分
小程序容器+自定义插件,可实现混合App快速开发
1. @Component注解的原理剖析
SOM Network 2: Implementation of the Code
罗克韦尔AB PLC RSLogix5000中的比较指令使用方法介绍
教你VSCode如何快速对齐代码、格式化代码
JS prototype hasOwnProperty in 加方法 原型终点 继承 重写父类方法
恒星的正方形问题
【ASM】字节码操作 MethodWriter
feel so stupid
安全第五次课后练习
Dichotomy Medium LeetCode6133. Maximum Number of Groups
第3讲:MySQL数据库中常见的几种表字段数据类型
论文解读(GSAT)《Interpretable and Generalizable Graph Learning via Stochastic Attention Mechanism》