首页 > 其他分享 >洛谷-P3478 STA-Station

洛谷-P3478 STA-Station

时间:2022-11-09 21:12:10浏览次数:69  
标签:include 洛谷 int siz Station nex gra now P3478

STA-Station

换根dp 模板

去到相邻的点可以根据去到的点的子树有多少个结点,来调整当前的值

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#include <array>
using namespace std;
typedef long long ll;
vector<vector<int>>gra;
vector<int>siz;
int n;

ll dfs(int now, int pre)
{
    siz[now] = 1;
    int ans = 1;
    for(int nex : gra[now])
    {
        if(nex == pre) continue;
        ans += dfs(nex, now);
        siz[now] += siz[nex];
    }
    return ans;
}

int bfs(int s, ll sum)
{
    queue<array<ll, 2>>q;
    vector<int>vis(n + 1), tot(n + 1, 0);
    q.push({s, sum});
    while(q.size())
    {
        auto [now, val] = q.front();
        q.pop();
        if(vis[now]) continue;
        vis[now] = 1;
        tot[now] = val;
        if(val > tot[s]) s = now;
        for(int nex : gra[now])
        {
            if(vis[nex]) continue;
            q.push({nex, val - siz[nex] - siz[nex] + n});
        }
    }
    return s;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    gra.resize(n + 1);
    siz.resize(n + 1);
    for(int i=1; i<n; i++)
    {
        int a, b;
        cin >> a >> b;
        gra[a].push_back(b);
        gra[b].push_back(a);
    }
    ll sum = dfs(1, 1);
    int rt = bfs(1, sum);
    cout << rt << endl;
    return 0;
}

标签:include,洛谷,int,siz,Station,nex,gra,now,P3478
From: https://www.cnblogs.com/dgsvygd/p/16875178.html

相关文章

  • 洛谷 [AGC021B] Holes 蓝 题解
    前言学校基础模拟赛的题,当时有思路但是不太会写凸包就没做,下来看了看,自己的思路大部分是正确的,有些细节没有想到,在此写篇题解。我用的是Andrew求凸包。思路答案为0......
  • 洛谷P1434滑雪分析
    [SHOI2002]滑雪题目描述Michael喜欢滑雪。这并不奇怪,因为滑雪的确很刺激。可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来......
  • 洛谷P1605题解分析
    迷宫题目描述给定一个\(N\timesM\)方格的迷宫,迷宫里有\(T\)处障碍,障碍处不可通过。在迷宫中移动有上下左右四种方式,每次只能移动一个方格。数据保证起点上没有障......
  • 洛谷 P1195.口袋的天空
    题目链接:https://www.luogu.com.cn/problem/P1195今天上算法设计课,复习一下Kruskal和并查集。 放AC代码1#include<bits/stdc++.h>2usingnamespacestd;3......
  • 洛谷题单【入门1】顺序结构-B2002 Hello,World!
    题目描述编写一个能够输出 Hello,World! 的程序,这个程序常常作为一个初学者接触一门新的编程语言所写的第一个程序,也经常用来测试开发、编译环境是否能够正常工作。......
  • 洛谷-1198
    洛谷-1198思路这个!辅助解释Code#include<bits/stdc++.h>usingnamespacestd;#define_u_u_ios::sync_with_stdio(false),cin.tie(nullptr)#definecfint_o_o......
  • 洛谷-3295
    洛谷-3295题意此题为中文题面。思路这里辅助解释Code#include<bits/stdc++.h>usingnamespacestd;#define_u_u_ios::sync_with_stdio(false),cin.tie(nullp......
  • 洛谷-P2491 消防
    消防树上直径+尺取考虑答案的路径一定是在树上直径,因为离树上任意一个点最远的点一定是直径的两个点其中之一因此先\(dfs\)一下找出直径两个端点从其中一个端点出......
  • 洛谷--【P1618】三连击升级版题解 排列枚举+循环枚举+stl
    题目描述将 1,2,…,91,2,…,9 共 99 个数分成三组,分别组成三个三位数,且使这三个三位数的比例是 A:B:C,试求出所有满足条件的三个三位数,若无解,输出 No!!!。输入格式......
  • 洛谷 P3287
    不难发现一定是拔高一段后缀。所以设\(f_{i,j}\)表示考虑前\(i\)个位置,拔高\(j\)次,第\(i\)个位置强制选的LIS的长度。则有\(f_{i,j}=\max\limits_{1\lex\lt......