当前位置:网站首页>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;
}
边栏推荐
猜你喜欢
blender3.2.1 unit setting
毕业十年,财富自由:那些比拼命努力更重要的事,从来没人会教你
FusionGAN:A generative adversarial network for infrared and visible image fusion article study notes
小程序中的多表联合查询
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
Centos7--MySQL的安装
感觉自己好傻
SAP ABAP OData 服务如何支持删除(Delete)操作试读版
Port protocol for WEB penetration
Getting Started Database Days4
随机推荐
SOM网络2: 代码的实现
Shell programming conditional statement
leetcode 204. Count Primes 计数质数 (Easy)
安全第五次课后练习
Upload markdown documents to blog garden
基于php湘西旅游网站管理系统获取(php毕业设计)
MySQL related knowledge
【建议收藏】ヾ(^▽^*)))全网最全输入输出格式符整理
罗克韦尔AB PLC RSLogix5000中的比较指令使用方法介绍
Spark shuffle调优
FusionGAN:A generative adversarial network for infrared and visible image fusion article study notes
还在纠结报表工具的选型么?来看看这个
The must-have "fishing artifact" for programmers is here!
No more rolls!After joining ByteDance for a week, he ran decisively.
教你VSCode如何快速对齐代码、格式化代码
使用分类权重解决数据不平衡的问题
SOM Network 1: Principles Explained
Pagoda application experience
你居然不懂Bitmap和Drawable? 相关知识大扫盲
如何防范 DAO 中的治理攻击?