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 ...
随机推荐
- Scrapy中的Callback如何传递多个参数
在scrapy提交一个链接请求是用 Request(url,callback=func) 这种形式的,而parse只有一个response参数,如果自定义一个有多参数的parse可以考虑用下面的方法实 ...
- 【updating】python读书笔记-The Django Book2.0(for django1.4)
原文:http://www.djangobook.com/en/2.0/frontmatter.html 译文:http://djangobook.py3k.cn/2.0/ 或者http://docs ...
- 问题03.如果有多个集合的迭代处理情况【使用MAP】
在SQL开发过程中,动态构建In集合条件查询是比较常见的用法,在Mybatis中提供了foreach功能,该功能比较强大,它允许你指定一个集合,声明集合项和索引变量,它们可以用在元素体内.它也允许你指 ...
- JavaScript之typedof,instanceof,Array.isArray()
typedof value检测对象类型. value instanceof Array检测a对象是否由b对象类型 Array.isArray(value)在两个框架中判断是否为数组类型.
- Linux powercli 以及connect-viserver 连接问题
1. 参考文档 http://fdo-workspace.blogspot.hk/2017/07/powershell-powercli-for-linux-server.html 2. powers ...
- 【LA】5135 Mining Your Own Business
[算法]点双连通分量 [题解]详见<算法竞赛入门竞赛入门经典训练指南>P318-319 细节在代码中用important标注. #include<cstdio> #includ ...
- 【转】IOS版本自定义字体步骤
本文转载自:http://quick.cocoachina.com/wiki/doku.php?id=ios%E7%89%88%E6%9C%AC%E4%BD%BF%E7%94%A8%E8%87%AA% ...
- ios的app,有新版本时必须先更新。
现在没时间整理,先把代码贴出来,以后再做详细的思路整理. 1 在AppController.mm的didFinishLaunchingWithOptions方法里面获取本地应用版本信息,保存起来. / ...
- Git彻底删除历史提交记录的方法
有时候我们可能会遇到git提交错误的情况,比如提交了敏感的信息或者提交了错误的版本.这个时候我们想将提交到代码库的记录删除,我们要怎么做呢? 首先,我们需要找到我们需要回滚到的提交点的hash,可以使 ...
- 表格td内容超出宽度显示... table-layout: fixed;
td宽度用百分比固定好的时候,即使设置了 white-space:nowrap;/*文本不会换行,在同一行显示*/ overflow:hidden;超出隐藏 text-overflow:ellipsi ...