当前位置:网站首页>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;
}
边栏推荐
- 入门数据库Days4
- Based on php hotel online reservation management system acquisition (php graduation project)
- 毕业十年,财富自由:那些比拼命努力更重要的事,从来没人会教你
- Still struggling with reporting tool selection?To take a look at this
- AI应用第一课:支付宝刷脸登录
- more grown, more lonely
- 高等代数_证明_矩阵的任意特征值的代数重数大于等于其几何重数
- Spark cluster construction
- 小程序中的多表联合查询
- PAM 回文自动机
猜你喜欢

感觉自己好傻

Uses of Anacoda

Advanced Algebra_Proof_The algebraic multiplicity of any eigenvalue of a matrix is greater than or equal to its geometric multiplicity

程序员必备的 “ 摸鱼神器 ” 来了 !

HCIP---Architecture of Enterprise Network

shell编程规范与变量

Small program -- subcontracting

小程序毕设作品之微信美食菜谱小程序毕业设计成品(6)开题答辩PPT

AIDL communication

入门数据库Days4
随机推荐
scikit-learn no moudule named six
Lecture 3: Several common table field data types in MySQL database
AQS
selenium无头,防检测
Raspberry Pi information display small screen, display time, IP address, CPU information, memory information (C language), four-wire i2c communication, 0.96-inch oled screen
APP专项测试:流量测试
2022 edition of MySQL tutorial, top collection good, take your time
User Experience | How to Measure User Experience?
熟悉的朋友
scikit-learn no moudule named six
如何理解 new (...args: any[]) => any
工程建筑行业数据中台指标分析
深度学习Course2第一周Practical aspects of Deep Learning习题整理
1. @Component注解的原理剖析
深度学习Course2第二周Optimization Algorithms习题整理
[Mobile Web] Mobile terminal adaptation
xctf攻防世界 Web高手进阶区 web2
Spark cluster construction
RxJs SwitchMapTo 操作符之移花接木
用户体验 | 如何度量用户体验?