HDU 4424 Conquer a New region largest spanning tree

Source: Internet
Author: User
Tags set set sort

Give you a tree each side has a weighted value select one point for center define S value as center to other n-1 points on the path of the minimum edge to seek all points s value of the and

From large to small sort each merge 2 tree set to a set B set set a set of the maximum s value of the and for Suma b set for Sumb

Center in A or b now join a A-c this edge makes 2 sets connected because the weight of the side of a-a is less than equal to the weight of the inner edge of the AB set so if the center is in a then SumA = Suma+b set of points *a-b the weight of this edge Sumb = number of points in the Sumb+a collection * The weight of this side of a-B is greater than 2.

#include <cstdio> #include <cstring> #include <algorithm> #include <queue> #include <cmath
> Using namespace std;
const int MAXN = 200010;
int n;
int F[MAXN], RANK[MAXN];

__int64 SUM[MAXN]; struct Edge {int u, V, W;}
E[MAXN];
	int find (int x) {if (x = f[x]) return f[x] = find (f[x]);
return f[x];  } BOOL CMP (Edge A, Edge b) {return A.W > B.W;} int main () {while (scanf ("%d", &n)! = EOF) {for (int i = 0; i < n-1;
		i++) scanf ("%d%d%d", &e[i].u, &AMP;E[I].V, &AMP;E[I].W);
		Sort (E, e+n-1, CMP);
		for (int i = 0; I <= N; i++) F[i] = i, sum[i] = 0, rank[i] = 1;
			for (int i = 0; i < n-1; i++) {int x = find (E[I].U);
			int y = find (E[I].V);
					if (x! = y) {if (sum[x]+ (__int64) e[i].w*rank[y] > sum[y]+ (__int64) e[i].w*rank[x]) {f[y] = x;
					SUM[X] = sum[x]+ (__int64) e[i].w*rank[y];
				RANK[X] + = Rank[y];
					} else {f[x] = y;
					Sum[y] = sum[y]+ (__int64) e[i].w*rank[x]; Rank[y] + = RANK[X];
		}}} int x = find (1);
	printf ("%i64d\n", sum[x]);
} 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.