luoguP1131
luoguP1131
。。。这道题我也不知道咋\(A\)的。
思路:
\(anst\) 距离 \(s\) 的最长距离,\(ansp\) 某一节点到祖先所有边的权值和这些些加过的权值和
先求出到\(s\)距离最长的点,其他点通过使用道具增加到这个点的长度。对于每一个节点,记录一个它的子节点到它的距离最长的点(距离\(dis[u]\))。在\(u\)到它父亲的边上加\(anst-ansp-dis[u]\),\(ans\)也加上\(anst-ansp-dis[u]\)。
。。。 解释得不太清楚
#include<iostream>
#include<cstdio>
#include<cstring>
#define LL long long
using namespace std;
const int N = 500005;
int head[N],n,tot,s,m[N];
struct edge{
int node,next,data;
}e[N<<1];
long long ans,anst,dis[N];
void add(int x,int y,int z)
{
e[++tot].node=y; e[tot].data=z;
e[tot].next=head[x]; head[x]=tot;
}
inline int read()
{
int ans=0,w=1;
char c=getchar();
while((c<'0'||c>'9')&&c!='-') c=getchar();
if(c=='-') { w=-1; c=getchar(); }
while(c>='0'&&c<='9')
{ ans=ans*10+c-'0'; c=getchar(); }
return ans*w;
}
void dfs(int u,int fa)
{
for(int i=head[u];i;i=e[i].next)
{
int v=e[i].node;
if(v==fa) continue;
dfs(v,u);
if(dis[v]+e[i].data>dis[u])
dis[u]=dis[v]+e[i].data,m[u]=v;
}
}
void dfs1(int u,int fa,LL ansp)
{
ans+=(anst-dis[u]-ansp),ansp+=(anst-dis[u]-ansp);
for(int i=head[u];i;i=e[i].next)
{
int v=e[i].node;
if(v==fa) continue;
dfs1(v,u,ansp+e[i].data);
}
}
int main()
{
// memset(dis,0x7f,sizeof(dis));
n=read(); s=read();
int x,y,z;
for(int i=1;i<n;i++)
{
x=read(); y=read(); z=read();
add(x,y,z); add(y,x,z);
}
dfs(s,0);
anst=dis[s];
dfs1(s,0,0);
printf("%lld\n",ans);
return 0;
}
luoguP1131的更多相关文章
- [luoguP1131] [ZJOI2007]时态同步(贪心)
传送门 显然是一棵树. 又显然一段一段地增加比较优. 我们可以dfs,并且尽量把每一个节点所有子树中所有节点的时间增加到一样. #include <vector> #include < ...
- LuoguP1131 [ZJOI2007]时态同步 (树形DP,贪心)
贪心就离根最大距离 #include <iostream> #include <cstdio> #include <cstring> #include <al ...
随机推荐
- MarkdownPad
MarkdownPad Markdown编辑器,只能在windows下使用 下载地址 :http://markdownpad.com/ 破解: 邮箱:Soar360@live.com 授权证书 GBP ...
- AVIER Racing Drone App Privacy Policy
Personal Data collected for the following purposes and using the following services: Device permissi ...
- 用 hugo 和 netlify 搭建blog【转】
用 hugo 和 netlify 搭建blog - kok的笔记本 Releases · gohugoio/hugo · GitHub 测试baidu 测试163
- 【XSY2851】蛋糕 数学
题目大意 有一个边长为 \(1\) 的正 \(n\) 边形,你要把这个正 \(n\) 边形放到一个正 \(m\) 边形里面,且两个多边形的中心重合. 问你这个正 \(m\) 边形的边长最小是多少. \ ...
- webpack中跨域请求proxy代理(vue与react脚手架不同设置方法)
因为浏览器有同源策略的限制,导致我们在本地开发的时候,请求不同域名的接口会存在跨域的问题 解决跨域的问题有很多方式,这里主要整理下代理模式来解决跨域的问题 代理方式能够实现的机制大体: 本地服务器 - ...
- 使用sshpass同时更新一台ubuntu和一台CentOS
1.在ubuntu上安装sshpass sudo apt install sshpass 2.分别在两台的root路径下放上升级脚本: cent:/root/upgrade.sh #!/bin/bas ...
- Git如何合并一个已经在GitHub上提交但没有合并的Pull Request请求
步骤 进入Git仓库,执行curl -L https://github.com/<USER>/<REPO>/pull/<NO>.patch | git am
- 在不同DPI屏幕环境下,让图标显示的尺寸保持不变,使用 LoadImage() 加载图标
之前写过的一个客户端程序中,需要在状态栏上显示图标: 我当时使用的是:HICON LoadIcon(HINSTANCE hInstance, LPCTSTR lpIconName); 在DPI:125 ...
- 【优秀的图片后期编辑工具】Luminar 3.1 for Mac
[简介] 今天和大家分享最新的 Luminar for Mac 3.1 版本,支持中文界面,Luminar是一款Mac上优秀的图片后期处理工具,功能类似 Photoshop Lightroom 等软 ...
- 十行代码分清Java 的 || 和 &&
发现有些同学到现在还没分不清 || 和 &&的作用 package System; /** * * @ClassName: RandomTest * @Description: 十行代 ...