首页 > 其他分享 >Luogu 3478 [POI2008]STA-Station

Luogu 3478 [POI2008]STA-Station

时间:2022-10-25 17:04:01浏览次数:62  
标签:fr int Luogu ca 3478 head edge Station include


题目链接:​​传送门​

题目描述

给出一个N个点的树,找出一个点来,以这个点为根的树时,所有点的深度之和最大

输入样例

8
1 4
5 6
4 5
6 7
6 8
2 4
3 4

输出样例

7

一句话题意
好吓人啊
其实还是很简单的
表示以为根能获得深度和
那么如果我们选了的任意一个儿子节点
那么以为根节点的所有点的深度都会减一
也就是
所有不是的子树的点深度都加一
那就是
要用f[i]加上它们两个
就是
递推式出来了
这题做完了
要记得预处理的情况
就是以为根的时候的深度和
这题需要

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <complex>
#include <algorithm>
#include <climits>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define
#define

using namespace std;
typedef long long ll;
struct node{
int next, to;
}edge[A];
int head[A], num_edge;
void add_edge(int from, int to) {
edge[++num_edge].next = head[from];
edge[num_edge].to = to;
head[from] = num_edge;
}
int n, m, a, b, rank;
ll f[A], ans, dep[A], siz[A];
void prepare(int fr, int fa) {
siz[fr] = 1; dep[fr] = dep[fa] + 1;
for (int i = head[fr]; i; i = edge[i].next) {
int ca = edge[i].to;
if (ca == fa) continue;
prepare(ca, fr);
siz[fr] += siz[ca];
}
}
void dfs(int fr, int fa) {
for (int i = head[fr]; i; i = edge[i].next) {
int ca = edge[i].to;
if (ca == fa) continue;
f[ca] = f[fr] + n - siz[ca] * 2;
dfs(ca, fr);
}
}

int main(int argc, char const *argv[])
{
cin >> n;
for (int i = 1; i < n; i++) {
cin >> a >> b;
add_edge(a, b);
add_edge(b, a);
}
prepare(1, 0);
for (int i = 1; i <= n; i++) f[1] += dep[i];
dfs(1, 0);
for (int i = 1; i <= n; i++)
if (f[i] > ans)
ans = f[i], rank = i;
cout << rank;
return 0;
}


标签:fr,int,Luogu,ca,3478,head,edge,Station,include
From: https://blog.51cto.com/lyle/5794951

相关文章

  • Luogu 1507 NASA的食物计划
    题目链接:​​传送门​​题目背景NASA(美国航空航天局)因为航天飞机的隔热瓦等其他安全技术问题一直大伤脑筋,因此在各方压力下终止了航天飞机的历史,但是此类事情会不会在以......
  • Luogu 1853 投资的最大效益
    题目链接:​​传送门​​题目背景约翰先生获得了一大笔遗产,他暂时还用不上这一笔钱,他决定进行投资以获得更大的效益。银行工作人员向他提供了多种债券,每一种债券都能在固定的......
  • Luogu 1833 樱花
    题目链接:​​传送门​​题目背景《爱与愁的故事第四弹·plant》第一章。题目描述爱与愁大神后院里种了n棵樱花树,每棵都有美学值Ci。爱与愁大神在每天上学前都会来赏花。爱与......
  • Luogu 2014 选课
    题目链接:​​传送门​​题目描述在大学里每个学生,为了达到一定的学分,必须从很多课程里选择一些课程来学习,在课程里有些课程必须在某些课程之前学习,如高等数学总是在其它课程......
  • Luogu P4171 [JSOI2010]满汉全席
    题目链接:​​传送门​​2-sat板子题注意输入的时候可不要以为w和h后面数字只有一位*/#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#includ......
  • Luogu P4915 帕秋莉的魔导书
    题目链接:​​传送门​​动态开点是真的麻烦跟普通线段树差别还是挺大的题意就是区间前缀和的和除以区间长度#include<iostream>#include<cstdio>#include<cstring>#inc......
  • Luogu P4868 Preprefix sum
    题目链接:​​传送门​​线段树维护前缀和简单明了修改就修改当然还有更快的树状数组差分的做法*/#include<iostream>#include<cstdio>#include<cstring>#include<cs......
  • Luogu P4514 上帝造题的七分钟
    题目链接:​​传送门​​二维树状数组区间加区间求和烦人的输入#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<complex>#include<......
  • Luogu P2455 [SDOI2006]线性方程组
    题目链接:​​传送门​​高斯消元可以去下面看一下​​​https://www.bilibili.com/video/av4688674​​​听视频比瞅博客有用得多这题算比较标准的板子了各种情况都有......
  • Luogu P3833 [SHOI2012]魔法树
    题目链接:​​传送门​​树剖板子区间加,子树查询树剖里查询的时候x和y地方小于号写反T了一会a,b写成dfn[a],dfn[b]竟然还有50分又WA了一会也就交了二十遍。#include<io......