当前位置:网站首页>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;
}
边栏推荐
猜你喜欢
【牛客刷题-SQL大厂面试真题】NO4.出行场景(某滴打车)
ModuleNotFoundError: No module named ‘yaml‘
小程序毕设作品之微信美食菜谱小程序毕业设计成品(6)开题答辩PPT
2022 版 MySQL 巅峰教程,收藏好,慢慢看
入门数据库Days4
seaborn笔记:可视化统计关系(散点图、折线图)
19 Lectures on Disassembly of Multi-merchant Mall System Functions - Invoice Management on the Platform
Prufer序列
Small program -- subcontracting
number of solutions to solve a multivariate multi-degree equation
随机推荐
ImportError: `save_weights` requires h5py.问题解决
AI应用第一课:支付宝刷脸登录
一种灵活的智能合约协作方式
_ _ determinant of a matrix is higher algebra eigenvalue of the product, the characteristic value of matrix trace is combined
Prufer序列
如何理解 new (...args: any[]) => any
SAP Spartacus Accessibility E2E 端到端测试
小程序毕设作品之微信体育馆预约小程序毕业设计成品(1)开发概要
工程建筑行业数据中台指标分析
Centos7--MySQL的安装
模拟数据之mockjs
ModuleNotFoundError: No module named ‘yaml‘
小程序毕设作品之微信体育馆预约小程序毕业设计成品(2)小程序功能
AQS
LeetCode952三部曲之一:解题思路和初级解法(137ms,超39%)
scikit-learn no moudule named six
深度学习Course2第一周Practical aspects of Deep Learning习题整理
ARFoundation Getting Started Tutorial U2-AR Scene Screenshot Screenshot
365天挑战LeetCode1000题——Day 046 生成每种字符都是奇数个的字符串 + 两数相加 + 有效的括号
Based on php hotel online reservation management system acquisition (php graduation project)