根的转移变化-洛谷P3478 STA-Station & CF219D Choosing Capital for Treeland
洛谷P3478 STA-Station 题目连接:https://www.luogu.com.cn/problem/P3478
CF219D Choosing Capital for Treeland 题目连接:https://codeforces.com/contest/219/problem/D
CSDN食用连接:https://blog.csdn.net/qq_43906000/article/details/109210673
STA-Station
题目描述
给定一个 n 个点的树,请求出一个结点,使得以这个结点为根时,所有结点的深度之和最大。
一个结点的深度之定义为该节点到根的简单路径上边的数量。
输入格式
第一行有一个整数,表示树的结点个数 n。
接下来 \((n - 1)\) 行,每行两个整数 \(u, v\),表示存在一条连接 \(u, v\) 的边。
输出格式
本题存在 Special Judge。
输出一行一个整数表示你选择的结点编号。如果有多个结点符合要求,输出任意一个即可。
输入输出
输入
8
1 4
5 6
4 5
6 7
6 8
2 4
3 4
输出
7
说明/提示
样例 1 解释
输出 7 和 8 都是正确答案。
数据规模与约定
对于全部的测试点,保证 \(1 \leq n \leq 10^6\),\(1 \leq u, v \leq n\),给出的是一棵树。
看起来有点乱,但实际上我们只需要算一个根节点的答案就好了,其他的都可以通过规律直接推出来,我们可以手动找找规律,当根从一个节点a转移到另一个节点b的时候它的所有节点深度之和会发生什么变化? 我们很容易知道,所有b的儿子包括它自己的距离都会减少1,而所有不是b这个子树的节点的深度都会增加1,也就是:
\(sum[v]=sum[u]+(n-son[v]-1)-son[v]-1\)
于是答案也就出来了。
以下是AC代码:
#include <bits/stdc++.h>
using namespace std;
const int mac=1e6+10;
typedef long long ll;
struct Edge{
int to,next;
}eg[mac<<1];
int head[mac],num=0;
int son[mac],deep[mac];
ll sum[mac];
ll tot=0;
int id=1,n;
void add(int u,int v)
{
eg[num++]=Edge{v,head[u]};
head[u]=num-1;
}
void dfs(int fa,int u)
{
for (int i=head[u]; i!=-1; i=eg[i].next){
int v=eg[i].to;
if (v==fa) continue;
deep[v]=deep[u]+1;
tot+=deep[v];
dfs(u,v);
son[u]+=son[v]+1;
}
}
void move(int fa,int u)
{
for (int i=head[u]; i!=-1; i=eg[i].next){
int v=eg[i].to;
if (v==fa) continue;
sum[v]=sum[u]+(n-son[v]-1)-son[v]-1;
move(u,v);
}
}
int main(int argc, char const *argv[])
{
scanf ("%d",&n);
memset(head,-1,sizeof head);
for (int i=1; i<n; i++){
int u,v;
scanf ("%d%d",&u,&v);
add(u,v); add(v,u);
}
dfs(-1,1);
sum[1]=tot;
move(-1,1);
ll dis=0;
for (int i=1; i<=n; i++) {
if (sum[i]>dis) {dis=sum[i]; id=i;}
}
printf ("%d\n",id);
return 0;
}
Choosing Capital for Treeland
The country Treeland consists of n cities, some pairs of them are connected with unidirectional roads. Overall there are n - 1 roads in the country. We know that if we don't take the direction of the roads into consideration, we can get from any city to any other one.
The council of the elders has recently decided to choose the capital of Treeland. Of course it should be a city of this country. The council is supposed to meet in the capital and regularly move from the capital to other cities (at this stage nobody is thinking about getting back to the capital from these cities). For that reason if city a is chosen a capital, then all roads must be oriented so that if we move along them, we can get from city a to any other city. For that some roads may have to be inversed.
Help the elders to choose the capital so that they have to inverse the minimum number of roads in the country.
Input
The first input line contains integer \(n (2 ≤ n ≤ 2\times10^5)\) — the number of cities in Treeland. Next n - 1 lines contain the descriptions of the roads, one road per line. A road is described by a pair of integers \(s_i, t_i (1 ≤ s_i, t_i ≤ n; s_i ≠ t_i)\) — the numbers of cities, connected by that road. The i-th road is oriented from city si to city ti. You can consider cities in Treeland indexed from 1 to n.
Output
In the first line print the minimum number of roads to be inversed if the capital is chosen optimally. In the second line print all possible ways to choose the capital — a sequence of indexes of cities in the increasing order.
Examples
input
3
2 1
2 3
output
0
2
input
4
1 4
2 4
3 4
output
2
1 2 3
题目大意:有n个城市,这n个城市连成了一颗树,有n-1条道路连接了所有城市。每条道路只能单向通行。现在政府需要决定选择哪个城市为首都。假如城市i成为了首都,那么为了使首都能到达任意一个城市,不得不将一些道路翻转方向,记翻转道路的条数为k。你的任务是找到所有满足k最小的首都。
实际上这题也是只需要计算一个节点作为根节点时候的答案就行了,其他的情况我们同样可以推出来,加上现在节点u需要翻转的边数为x,那么如果转移到u的儿子v,我们可以知道,答案为x+1,因为我们需要从儿子节点v到父节点u,这一步是逆行的,如果转移的不是儿子节点,那么也就是说他转移到了它的父亲节点(这里的父亲有多个),那么我们可以知道的是,答案为x-1,因为从父节点到儿子节点是顺的。
那么则有转移公式:\(if (son[u]==v) nb[v]=nb[u]-1;else\ nb[v]=nb[u]+1;\)
那么答案也就出来了。
以下是AC代码:
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mke make_pair
typedef pair<int,int> pill;
const int inf=1e9+10;
const int mac=2e5+10;
vector<int>con[mac];
map<pill,int>mp;
int nb[mac],ans=inf;
void dfs(int fa,int u)
{
for (auto v:con[u]){
if (v==fa) continue;
pill s={0,0};
s.fi=u; s.se=v;
if (!mp[s]) nb[1]++;
dfs(u,v);
}
}
void dfs2(int fa,int u)
{
for (auto v:con[u]){
if (v==fa) continue;
pill s={0,0};
s.fi=u; s.se=v;
if (!mp[s]) nb[v]=nb[u]-1;
else nb[v]=nb[u]+1;
ans=min(ans,nb[v]);
dfs2(u,v);
}
}
int main(int argc, char const *argv[])
{
int n;
scanf ("%d",&n);
for (int i=1; i<n; i++){
int u,v;
scanf ("%d%d",&u,&v);
mp[mke(u,v)]=1;
con[u].push_back(v); con[v].push_back(u);
}
dfs(-1,1);
ans=nb[1];
dfs2(-1,1);
printf("%d\n",ans);
for (int i=1; i<=n; i++)
if (nb[i]==ans) printf("%d ",i);
return 0;
}
原文地址:https://www.cnblogs.com/lonely-wind-/p/13855144.html
- 关小刷刷题08 – Leetcode 26. Remove Duplicates from Sorted Array 方法2、3
- C++课程设计类作业2
- C++课程设计类作业4
- [接口测试 - 基础篇] 01 你应该了解的协议基础
- 代码分享系列(1)之感知机【代码可下载】
- C++课程设计类作业3
- [快学Python3]INI文件读写
- 【专知-PyTorch手把手深度学习教程08】NLP-PyTorch: 用字符级RNN生成名字
- Codeforces Round #415 (Div. 2)(A,暴力,B,贪心,排序)
- BZOJ 2456: mode(新生必做的水题)
- 【专知-PyTorch手把手深度学习教程07】NLP-基于字符级RNN的姓名分类
- Codeforces Round #301 (Div. 2)(A,【模拟】B,【贪心构造】C,【DFS】)
- 【专知-PyTorch手把手深度学习教程06】NLP-Word Embedding快速理解与PyTorch实现: 图文+代码
- [linux][memory]memcmp几种实现和性能对比
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 疯子的算法总结(九) 图论中的矩阵应用 Part 1 POJ3613 Cow Relays
- 『数据库』你这些知识点都不会,你学个锤子SQL数据库!
- The Preliminary Contest for ICPC Asia Xuzhou 2019 徐州网络赛 XKC's basketball team
- The Preliminary Contest for ICPC Asia Xuzhou 2019 徐州网络赛 D Carneginon
- 地表最强的MySQL安装一键式安装,信不信你下完我就给你装好!附各种Mysql安装失败的解决办法(什么你安装失败了?快来看这个)
- The Preliminary Contest for ICPC Asia Xuzhou 2019 徐州网络赛 C Buy Watermelon
- The Preliminary Contest for ICPC Asia Xuzhou 2019 徐州网络赛 B so easy
- 萌新不看会后悔的C++基本类型总结(二)
- The Preliminary Contest for ICPC Asia Xuzhou 2019 徐州网络赛 A Who is better?
- 树的重心
- PostgreSQL 13:索引并行vacuum
- 『数据库』震惊,某博主为吸引眼球拿出压箱底SQL总结,如果你没看那就吃亏了!(超级详细的SQL基础,你还不会的话就别学数据库了)
- 萌新不看会后悔的C++基本类型总结(一)
- 2019 ICPC 银川网络赛 D. Take Your Seat (疯子坐飞机问题)
- 2019 ICPC 银川网络赛 H. Fight Against Monsters