AtCoder Grand Contest 018 D - Tree and Hamilton Path
题目传送门:https://agc018.contest.atcoder.jp/tasks/agc018_d
题目大意:
给定一棵\(N\)个点的带权树,求最长哈密顿路径(不重不漏经过每个点一次,两点之间转移可以看做瞬移,对答案贡献为两点之间的距离)
这道题直接计算不好算,我们考虑每条边的贡献,基于一种贪心的思想,我们发现只要围着树的重心跑,就可以使每条边得到充分利用
考虑边i的贡献,我们假定边i割掉后分成两个大小为x,y的联通块,那么贡献则为\(2*v[i]*min(x,y)\)
因为我们走的不是回路,因此有一条边不会被算两次。如果树的重心只有一个,那么必然减去与重心相连的边权最小的边;如果树的重心有两个,减去两重心相连的边即可
/*program from Wolfycz*/
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define inf 0x7f7f7f7f
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
inline char gc(){
static char buf[1000000],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;
}
inline int frd(){
int x=0,f=1;char ch=gc();
for (;ch<'0'||ch>'9';ch=gc()) if (ch=='-') f=-1;
for (;ch>='0'&&ch<='9';ch=gc()) x=(x<<1)+(x<<3)+ch-'0';
return x*f;
}
inline int read(){
int x=0,f=1;char ch=getchar();
for (;ch<'0'||ch>'9';ch=getchar()) if (ch=='-') f=-1;
for (;ch>='0'&&ch<='9';ch=getchar()) x=(x<<1)+(x<<3)+ch-'0';
return x*f;
}
inline void print(int x){
if (x<0) putchar('-'),x=-x;
if (x>9) print(x/10);
putchar(x%10+'0');
}
const int N=2e5;
int pre[(N<<1)+10],now[N+10],child[(N<<1)+10],val[(N<<1)+10];
int size[N+10],Msize[N+10];
int n,tot,rize,root;
ll Ans;
void join(int x,int y,int z){pre[++tot]=now[x],now[x]=tot,child[tot]=y,val[tot]=z;}
void insert(int x,int y,int z){join(x,y,z),join(y,x,z);}
void dfs(int x,int fa,int v){
int Max=0; size[x]=1;
for (int p=now[x],son=child[p];p;p=pre[p],son=child[p]){
if (son==fa) continue;
dfs(son,x,val[p]),size[x]+=size[son];
Max=max(Max,size[son]);
}
Ans+=1ll*min(size[x],n-size[x])*v*2;
Max=max(Max,n-size[x]);
Msize[x]=Max;
if (Max<rize){
rize=Max;
root=x;
}
}
int main(){
n=read(),rize=inf;
for (int i=1;i<n;i++){
int x=read(),y=read(),z=read();
insert(x,y,z);
}
dfs(1,0,0);
int tmp=inf;
for (int p=now[root],son=child[p];p;p=pre[p],son=child[p]){
tmp=min(tmp,val[p]);
if (Msize[root]==Msize[son]){
Ans-=val[p];
printf("%lld\n",Ans);
return 0;
}
}
printf("%lld\n",Ans-tmp);
return 0;
}
AtCoder Grand Contest 018 D - Tree and Hamilton Path的更多相关文章
- AtCoder Grand Contest 010 F - Tree Game
题目传送门:https://agc010.contest.atcoder.jp/tasks/agc010_f 题目大意: 给定一棵树,每个节点上有\(a_i\)个石子,某个节点上有一个棋子,两人轮流操 ...
- AtCoder Grand Contest 005 C - Tree Restoring
题目传送门:https://agc005.contest.atcoder.jp/tasks/agc005_c 题目大意: 给定一个长度为\(N\)的整数序列\(A_i\),问能否构造一个\(N\)个节 ...
- AtCoder Grand Contest 018 A
A - Getting Difference Time limit時間制限 : 2sec / Memory limitメモリ制限 : 256MB 配点 : 300 点 問題文 箱に N 個のボールが入 ...
- AtCoder Grand Contest 018 E Sightseeing Plan
题意: 给定三个矩形,选定三个点,答案加上第一个点出发经过第二个点在第三个点结束的方案数,只能往右或往下走. 折腾了我半个多下午的题. 设三个矩形为$A,B,C$一个思路是枚举$B$的那个点$s(x, ...
- 【贪心】【堆】AtCoder Grand Contest 018 C - Coins
只有两维的时候,我们显然要按照Ai-Bi排序,然后贪心选取. 现在,也将人按照Ai-Bi从小到大排序,一定存在一个整数K,左侧的K个人中,一定有Y个人取银币,K-Y个人取铜币: 右侧的X+Y+Z-K个 ...
- 【贪心】AtCoder Grand Contest 018 B - Sports Festival
假设我们一开始选取所有的运动项目,然后每一轮将当前选择人数最多的运动项目从我们当前的项目集合中删除,尝试更新答案.容易发现只有这样答案才可能变优,如果不动当前选取人数最多的项目,答案就不可能变优. 我 ...
- 【GCD】AtCoder Grand Contest 018 A - Getting Difference
从大到小排序,相邻两项作差,求gcd,如果K是gcd的倍数并且K<=max{a(i)},必然有解,否则无解. 可以自己手画画证明. #include<cstdio> #include ...
- AtCoder Grand Contest 018 A - Getting Difference
A - Getting Difference Time limit : 2sec / Memory limit : 256MB Score : 300 points Problem Statement ...
- AtCoder Grand Contest 018题解
传送门 \(A\) 根据裴蜀定理显然要\(k|\gcd(a_1,...,a_n)\),顺便注意不能造出大于\(\max(a_1,...,a_n)\)的数 int n,g,k,x,mx; int mai ...
随机推荐
- 翻译:A Tutorial on the Device Tree (Zynq) -- Part III
A Tutorial on the Device Tree (Zynq) -- Part III 定义外设 可能你读本文是为了给你的设备写一个Linux驱动,在这方面要推荐著名的<Linux D ...
- 使用Genymotion调试出现错误INSTALL_FAILED_CPU_ABI_INCOMPATIBLE解决的方法
今天在使用android studio在Genymotion上调试程序出现INSTALL_FAILED_CPU_ABI_INCOMPATIBLE导致程序安装不了: 后来百度发现是要安装:http:// ...
- eclipse 添加库
Window ->Preferences ->Java ->Build Path ->User Libraries New,起个名字,如myLibrary add jars,添 ...
- org.apache.flume.EventDeliveryException: NettyAvroRpcClient { host: hadoop1, port: 41414 }: Failed to send event
org.apache.flume.EventDeliveryException: NettyAvroRpcClient { host: hadoop1, port: 41414 }: Failed t ...
- Sublime Text web开发神器
开发工具介绍 开发工具一般分为两种类型:文本编辑器和集成开发环境(IDE) 常用的文本编辑器:Sublime Text.Notepad++.EditPlus等 常用的IDE:WebStorm.Inte ...
- Get Luffy Out (poj 2723 二分+2-SAT)
Language: Default Get Luffy Out Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7969 ...
- 计算(!(~+[])+{})[--[~+""][+[]]*[~+[]] + ~~!+[]]+({}+[])[[~!+[]]*~+[]]
(!(~+[])+{})[--[~+""][+[]]*[~+[]] + ~~!+[]]+({}+[])[[~!+[]]*~+[]] 一.JS运算符的优先级 首先要运用到的第一个 ...
- iOS 中代码获取当前版本号
[1]概念 iOS的版本号,一个叫做Version,一个叫做Build,这两个值都可以在Xcode 中选中target,点击“Summary”后看到. Version在plist文件中的key是“CF ...
- linux 一个超简单的makefile
makefile 自动化变量: $@ : 规则的目标文件名 例如:main:main.o test.o g++ -Wall -g main.o test. ...
- 缓存框架Ehcache相关
单点缓存框架 只能针对单个jvm中,缓存容器存放jvm中,每个缓存互不影响 Ehcache gauva chache 内置缓存框架 jvm缓存框架 分布式缓存框架(共享缓存数据) Redis ...