HDU 3938 Portal (and check set + offline +kruskal)

Source: Internet
Author: User
Tags new set

Search the problem to understand. Understand later found that in fact test instructions is very clear, problem-solving ideas are also very clear, but the problem is not clearly expressed ...

The effect is as follows--

Give you an no-show graph, the distance of any two points in the figure is an edge on all paths between two points, which needs to satisfy two conditions: 1. The longest side of a path between these two points on this side; 2. This edge is the shortest edge of the longest edge on all the paths between these two points.

Simply put, if a to D has two paths, one through B, and one through D, where AB = 1, bd = 3, AC = 2, cd = 2, then the longest edge on Abd is 2 on the longest side of 3,ACD, and the distance for ad is 2.

If the distance between A and d two points is less than energy L, then a portal can be established between a and d two points.

Now, find out how many portals can be built in this diagram in the energy of L.

Input:

Multiple sets of input data.

The first line of input data for each group consists of three integers n, m, Q. Represents the number of nodes, the number of sides, and the number of requests.

Next m line, three integers per line u, V, Val, indicates the source point of the edge, the destination point, Benquan (note, is the graph, the source point and the destination point equivalent).

The next Q line, an integer l for each line, represents the energy provided by the request.

Core problem: If the set X is not connected to the set Y, and there is a path l will connect X with Y, and L ' <= L, at which point a new portal num[x]*num[y], Num[x] represents the number of nodes in the X collection. After the L1 is connected, the collection x is merged with the set Y to get the new set x,num[x] + = Num[y], which is the set.

The +kruskal can be solved using and checking the set. That is, all edges are ordered from small to large, and each time the new edges are added to and checked in order, it is necessary to ensure that the new edges added do not form the ring until the edge length > The energy provided by the request.

The new problem arises, when we find the path in the L1 energy, then if the next request energy is L2, then we can not continue to solve the existing and check set, can only re-establish and check the set, which will produce a great waste. Therefore, we need to record all requests l1--lq, that is, off-line operations, and then in order from small to large to solve. Finally, the solution is sorted out in order of request.

On the code--

1#include <cstdio>2#include <cstring>3#include <cmath>4#include <algorithm>5 using namespacestd;6 7 Const intM =10010;8 9 structQue//Save QueryTen { One     intQ, id, ans;//query values, query order, output results, respectively A }que[m]; -  - structEdge//Save Edge the { -     intu, V, Val; -}edge[5*M]; -  + intFM[M];//and check the set use - intSUM[M];//record the number of interval nodes +  A intN, M, q; at  - BOOLCMP (Edge x, Edge y) - { -     returnX.val <=Y.val; - } -  in BOOLCMP1 (Que x, Que y) - { to     returnX.Q <=y.q; + } -  the BOOLCMP2 (Que x, Que y) * { $     returnX.id <y.id;Panax Notoginseng } -  the voidInit () + { A      for(inti =1; I <= N; i++) the     { +Fm[i] =i; -Sum[i] =1; $     } $      for(inti =0; I < m; i++) scanf ("%d%d%d", &edge[i].u, &AMP;EDGE[I].V, &edge[i].val); -Sort (Edge, edge+m, CMP);//Sort by path length from small to large -  the      for(inti =0; i < Q; i++) -     {Wuyiscanf"%d", &que[i].q); theQue[i].id =i; -Que[i].ans =0; Wu     } -Sort (que, que+q, cmp1);//Sort by request length from small to large About } $  - intMfind (intX//query operation with path compression - { -     intFX =x; A      while(FX! = Fm[fx]) FX =FM[FX]; +      while(X! =Fm[x]) the     { -         intMID =Fm[x]; $FM[X] =FX; thex =mid; the     } the     returnFX; the } -  in voidWork () the { the     intCNT =0; About      for(inti =0; i < Q; i++)//Responding to requests the     { the          while(que[i].q >= edge[cnt].val && cnt < m)//Kruskal Algorithm the         { +             intFX =Mfind (edge[cnt].u); -             intFY =Mfind (EDGE[CNT].V); the             if(FX! =fy)Bayi             { theQue[i].ans + = Sum[fx]*sum[fy];//New Transfer Array theFM[FY] = FX;//Collection Merge -SUM[FX] + =Sum[fy]; -             } thecnt++; the         } the         if(I >0) Que[i].ans + = que[i-1].ans;//contains an existing transfer array the     } - } the  the voidoutput () the {94Sort (que, que+q, cmp2);//Sort by Request order the      for(inti =0; i < Q; i++) printf ("%d\n", Que[i].ans); the } the 98 intMain () About { -     //freopen ("Test.txt", "R", stdin);101      while(~SCANF ("%d%d%d", &n, &m, &q))102     {103 init ();104 Work (); the output ();106     }107     return 0;108}

HDU 3938 Portal (and check set + offline +kruskal)

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.