The Experience of Love

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 645    Accepted Submission(s): 216

Problem Description
A girl named Gorwin and a boy named Vivin is a couple. They arrived at a country named LOVE. The country consisting of N cities and only N−1
edges (just like a tree), every edge has a value means the distance of
two cities. They select two cities to live,Gorwin living in a city and
Vivin living in another. First date, Gorwin go to visit Vivin, she would
write down the longest edge on this path(maxValue).Second date, Vivin
go to Gorwin, he would write down the shortest edge on this
path(minValue),then calculate the result of maxValue subtracts minValue
as the experience of love, and then reselect two cities to live and
calculate new experience of love, repeat again and again.

Please help them to calculate the sum of all experience of love after they have selected all cases.

 
Input
There will be about 5 cases in the input file.
For each test case the first line is a integer N, Then follows n−1 lines, each line contains three integers a, b, and c, indicating there is a edge connects city a and city b with distance c.

[Technical Specification]
1<N<=150000,1<=a,b<=n,1<=c<=109

 
Output
For
each case,the output should occupies exactly one line. The output
format is Case #x: answer, here x is the data number, answer is the sum
of experience of love.
 
Sample Input
3
1 2 1
2 3 2
5
1 2 2
2 3 5
2 4 7
3 5 4
 
Sample Output
Case #1: 1
Case #2: 17

Hint

huge input,fast IO method is recommended.

In the first sample:
The maxValue is 1 and minValue is 1 when they select city 1 and city 2, the experience of love is 0.
The maxValue is 2 and minValue is 2 when they select city 2 and city 3, the experience of love is 0.
The maxValue is 2 and minValue is 1 when they select city 1 and city 3, the experience of love is 1.
so the sum of all experience is 1;

 
 
题意:给出一棵树n个结点n-1条边,找到所有的两个点之间的最大值和最小值,求 sum(MAX-MIN).
题解:sum(MAX-MIN) = sum(MAX)-sum(MIN)很巧妙的思路,并查集将n-1条边添加进去,按照边权从小到大排序,如果现在找到了点 a ,b 那么a的所有子节点和 b的所有子节点中的最大权边必定为 edge[a][b] 所以我们根据乘法原理可以得出当前的所有最大权为 edge[a][b]  的点,他们对最大值结果的贡献为 cnt[a]*cnt[b]*edge[a][b].最小值的话从大到小选就行了。最后结果会爆long long ,所以要开 unsigned long long ,输入输出用 %I64u
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#include <math.h>
using namespace std;
typedef unsigned long long LL;
const int N = ;
struct Edge
{
LL u,v;
LL w;
} edge[N];
LL father[N];
LL cnt[N];
LL MAX,MIN;
LL _find(LL x)
{
if(x!=father[x]){
father[x] = _find(father[x]);
}
return father[x];
}
void init(int n)
{
for(int i=; i<=n; i++)
{
father[i] = i;
cnt[i] = ;
}
}
void Union(LL a,LL b,LL w,int flag)
{
LL x = _find(a);
LL y = _find(b);
if(flag)
{
MAX+=cnt[x]*cnt[y]*w;
}
else
{
MIN+=cnt[x]*cnt[y]*w;
}
father[x] = y;
cnt[y]+=cnt[x];
}
int cmp(Edge a,Edge b)
{
return a.w<b.w;
}
int main()
{
int n;
int t = ;
while(scanf("%d",&n)!=EOF)
{
init(n);
for(int i=; i<n; i++)
{
scanf("%I64u%I64u%I64u",&edge[i].u,&edge[i].v,&edge[i].w);
}
MAX = ,MIN = ;
sort(edge+,edge+n,cmp);
for(int i=; i<n; i++)
{
Union(edge[i].u,edge[i].v,edge[i].w,);
}
init(n);
for(int i=n-; i>=; i--)
{
Union(edge[i].u,edge[i].v,edge[i].w,);
}
printf("Case #%d: ",t++);
printf("%I64u\n",MAX-MIN);
}
return ;
}

最新文章

  1. Android实现侧边栏SlidingPaneLayout
  2. HttpClient方式模拟http请求
  3. TCP UDP 协议的区别和联系
  4. 数据库笔记--常见sql操作
  5. 面向对象 理解 C#复习
  6. delphi webbrowser 经常用法演示样例
  7. Strusts2--课程笔记9
  8. MongoDB文档基本操作
  9. Akka(4): Routers - 智能任务分配
  10. android studio2.0出现的gradle问题,instant Run即时运行不了.
  11. mybatis 参数格式异常-- Error querying database. Cause: java.lang.NumberFormatException: For input string
  12. JQuery+formValidator实现表单验证
  13. CDR锁定方式
  14. vue--使用vue-cli构建项目
  15. Java使用Sockt进行通信(2)
  16. Minimum Integer CodeForces - 1101A (思维+公式)
  17. Always run a program in administrator mode in Windows 10
  18. PAT 1043 输出PATest
  19. MVC控制器使用总结
  20. JavaWeb基础—JDBC(二)事务与批处理

热门文章

  1. [转]dwr3框架学习笔记--简介及原理简介
  2. 【转】cocos2dx 3.x 集成protobuf
  3. hibernate笔记(四)
  4. oracle补充
  5. 关于&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html:charset=UTF-8&quot;&gt;
  6. BZOJ4444 SCOI2015国旗计划(贪心+倍增)
  7. P2066 机器分配
  8. [Leetcode] The minimum depth of binary tree二叉树的最小深度
  9. CF451E Devu and Flowers 解题报告
  10. 团队代码中Bug太多怎么办?怎样稳步提高团队的代码质量