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 ...
随机推荐
- 【ARM-Linux开发】Rico Board上编译USB WiFi RT3070驱动
1.附件中提供了RT3070驱动源码包DPO_RT5572_LinuxSTA_2.6.1.3_20121022.tar.gz和编译好的驱动,这里选择使用taget_file.tar.gz中已经编译好的 ...
- 【miscellaneous】各种音视频编解码学习详解
编解码学习笔记(一):基本概念 媒体业务是网络的主要业务之间.尤其移动互联网业务的兴起,在运营商和应用开发商中,媒体业务份量极重,其中媒体的编解码服务涉及需求分析.应用开发.释放license收费等等 ...
- Linux与windows的对比
Linux与windows的对比 声明:下面的内容总结自实验楼的linux教程,特在此说明. Linux与windows一样,是一个操作系统. Linux与Windows的区别 软件与支持 windo ...
- 火狐低版本中显示时间格式为:yyyy-MM-dd hh:mm:ss,出现NaN
在低版本的火狐(43以下)和IE8中,显示时间格式为:yyyy-MM-dd hh:mm:ss,会出现NaN:原因是只支持yyyy/MM/dd hh:mm:ss; 所以在new Date('2018-0 ...
- OpenCV Python 4.0安装
1.安装OpenCV-Python 如果你是第一次使用OpenCV Python开发包,想要安装OpenCV Python4.0只要执行如下命令行即可: pip install opencv-pyth ...
- subplot()一个窗口画多个图
import matplotlib.pyplot as plt plt.subplot(m,n,p) m,n表示一个窗口上显示m行n列 p表示正在处理第p个区域的部分(区域编号从左到右,从上到下) f ...
- Linux安装expect
主要参考:https://www.cnblogs.com/zhenbianshu/p/5867440.html expect解释器 expect是一个能实现自动和交互式任务的解释器,它也能解释常见的s ...
- K8S从入门到放弃系列-(3)部署etcd集群
摘要:etcd 是k8s集群最重要的组件,用来存储k8s的所有服务信息, etcd 挂了,集群就挂了,我们这里把etcd部署在master三台节点上做高可用,etcd集群采用raft算法选举Leade ...
- 《Mysql 锁 - 概述》
一:锁类型(加锁范围区分类型) - MySQL里面的锁可以分为:全局锁.表级锁.行级锁. 二:全局锁 - 作用 - 对整个数据库实例加锁. - 加锁方式 - MySQL提供加全局读锁的方法:Flus ...
- Win10 自定义鼠标右键菜单
1. 点击文件鼠标右键显示软件 1.1 步骤 win+R输入regedit进入注册表 定位到HKEY_CLASSES_ROOT\*\shell下 在shell创建一个你想要的右键文件 例如:Kinok ...