HDU 3938 offline query set

Source: Internet
Author: User
/* Perform offline and query on the set, sort the query side in ascending order, and sort the query side in ascending order. For query X, the answer is to ask about the value of the X-1 plus ask about the L1 of the X-1 and ask about the edge merging between the L2 of X to bring the value to an edge between L1 and L2 if the endpoint U, V is ignored in a collection, otherwise, the new path is the size of the set where u is located multiplied by the size of the set where v is located */# include <iostream> # include <cstdio> # include <cstring> # include <cmath> # include <algorithm> # include <queue> # include <vector> # include <stack> # include <map> using namespace STD; const int M = 50009; const int n = 10009; struct edge {int U, V, W;} edge [m]; bool CMP (edge a, edge B) {return. W <B. W ;} Struct que {int num, Val;} Qu [N]; bool cmp2 (que a, que B) {return. val <B. val;} int n, m, Q; int Fa [N], Heav [N]; int ans [N]; void Init () {for (INT I = 0; I <m; I ++) {scanf ("% d", & edge [I]. u, & edge [I]. v, & edge [I]. w) ;}} int find (int x) {return Fa [x] = x? X: Fa [x] = find (Fa [x]);} int Union (int x, int y) {If (heav [x]> heav [y]) {Fa [y] = x; heav [x] + = heav [y];} else {Fa [x] = y; heav [y] + = heav [x] ;}} void solve () {for (INT I = 1; I <= N; I ++) {Fa [I] = I; heav [I] = 1 ;}for (INT I = 1; I <= Q; I ++) {scanf ("% d ", & Qu [I]. val); qu [I]. num = I;} Sort (edge, edge + M, CMP); sort (qu + 1, Qu + q + 1, cmp2); Int J = 0; for (INT I = 1; I <= Q; I ++) {ans [Qu [I]. num] = ans [Qu [I-1]. num]; while (edge [J]. W <= Qu [I]. val & J <m) {Int x = find (edge [J]. U); int y = find (edge [J]. V); If (X! = Y) {ans [Qu [I]. num] + = heav [x] * heav [y]; // cout <Qu [I]. num <"#" <ans [Qu [I]. num] <Endl; Union (x, y);} J ++; }}for (INT I = 1; I <= Q; I ++) {printf ("% d \ n", ANS [I]) ;}} int main () {While (scanf ("% d", & N, & M, & Q )! = EOF) {Init (); solve ();} 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.