当前位置:网站首页>F. Weights assignment for tree edges problem solving Report
F. Weights assignment for tree edges problem solving Report
2022-07-05 15:25:00 【wch(】
F. Weights Assignment For Tree Edges Problem solving report
label : graph theory , Undirected graph
The question :
The problem is quite long , It took me half an hour to understand , The most important thing is patience .
The title first gives a sequence b Count into parent node , Such as b[1]=2, That node 2 Is the node 1 The father node of ,b[2]=2, node 2 yes 2 The father node of , That is to say, nodes 2 It's the root node of the tree .
And then give a p Nodes sort each node ,p[1]=3 That is, node 3 Number one .
use dist The array counts the distance from each point to the root node , Then let's assign a value to the edge weight , Guarantee dist[pi]<dist[pi+1], That is, the lower the ranking, the farther the distance from the point to the root node .
Their thinking
After understanding the meaning of the topic, you can find that the topic is not so difficult .
For a given arrangement p, If there is a node whose parent node ranks lower than that node , dist[ Parent node ]>dist[ The node ], Obviously illegal .
We use it ans Array to include edge weights . To meet the conditions of the topic dist[pi]<dist[pi+1], It can be set by yourself
dist[pi]+1=dist[pi+1];
Then the key code can be obtained to calculate the edge weight .
ans[p[i]] = dist[p[i - 1]] - dist[b[p[i]]] + 1;
dist[p[i]] = dist[b[p[i]]] + ans[p[i]];
Code implementation
#include <iostream>
using namespace std;
const int N = 2 * 1e5 + 50;
int b[N], p[N], dist[N], ans[N];
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
int fa = 0;
for (int i = 1; i <= n; i++) {
cin >> b[i];
if (b[i] == i) fa = i;
}
for (int i = 1; i <= n; i++)
cin >> p[i];
for (int i = 1; i <= n; i++)
ans[i] = dist[i] = -1;// Mark the array as -1, Indicates that it has not been marked
int f = 1;
if (p[1] != fa) f = 0;
else
{
ans[p[1]] = 0, dist[p[1]] = 0;
for (int i=2;i<=n;i++)
{
if (dist[b[p[i]]]==-1) { f = 0; break; }
// If the parent node has not been assigned , It indicates that the parent node is behind this node , Illegal array
ans[p[i]] = dist[p[i - 1]] - dist[b[p[i]]] + 1;
dist[p[i]] = dist[b[p[i]]] + ans[p[i]];
}
}
if (f == 0) cout << -1 << endl;
else {
for (int i=1;i<=n;i++) cout << ans[i] << " ";
cout << endl;
}
}
return 0;
}
Ah ha ha ha , My task is finished .
边栏推荐
- JS topic - console log()
- 做研究无人咨询、与学生不交心,UNC助理教授两年教职挣扎史
- 百亿按摩仪蓝海,难出巨头
- Behind the ultra clear image quality of NBA Live Broadcast: an in-depth interpretation of Alibaba cloud video cloud "narrowband HD 2.0" technology
- Cartoon: programmers don't repair computers!
- 数据库学习——数据库安全性
- Talking about how dataset and dataloader call when loading data__ getitem__ () function
- Au - delà du PARM! La maîtrise de l'Université de Pékin propose diverse pour actualiser complètement le classement du raisonnement du NLP
- Optional parameters in the for loop
- mapper.xml文件中的注释
猜你喜欢

Garbage collection mechanism of PHP (theoretical questions of PHP interview)

当代人的水焦虑:好水究竟在哪里?

Ctfshow web entry information collection

MySQL 巨坑:update 更新慎用影响行数做判断!!!

Photoshop plug-in action related concepts actionlist actiondescriptor actionlist action execution load call delete PS plug-in development
![[JVM] operation instruction](/img/f5/85580495474ef58eafbb421338e93f.png)
[JVM] operation instruction

First PR notes

Your childhood happiness was contracted by it

Fr exercise topic - simple question

Ecotone technology has passed ISO27001 and iso21434 safety management system certification
随机推荐
MySQL之CRUD
qt creater断点调试程序详解
Select sort and bubble sort
Garbage collection mechanism of PHP (theoretical questions of PHP interview)
Coding devsecops helps financial enterprises run out of digital acceleration
Redis distributed lock principle and its implementation with PHP (1)
Common redis data types and application scenarios
MySQL 巨坑:update 更新慎用影响行数做判断!!!
亿咖通科技通过ISO27001与ISO21434安全管理体系认证
Your childhood happiness was contracted by it
Fr exercise topic --- comprehensive question
mapper. Comments in XML files
Reasons and solutions for redis cache penetration and cache avalanche
1330:【例8.3】最少步数
Calculate weight and comprehensive score by R entropy weight method
我这边同时采集多个oracle表,采集一会以后,会报oracle的oga内存超出,大家有没有遇到的?
Does maxcompute have SQL that can query the current storage capacity (KB) of the table?
Super wow fast row, you are worth learning!
超越PaLM!北大硕士提出DiVeRSe,全面刷新NLP推理排行榜
MySQL之CRUD