Codeforces 1179 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 + ;
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] = ;
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 = -, r = -;
for (int v : g[u]) {
if(v == o) continue;
if(l == -) {
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 = ; i < n; ++i) scanf("%d %d", &u, &v), g[u].pb(v), g[v].pb(u);
int rt = ;
for (int i = ; i <= n; ++i) if(g[i].size() != ) rt = i;
ans = n*1LL*n;
dfs(rt, rt);
printf("%lld\n", n*1LL*(n-) - (ans-n)/);
return ;
}
Codeforces 1179 D - Fedor Runs for President的更多相关文章
- CF1179D Fedor Runs for President [DP,斜率优化]
Codeforces 思路 考虑把连的那两个点中间的链提出来,那么就会变成一条链,链上的每个点挂着一棵子树的形式. 设那些子树的大小为\(S_1,S2,\cdots\),那么新加的简单路径个数就是 \ ...
- Codeforces Round #Pi (Div. 2) E. President and Roads tarjan+最短路
E. President and RoadsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/567 ...
- 【codeforces 754D】Fedor and coupons
time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- Codeforces Round #Pi (Div. 2) E. President and Roads 最短路+桥
题目链接: http://codeforces.com/contest/567/problem/E 题意: 给你一个带重边的图,求三类边: 在最短路构成的DAG图中,哪些边是必须经过的: 其他的(包括 ...
- 【Codeforces 467D】Fedor and Essay
Codeforces 467 D 题意:给\(m\)个单词,以及\(n\)个置换关系,问将\(m\)个单词替换多次后其中所含的最少的\(R\)的数量以及满足这个数量的最短总长度 思路:首先将置 ...
- CodeForces - 896D :Nephren Runs a Cinema(卡特兰数&组合数学---比较综合的一道题)
Lakhesh loves to make movies, so Nephren helps her run a cinema. We may call it No. 68 Cinema. Howev ...
- Codeforces Round #Pi (Div. 2) 567E President and Roads ( dfs and similar, graphs, hashing, shortest paths )
图给得很良心,一个s到t的有向图,权值至少为1,求出最短路,如果是一定经过的边,输出"YES",如果可以通过修改权值,保证一定经过这条边,输出"CAN",并且输 ...
- Codeforces Round #569 题解
Codeforces Round #569 题解 CF1179A Valeriy and Deque 有一个双端队列,每次取队首两个值,将较小值移动到队尾,较大值位置不变.多组询问求第\(m\)次操作 ...
- Codeforces Beta Round #6 (Div. 2 Only) B. President's Office 水题
B. President's Office 题目连接: http://codeforces.com/contest/6/problem/B Description President of Berla ...
随机推荐
- Docker Machine(十五)
目录 一.Docker Machine 总览 1.Docker Engine VS Docker Machine 2.环境准备 二.安装 Docker Machine 1.Install Machin ...
- 写一个java常用的加密工具类
1.叙述 java security包下有很多加密算法类,我们可以很简单的调用它们.他们虽然功能很全,但是使用起来步骤有些繁琐.我在这里封装来一些常用的加密算法及他们常用的一些方法,来简化代码. 工具 ...
- 搭建sqli靶场
前言: sqli是一个印度程序员编写的,用来学习sql注入的一个游戏教程 sqli这个靶场对php7.0是不兼容的(因为一些函数在php7中被删除了),所以搭建的时候要下载php5,如果你的系统要下载 ...
- cent8安装postgres
postgres是一款免费.开源的对象型关系数据库,其在cent8的安装方式与cent7的不太一样,特此记录. 步骤: 1 安装postgres server dnf install postgres ...
- IO-file-07 递归
package com.bwie.io; /** * 递归: * 方法自己调用自己 * 递归头:何时结束递归 * 递归体:重复调用 * @author Allen17805272076 * */ pu ...
- sql server 日期函数
一.sql server日期时间函数Sql Server中的日期与时间函数 1. 当前系统日期.时间 select getdate() 2. dateadd 在向指定日期加上一段时间的基 ...
- WUSTOJ 1290: 01字串(Java)
题目链接:
- WUSTOJ 1232: 矩阵乘法(C)
1232: 矩阵乘法 Time Limit: 1 Sec Memory Limit: 128 MB 64bit IO Format: %lld Description 小明明正在学习线性代数,老师布置 ...
- EXIT(外部中断)控制实验
实验目的 设计使用外接的按键来作为触发源,使得控制器产生中断,并在中断服务函数中实现控制小灯的亮灭. 按键硬件点路 编程要点 初始化用来产生中断的 GPIO: 初始化 EXTI: 配置 NVIC: 编 ...
- Python中操作mysql的pymysql模块详解(转载)
https://www.cnblogs.com/wt11/p/6141225.html