EOJ Monthly 2018.8 D. Delivery Service-树上差分(边权/边覆盖)(边权转点权)(模板题)
单测试点时限: 2.5 秒
内存限制: 512 MB
EOJ Delivery Service Company handles a massive amount of orders in our nation. As is well known, our nation has ncities, with n−1 bi-directional paths connecting them, forming a tree. The length of the i-th path is wi, meaning that the time we have to spend walking through this path is wi.
Now, you, as the headquarter of EOJ, has somehow learned a magic spell. Each time you cast a spell on two paths, the lengths of these two paths magically swap with each other. You can do this magic as many times as you want(possibly none).
We have q orders waiting to be handled. The i-th order demands sending a package from city si to city ti. We kindly ask you to do your magic before all the deliveries start, such that the total time we have to spend on delivering these packages is minimized.
输入
The first line of the input contains one single integer n (1≤n≤2⋅105).
The next n−1 lines tell about the path information from path 1 to path n−1, the i-th of which contains three space-separated integers ui, vi and wi (1≤ui,vi≤n, ui≠vi, 1≤wi≤1000).
The next line contains one integer q (1≤q≤2⋅105).
The next q lines each contains two space-separated integers si and ti (1≤si,ti≤n,si≠ti).
输出
Output one integer, the minimum total time you can achieve.
样例
5
1 2 2
1 3 3
3 4 3
3 5 4
2
1 5
4 2
14
提示
In the example, we swap the weights between edge (1,2) and (1,3), so that the first order takes 2+4=6 time units to complete; the second order takes 2+3+3=8 time units. Thus the total time is 14.
题意就是给你一棵树,给你边(双向路径)和边权,边权是走这条路要花的时间,你有神奇的能力,可以交换任意两条边的边权,并且你可以无数次使用你的能力。然后给你m对点,表示要走路径<a,b>,要求你只能在他们开始走之前使用你的能力,问走完之后要花的最少时间是多少。
因为是走之前使用能力,所以可以提前处理出来,所以可以树上差分,所以树上差分,对边覆盖,把所有要走的路径先处理一下,sum[a]++,sum[b]++,sum[lca]-=2;然后Dfs遍历,求出来每条边走的次数,然后排个序,走的最多的路给最小的边权,就可以了。
代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstdlib>
using namespace std;
typedef long long ll;
const int maxn=2e5+; struct node{
int to,next;
}edge[maxn<<]; bool cmp(int a,int b)
{
return a>b;
}
int head[maxn<<],sum[maxn],dep[maxn],fa[maxn][],n,m,cnt,ans; void add(int x,int y){edge[++cnt].to=y,edge[cnt].next=head[x],head[x]=cnt;} void dfs(int u,int fath)//第一遍dfs,把信息处理出来
{
dep[u]=dep[fath]+,fa[u][]=fath;
for(int i=;fa[u][i];++i) fa[u][i+]=fa[fa[u][i]][i];
for(int i=head[u];i;i=edge[i].next){
int v=edge[i].to;
if(v!=fath) dfs(v,u);
}
} int LCA(int u,int v)//LCA
{
if(dep[u]>dep[v]) swap(u,v);
for(int i=;i>=;i--) if(dep[u]<=dep[v]-(<<i)) v=fa[v][i];
if(u==v) return u;
for(int i=;i>=;i--) if(fa[u][i]!=fa[v][i]) u=fa[u][i],v=fa[v][i];
return fa[u][];
} void Dfs(int u,int fath)//遍历求和
{
for(int i=head[u];i;i=edge[i].next){
int v=edge[i].to;
if(v==fath) continue;
Dfs(v,u);
sum[u]+=sum[v];
}
} int w[maxn]; int main()
{
int n;
scanf("%d",&n);
for(int i=;i<n;i++){
int x,y,v;
scanf("%d%d%d",&x,&y,&v);
add(x,y);add(y,x);
w[i]=v;
}
dfs(,);
int q;
scanf("%d",&q);
for(int i=;i<=q;i++){
int x,y;
scanf("%d%d",&x,&y);
int lca=LCA(x,y);
sum[x]++;sum[y]++;sum[lca]-=;
}
Dfs(,);
sort(w+,w++n-);
sort(sum+,sum+n+,cmp);
ll ans=;
for(int i=;i<n;i++){
ans+=(ll)w[i]*sum[i];
}
cout<<ans<<endl;
}
溜了。。。
EOJ Monthly 2018.8 D. Delivery Service-树上差分(边权/边覆盖)(边权转点权)(模板题)的更多相关文章
- EOJ Monthly 2018.7
准备继续大学acm啦 又要开始愉快的码码码啦 第一次在华东师大OJ上面做题 看来EOJ上的积分体质是假的,我怎么一把上红??? A.数三角形 神tm的防AK题放在A,出题人很不友好啊... 先写了个暴 ...
- EOJ Monthly 2018.4
A. ultmaster 的小迷妹们 Time limit per test: 2.0 seconds Memory limit: 256 megabytes ultmaster 男神和他的小迷妹们准 ...
- EOJ Monthly 2018.4 (E.小迷妹在哪儿(贪心&排序&背包)
ultmaster 男神和小迷妹们玩起了捉迷藏的游戏. 小迷妹们都希望自己被 ultmaster 男神发现,因此她们都把自己位置告诉了 ultmaster 男神,因此 ultmaster 男神知道了自 ...
- [EOJ Monthly 2018.10][C. 痛苦的 01 矩阵]
题目链接:C. 痛苦的 01 矩阵 题目大意:原题说的很清楚了,不需要简化_(:з」∠)_ 题解:设\(r_i\)为第\(i\)行中0的个数,\(c_j\)为第\(j\)列中0的个数,\(f_{i,j ...
- EOJ Monthly 2018.11 D. 猜价格
猜价格 分两种情况讨论: k≤n,先猜至多 k 次 1,由于回答 <1 肯定是假的,所以可以把剩余系下是哪次错试出来,然后用至多 n 次搞定. k>n,每个数都猜两次,如果两次结果不一样, ...
- 【EOJ Monthly 2018.7】【D数蝌蚪】
https://acm.ecnu.edu.cn/contest/92/problem/D/ D. 数蝌蚪 Time limit per test: 2.0 seconds Memory limit: ...
- EOJ Monthly 2018.7 B.锐角三角形(数学几何+思维)
描述 是否存在面积为S/2的整点锐角三角形?存在输出Yes并输出三个整点坐标,否则输出No. 注意如果存在输出的坐标必须在long long范围内. Input 第一行一个整数S(1<=S< ...
- EOJ Monthly 2018.11 猜价格 (模拟)
分三种情况: 1.k=1.此时每次都说反话,反着二分即可. 2.1<k <= n.那么在前n次问答中一定会出现一次错误,通过不断输出1找出那个错误发生的位置(若回答是>那这就是错误) ...
- EOJ Monthly 2018.2
A. 坑爹的售票机 题意 用\(1,5,10,25,50,100\)的纸币买\(n\)张单价为\(p\)的船票,且一次性最多买\(k\)张,求钱数恰好时最少需要多少张纸币. Hard: \(n,k,p ...
随机推荐
- notify()与notifyAll()
notify() :随机唤醒一个线程. notifyAll():唤醒等待某个锁的所有任务. 在技术上,可能会有多个任务在所创建的任务上处于wait()状态,调用notifyAll()比只调用notif ...
- 上下文路径request.getContextPath();与${pageContext.request.contextPath}
(1) request.getContextPath();与${pageContext.request.contextPath}都是获取上下文路径: 1. request.getContextPath ...
- 【转载】Java JVM : Xms Xmx PermSize MaxPermSize 区别
转载自:http://cxh61207.iteye.com/blog/1160663 java JVM虚拟机选项: Xms Xmx PermSize MaxPermSize 区别 Xms 是指设定程 ...
- 调戏OpenShift:一个免费能干的云平台(已失效)
一.前因后果 以前为了搞微信的公众号,在新浪sae那里申请了一个服务器,一开始还挺好的 ,有免费的云豆送,但是一直运行应用也要消费云豆,搞得云豆也所剩无几了.作为一名屌丝,日常吃土,就单纯想玩一玩微信 ...
- 如何设计一个优雅健壮的Android WebView?(上)
转:如何设计一个优雅健壮的Android WebView?(上) 前言 Android应用层的开发有几大模块,其中WebView是最重要的模块之一.网上能够搜索到的WebView资料可谓寥寥,Gith ...
- Spring 与 Quartz 动态配置(数漫江湖)
因为项目的需求,需要有动态配置计划任务的功能.本文在 Quartz JobBean 中获取配置的 Quartz cronExpression 时间表达式及 Spring Bean 的对象名.方法名并运 ...
- 【Windows使用笔记】Windows日常使用软件
整理一些对于我来说日常使用的Windows软件. 排名不分先后,仅凭我想起来的顺序! 1 MadAppLauncher 这个对我来说非常需要了. 使用它可以快速启动日常常用的软件,非常快捷高效.一般来 ...
- Optimizing subroutine calls based on architecture level of called subroutine
A technique is provided for generating stubs. A processing circuit receives a call to a called funct ...
- python基础===requests学习笔记
这里有一个新的学习requests网站:http://docs.python-requests.org/zh_CN/latest/user/quickstart.html2017/11/30 Requ ...
- 2017多校第9场 HDU 6162 Ch’s gift 树剖加主席树
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6162 题意:给出一棵树的链接方法,每个点都有一个数字,询问U->V节点经过所有路径中l < ...