当前位置:网站首页>[Luogu] p1395 meeting
[Luogu] p1395 meeting
2022-07-26 23:19:00 【Record algorithm solution】
Title address :
https://www.luogu.com.cn/problem/P1395
Title Description :
There is a village where n n n A villager , Yes n − 1 n-1 n−1 This path makes this n n n It's a village home , The length of each path is 1 1 1. Now the village head wants to hold a meeting in a villager's home , The village head hopes that the sum of the distance between all villagers and the meeting place will be the smallest , Then the village head should set the meeting place in which villager's home , And what is the minimum sum of this distance ? If more than one node satisfies the condition , Then select the point with the smallest node number .
Input format :
first line , a number n n n, Express n n n A villager .
Next n − 1 n-1 n−1 That's ok , Two Numbers per line a a a and b b b, Represents the villagers a a a Their homes and villagers b b b There is a path between our homes .
Output format :
Output two numbers on one line x x x and y y y.
x x x It means that the village head will hold a meeting in which villager's home .
y y y Represents the minimum value of the sum of distances .
Data range :
about 70 % 70\% 70% data n ≤ 1 0 3 n \le 10^3 n≤103.
about 100 % 100\% 100% data n ≤ 5 × 1 0 4 n \le 5 \times 10^4 n≤5×104.
In fact, it is to find a point in the tree , Make the sum of the distance between the rest points to be minimum , If there are multiple and minimal points , Then take the one with the smallest number .
The answer is the center of gravity of the tree , Method reference https://blog.csdn.net/qq_46105170/article/details/113813152. Proof of nature reference https://blog.csdn.net/qq_46105170/article/details/125841504. Suppose we don't know the property , You can also solve .
You can do it twice DFS solve . set up d [ u ] d[u] d[u] For all points and u u u The sum of the distances . With 1 1 1 Point number is the root of the tree , First pass DFS Find out each sub tree v v v Number of nodes s [ v ] s[v] s[v], Distance from all points 1 1 1 The distance between the points d [ 1 ] d[1] d[1]. Second times DFS When , If u → v u\to v u→v, that d [ v ] = d [ u ] + n − 2 s [ u ] d[v]=d[u]+n-2s[u] d[v]=d[u]+n−2s[u], thus d [ v ] d[v] d[v] Can be d [ u ] d[u] d[u] Push it out . Because it is known that d [ 1 ] d[1] d[1], So that we can put d d d The whole array is pushed out , to glance at d d d Who is the smallest point . The code is as follows :
#include <iostream>
#include <cstring>
using namespace std;
const int N = 5e4 + 10, M = N << 1;
int n;
int h[N], e[M], ne[M], idx;
int sz[N], d[N];
int x, y;
void add(int a, int b) {
e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}
int dfs1(int u, int from, int dist) {
sz[u] = 1;
d[1] += dist;
for (int i = h[u]; ~i; i = ne[i]) {
int v = e[i];
if (v != from) sz[u] += dfs1(v, u, dist + 1);
}
return sz[u];
}
void dfs2(int u, int from) {
if (~from) {
d[u] = d[from] + n - 2 * sz[u];
if (d[u] < y) {
y = d[u];
x = u;
} else if (d[u] == y) x = min(x, u);
}
for (int i = h[u]; ~i; i = ne[i]) {
int v = e[i];
if (v != from) dfs2(v, u);
}
}
int main() {
memset(h, -1, sizeof h);
scanf("%d", &n);
for (int i = 1; i < n; i++) {
int a, b;
scanf("%d%d", &a, &b);
add(a, b), add(b, a);
}
dfs1(1, -1, 0);
// Suppose the answer is 1 Number point
x = 1, y = d[1];
dfs2(1, -1);
printf("%d %d\n", x, y);
}
Complexity of time and space O ( n ) O(n) O(n).
边栏推荐
- pgsql -&gt; flink cdc -&gt; flink -&gt; Mysql, if a PgSQL CDC
- Kalibr calibration realsensed435i -- multi camera calibration
- The memory occupation of the computer is too high after it is turned on (more than 50%)
- Product principles of non-financial decentralized application
- The interviewer asked: this point of JS
- kalibr标定realsenseD435i --多相机标定
- The JSON string is converted into a JSON object, the value of a key is obtained, and whether a key exists is judged
- [postgresql]postgresqlg使用enerate_series() 函数补全统计
- 蔚来杯2022牛客暑期多校训练营2
- Eureka basic use
猜你喜欢

Public cloud security and compliance considerations

Reinforcement learning weekly 55: lb-sgd, msp-drl & robust reinforcement learning against

公有云安全性和合规性方面的考虑事项

SQL Basics

Apifox -- a better API testing tool than postman
![[hcip] OSPF route calculation](/img/1c/ee9eee2e723b850c401f7cddda1b27.png)
[hcip] OSPF route calculation

Docker uses mysql:5.6 and owncloud image to build a personal network disk, install and build a private warehouse harbor

Kt6368a Bluetooth chip development precautions and problem collection - long term update

SQL 基础知识

json格式化小工具--pyqt5实例
随机推荐
New thrust of Moore's law, detailed explanation of Intel Advanced Packaging Technology!
Cheaper than seals, with a large space for shape explosion. Is there really no match for 200000 or so? Chang'an's new "King fried" is cost-effective
Will the approval in advance affect the formal approval?
Kalibr calibration realsensed435i -- multi camera calibration
正则表达式与绕过案例复现
Xinding acquires Ziguang holdings! Wanye enterprise: comprehensive transformation of integrated circuits!
沟通中经常用到的几个库存术语
Monte Carlo search tree (UCT) based on confidence upper bound to realize four sub chess
2019 biometric forum successfully ended: these ten highlights should not be missed!
Boss; Can flick CDC Oracle finish reading the full amount of data, just like directly fetching data from the database
HCIA-R&S自用笔记(19)VLAN配置及实验、VLAN间路由
Promote the replacement of X86 with arm server chips, and Huawei and Feiteng carry the banner of localization!
Kingbasees SQL language reference manual of Jincang database (3.1.1.3. currency type)
Import of MySQL data
Day07 MySQL knowledge points re summary and multi table query
华裔科学家Ashe教授对涉嫌造假的Nature论文的正面回应
Too busy with scientific research to take care of your family? Chen Ting: life cannot have only one fulcrum
Customer case | student education relies on observation cloud to create a new ecosystem of observable Smart Education
PostgreSQL 与 Navicat:数据库行业的中坚力量
Hcia-r & s self use notes (23) DHCP