Codeforces 1179 D - Fedor Runs for President

D - Fedor Runs for President

思路:

推出斜率优化公式后,会发现最优点只可能来自凸斜率中的第一个元素和最后一个元素,

这两个元素不用维护凸斜率也能知道,就是第一个和上一个元素

代码:

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize(4)
#include<bits/stdc++.h>
using namespace std;
#define y1 y11
#define fi first
#define se second
#define pi acos(-1.0)
#define LL long long
//#define mp make_pair
#define pb emplace_back
#define ls rt<<1, l, m
#define rs rt<<1|1, m+1, r
#define ULL unsigned LL
#define pll pair<LL, LL>
#define pli pair<LL, int>
#define pii pair<int, int>
#define piii pair<pii, int>
#define pdd pair<double, double>
#define mem(a, b) memset(a, b, sizeof(a))
#define debug(x) cerr << #x << " = " << x << "\n";
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
//head 

const int N = 5e5 + 5;
vector<int> g[N];
int n, u, v, sz[N];
LL dp[N], ans;
//(dp[j]-dp[k]+sz[j]^2-sz[k]^2)/(sz[j]-sz[k]) >= 2*(n-sz[i]) k < j
bool _g(int k, int j, LL C) {
    return (dp[j]-dp[k]+sz[j]*1LL*sz[j]-sz[k]*1LL*sz[k]) <= C*(sz[j]-sz[k]);
}
//(dp[i]-dp[j]+sz[i]^2-sz[j]^2)/(sz[i]-sz[j]) >= (dp[j]-dp[k]+sz[j]^2-sz[k]^2)/(sz[j]-sz[k])
//k < j < i
bool gg(int k, int j, int i) {
    return (dp[i]-dp[j]+sz[i]*1LL*sz[i]-sz[j]*1LL*sz[j])*(sz[j]-sz[k]) <= (dp[j]-dp[k]+sz[j]*1LL*sz[j]-sz[k]*1LL*sz[k])*(sz[i]-sz[j]);
}
void dfs(int u, int o) {
    sz[u] = 1;
    for (int v : g[u]) {
        if(v != o) {
            dfs(v, u);
            ans = min(ans, (n-sz[v])*1LL*(n-sz[v]) + sz[v]*1LL*sz[v]);
            sz[u] += sz[v];
        }
    }
    dp[u] = sz[u]*1LL*sz[u];
    for (int v : g[u]) {
        if(v != o) {
            dp[u] = min(dp[u], (sz[u]-sz[v])*1LL*(sz[u]-sz[v])+dp[v]);
        }
    }
    sort(g[u].begin(), g[u].end(), [](int x, int y){
            return sz[x] > sz[y];
         });
    int l = -1, r = -1;
    for (int v : g[u]) {
        if(v == o) continue;
        if(l == -1) {
            l = r = v;
            continue;
        }
        int x = l;
        ans = min(ans, dp[v]+dp[x]+(n-sz[v]-sz[x])*1LL*(n-sz[v]-sz[x]));
        x = r;
        ans = min(ans, dp[v]+dp[x]+(n-sz[v]-sz[x])*1LL*(n-sz[v]-sz[x]));
        r = v;
    }
}
int main() {
    scanf("%d", &n);
    for (int i = 1; i < n; ++i) scanf("%d %d", &u, &v), g[u].pb(v), g[v].pb(u);
    int rt = 1;
    for (int i = 1; i <= n; ++i) if(g[i].size() != 1) rt = i;
    ans = n*1LL*n;
    dfs(rt, rt);
    printf("%lld\n", n*1LL*(n-1) - (ans-n)/2);
    return 0;
}

原文地址:https://www.cnblogs.com/widsom/p/11156690.html

时间: 2024-10-14 00:32:00

Codeforces 1179 D - Fedor Runs for President的相关文章

Codeforces Round #Pi (Div. 2) E. President and Roads (最短路+强连通求割边)

题目地址:codeforces #pi (DIV2) E 题目很水..就是先求两边最短路,然后把可能为最短路的边挑出来,然后判断是否yes只需要转化成无向图跑一遍tarjan,找出割边,割边就是yes,然后剩下的边就让它的值为最短路-1就行了,如果-1后变成了非正数,就是no. 但是!!!居然卡spfa!!那是不是说cf以后就不能用可以卡的算法了..完全可以出组数据来卡这些算法...比如spfa,isap... 于是为了这题,又看了一遍迪杰斯特拉算法.. 代码如下: #include <cstd

Codeforces Round #Pi (Div. 2) E. President and Roads tarjan+最短路

#include<time.h> #include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<vector> #include<stack> #include<queue> #include<set> #include<map> #in

Codeforces 467D Fedor and Essay(bfs)

题目链接:Codeforces 467D Fedor and Essay 题目大意:给定一个含n个单词的文本,然后给定m种变换,要求变换后r的个数尽量少,长度尽量短,不区分大小写. 解题思路:bfs,将每个单词处理成长度以及r的个数,然后从最优的开始更新即可,类似dp. #include <cstdio> #include <cstring> #include <map> #include <string> #include <vector> #

Codeforces Beta Round #6 (Div. 2 Only) B. President&#39;s Office

题目大意 给出一个n*m的矩阵 ,描述桌子的布局.总统的桌子和他的副手的桌子相邻,每一个人的桌子有它独有的颜色.问总统有多少个副手. 解题思路 搜出总统的桌子在矩阵中的边界后判断边界外的其它颜色桌子的数量. 题目代码 #include <set> #include <map> #include <queue> #include <math.h> #include <vector> #include <string> #include

CodeForces 567E President and Roads(最短路 + tarjan)

CodeForces 567E President and Roads Description Berland has n cities, the capital is located in city s, and the historic home town of the President is in city t (s?≠?t). The cities are connected by one-way roads, the travel time for each of the road

Codeforces Round #267 (Div. 2) B. Fedor and New Game

After you had helped George and Alex to move in the dorm, they went to help their friend Fedor play a new computer game ?Call of Soldiers 3?. The game has (m?+?1) players and n types of soldiers in total. Players ?Call of Soldiers 3? are numbered for

【CodeForces 567E】President and Roads(最短路)

Description Berland has n cities, the capital is located in city s, and the historic home town of the President is in city t (s ≠ t). The cities are connected by one-way roads, the travel time for each of the road is a positive integer. Once a year t

CodeForces 754D Fedor and coupons ——(k段线段最大交集)

还记得lyf说过k=2的方法,但是推广到k是其他的话有点麻烦.现在这里采取另外一种方法. 先将所有线段按照L进行排序,然后优先队列保存R的值,然后每次用最小的R值,和当前的L来维护答案即可.同时,如果Q的size()比k大,那么就弹出最小的R. 具体见代码: 1 #include <stdio.h> 2 #include <algorithm> 3 #include <string.h> 4 #include <set> 5 #include <vec

codeforces Fedor and New Game

#include<iostream> #include<stack> #include<cstring> #include<cstdio> #include<queue> #include<vector> #include<algorithm> #define INF 0x3f3f3f3f using namespace std; int a[1005]; int main(){ int n, m, k; int ans,