当前位置:网站首页>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;
}
边栏推荐
- 工程建筑行业数据中台指标分析
- 第3讲:MySQL数据库中常见的几种表字段数据类型
- selenium无头,防检测
- 漫长的投资生涯
- [ASM] Bytecode Operation MethodWriter
- 论文解读(GSAT)《Interpretable and Generalizable Graph Learning via Stochastic Attention Mechanism》
- 企业公众号文章写作方向:如何写出读者认可的优质内容
- 365 days challenge LeetCode1000 questions - Day 046 Generate a string with odd number of each character + add two numbers + valid parentheses
- Spark shuffle tuning
- 恒星的正方形问题
猜你喜欢
一种灵活的智能合约协作方式
render-props and higher order components
高等代数_证明_矩阵的行列式为特征值之积, 矩阵的迹为特征值之和
Centos7--MySQL的安装
Still struggling with reporting tool selection?To take a look at this
Kubernetes第零篇:认识kubernetes
Uses of Anacoda
JS prototype hasOwnProperty in 加方法 原型终点 继承 重写父类方法
[Niu Ke brush questions-SQL big factory interview questions] NO4. Travel scene (a taxi)
小程序中的多表联合查询
随机推荐
模拟数据之mockjs
小程序毕设作品之微信美食菜谱小程序毕业设计成品(7)中期检查报告
FusionGAN:A generative adversarial network for infrared and visible image fusion article study notes
_ _ determinant of a matrix is higher algebra eigenvalue of the product, the characteristic value of matrix trace is combined
小程序毕设作品之微信体育馆预约小程序毕业设计成品(1)开发概要
ImportError: `save_weights` requires h5py.问题解决
小程序毕设作品之微信美食菜谱小程序毕业设计成品(5)任务书
小程序毕设作品之微信体育馆预约小程序毕业设计成品(3)后台功能
NgRx Selector 的 Memoization 特性学习笔记
SOM Network 2: Implementation of the Code
Still struggling with reporting tool selection?To take a look at this
2022-08-01 第八组 曹雨 泛型 枚举
Based on php online learning platform management system acquisition (php graduation design)
Homework 8.1 Orphans and Zombies
The Microsoft campus ambassador to shout you to autumn recruit!
漫长的投资生涯
越长大越孤单
VGUgarbage collector(垃圾回收器)的实现原理
0DFS Medium LeetCode6134. Find the closest node to the given two nodes
MySQL related knowledge