Zoj 3659 Conquer a New Region

Source: Internet
Author: User

Question:
There is a tree with each edge value. The path weight from one point on the tree to another is the minimum weight edge on the path, select a vertex so that the path weights of the vertex and all other vertices are the maximum.

Question:
Let's take a look at the scale. It may be greedy, dp, line segment tree, and so on. The line segment tree obviously has no idea. dp, too many states, so think about it. greedy, but no matter what method, the ultimate goal is to get a rooted tree. here we can use and query set + path compression for this purpose.
Greedy, we only need to consider whether the build tree is in descending order or ascending order of Edge Weight Values. If Ascending Order is selected, the current edge will be limited by the edge added to the front, so it is troublesome, in descending order, the added edge is limited by the current edge, so it is much easier.
We need to use the path weights and number of children that each father and each father need to save to their children and grandchildren.
1) There are no fathers at any of the two points. Either of them serves as a father.
2) one of them has no father and uses his father as a father without a father (this is a detour = ).
3) If you have a father, you need to determine who is a father and can make the tree path larger, here we need to use the path weights and the number of children and grandchildren (I have used them, and Daniel may not need them ...).

Code:
[Cpp]
# Include <stdlib. h>
# Include <string. h>
# Include <stdio. h>
# Include <ctype. h>
# Include <math. h>
# Include <stack>
# Include <queue>
# Include <map>
# Include <set>
# Include <vector>
# Include <string>
# Include <iostream>
# Include <algorithm>
Using namespace std;
 
# Define ll long
# Define ls rt <1
# Define rs ls | 1
# Define lson l, mid, ls
# Define rson mid + 1, r, rs
# Define middle (l + r)> 1
# Define eps (1e-9)
# Define type int
# Define clr_all (x, c) memset (x, c, sizeof (x ))
# Define clr (x, c, n) memset (x, c, sizeof (x [0]) * (n + 1 ))
# Define MOD 1000000007.
# Define inf 0x3f3f3f
# Define pi acos (-1.0)
# Define M 200000 + 5
 
Template <class T> void _ swap (T & x, T & y) {T t = x; x = y; y = t ;}
Template <class T> T _ max (T x, T y) {return x> y? X: y ;}
Template <class T> T _ min (T x, T y) {return x <y? X: y ;}
Int test, cas;
 
Int n, m;
Struct node {
Int u, v;
Ll c;
Void read (){
Scanf ("% d % lld", & u, & v, & c );
}
Bool operator <(const node & t) const {
Return c> t. c;
}
} P [M];
Ll sum [M], cnt [M];
Int fa [M];
 
Int Find (int x ){
For (; x! =-1 & x! = Fa [x]; x = fa [x]);
Return x;
}
 
Void Union (int u, int v, ll val ){
Int uf = Find (u), vf = Find (v );
If (uf =-1 & vf =-1)
Fa [u] = v, sum [v] + = val, cnt [v] ++, fa [v] = v;
Else if (uf =-1)
Fa [u] = vf, sum [vf] + = val, cnt [vf] ++;
Else if (vf =-1)
Fa [v] = uf, sum [uf] + = val, cnt [uf] ++;
Else {
Ll tu = sum [uf] + (cnt [vf] + 1) * val;
Ll TV = sum [vf] + (cnt [uf] + 1) * val;
If (tu> TV) fa [vf] = uf, sum [uf] = tu, cnt [uf] + = cnt [vf] + 1;
Else fa [uf] = vf, sum [vf] = TV, cnt [vf] + = cnt [uf] + 1;
}
}
 
Void run (){
Int I, j;
Clr (sum, 0, n), clr (cnt, 0, n );
Clr (fa,-1, n );
M = n-1;
For (I = 0; I <m; I ++)
P [I]. read ();
Sort (p, p + m );
For (I = 0; I <m; I ++)
Union (p [I]. u, p [I]. v, p [I]. c );
For (I = 1; I <= n; I ++) if (cnt [I] + 1 = n ){
Printf ("% lld \ n", sum [I]);
Return;
}
}
 
Void preSof (){
}
 
Int main (){
// Freopen ("1.in"," r ", stdin );
// Freopen ("1.out"," w ", stdout );
PreSof ();
// Run ();
While (~ Scanf ("% d", & n) run ();
// For (scanf ("% d", & test), cas = 1; cas <= test; cas ++) run ();
Return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.