[# 2] algorithm with disjoint-set problems related to graph theory

Recently disjoint-set and graph theory problems related to card a few times, write a little bit better.

Block?Cushions, next time do it

Because many examples Luo Gu inside is not, so the problem face to talk about.

A template problem is UVA1395 , very typical problem surfaces have not given.

Why use disjoint-set it? Determine whether each point will be used when China Unicom, China Unicom then how to ensure a minimum difference of it?

First, we put side by side the right sort, then we enumerate the number smaller side, about two connectivity, then we enumerate the larger side of small to large, and China Unicom around the edges of the block, until all points Unicom, then the minus side of the small side that is the minimum difference. (In addition, since the difference between the maximum and minimum values, even if the two sides have also ...... China Unicom) is not good, but it is still better to run the minimum spanning tree, as if all the points Unicom can not easily be detected.

Then running side Kruskal good, very bare board title. Record first time to forget the file input and output zero burst ......

This title is a purple book above there, behind the two questions is no.

One problem is set to go through and re-check to avoid double-counting algorithm optimized violence (simply do not see is the same algorithm ah) and uses off-line algorithm.

    In the distant future, Earth is invaded alien civilization, in this moment of crisis, humans showed unprecedented unity, in order to allow more people to live, coalition government decided that all the scientists on the United Earth, the transformation of those who have been together a coalition government carefully selected mankind, so that they become Superman has super powers, then sent them to destroy alien life on this planet. This brilliant and great program, named '' save the planet '' program.

    Fortunately, you have been selected and successfully been transformed into a superhuman, each mission, the coalition government will send you through the portal to a national unity government designated, and your task is: Do you want to eliminate as much as possible from this countries began to expand out of alien creatures that have saved as many countries.

    N countries on earth, there is no bar m to the road, only one alien (extraterrestrial life on Earth are all on the road, there is no alien country) on each road, each alien creature there is a defense value, then you have q missions, each mission makes the coalition government's attack power you have w (each task is to attack has been the same), then you will be sent to the national portal x (national x you have been saved), your mission is to save as many countries. (If there is a road between a country and the country b, a country you have been saved, and your defensive attack value greater than or equal value on this road alien, then you can save the country b, otherwise you can not pass this road save the country b)

    The number of countries for each mission, the most you can save the output of each task independent of each other

Enter a description:

Enter the first line of the three positive integers n, m, q, respectively, the number of countries on the planet, the number of roads, as well as the number of tasks you

Next m lines of three integers x, y, w, x and expressed between countries to countries without a y road, and only one alien monster on the road, its defense value w, guaranteed Defensive It is a positive integer

Next q lines of two integers x, w, represent each mission you will be sent to countries x, and let you have the attack power of w, each task independent of each other

Output Description:

Q total number of countries output lines, each a positive integer representing each mission you can save up to

For 20% of the data n-<= \ (10 ^ {. 3} \)     m <= \ (2 \ times10 ^ {. 3} \)    Q <= \ (10 ^ {. 3} \) $ W <= \ (10 9 ^ {} \)

For the other 20% of data n-<= \ (^ {10}. 5 \)     m <= \ (2 \ times10. 5 ^ {} \)    Q <= \ (^ {10}. 5 \)     W <= 100

For still further 20% of the data n-<= \ (10 ^ {. 5} \)    m <= \ (2 \ times10 ^ {. 5} \)   Q <= \ (10 ^ {. 5} \)     W <= \ ( 10 ^ {9} \)     to ensure that each of the input task are the same x

For the remaining 40% of the data n-<= \ (10 ^ {. 5} \)   m <= \ (2 \ times10 ^ {. 5} \)   Q <= \ (10 ^ {. 5} \)     W <= \ (10 9 ^ {} \)

Every task independent of each other, said:

Every country in the task of rescue or other tasks need to be saved, every task to destroy alien among other tasks or will exist again.

A glance down the violence, directly for each given point running while dfs get away. Hi mention twenty.

Then we found the place for smaller x x can be reached if the larger one can reach into the local absolute all to achieve.

So we off-line algorithm, maintaining the size of each block Unicom, the query, and then sorted by size x sorted side by side right edge traversal, if the left and right sides of the point is not inside the same block, and the updating blocks of the block merger size, if x is less than the current inquiry current edge weight of the edge, and the jump to the next sub-query answer to the inquiry update interrogation starting point where the block size is different.

Code

In this piece we began to climb the long, cherry blossoms in full bloom along the ramp.

In one hill, n pavilion, which is connected to the n pavilion, and m is bidirectionally walking trails. Each trail sides are planted with two rows of cherry trees, however, since the light, and the difference in frequency soil care, aesthetic level is not the same for each trail, the trail is connected to the i th and the pavilion the pavilion ai bi, ci for the aesthetic appearance .

Now we want to go from No. 1 pavilion pavilion number n, and because they do not want to view appear much difference, we hope difference between the maximum and minimum values ​​of aesthetics through the trails as small as possible. Please find this minimum.

Enter a description:

The first line of two integers n, m, and represents the number of pavilion trail.
Next m lines of three integers ai, bi, ci, representative of the ends of the pavilion number and aesthetics this trail trail.

Output Description:

Total an integer representing the optimal solution, the minimum difference between the maximum value and the minimum value through the aesthetics of the trail.

Data range:
For 30% of the data, there are \ (n-\ leq10, m \ Leq 20 is \) ;

For 60% of the data, there are \ (n-\ leq100, m \ leq200 \) ;

To 100% of the data, there are \ (n-\ Leq 2000, m \ Leq 4000,1 \ Leq a_i, B_i \ n-Leq,. 1 \ Leq C_i \ Leq. 5 ^ 10 \) ;

FIG guarantee is given in FIG communication, but there may be multiple edges or loopback.

The title and the first question is similar, except that the data range is slightly larger a little, did not (you can use Dijkstra water to 80 minutes, I later wrote a DP feeling is right, but only 70 points.)

First, sort the right side

Status \ ((i, j) \ ) represents the point \ (I \) a large number of side \ (J \) maximum side is small. Maintenance of all possible smallest difference for each point of the array with a size of n.

State transition, then, first find the smallest difference between the current point and then enumerate the largest edge, and then traverse the edge even at this point, update the other point ......

\[dp[dis[id][j].v][max(i,dis[id][j].id)]=max(min(dp[id][i],dis[id][j].id),dp[dis[id][j].v][max(i,dis[id][j].id)]);\]

// dis means side, to the target point comprises a v, a w is the right side, an id number, id is the current point number, j is the number of current edge.

If there is no problem but only 70 points, and difficult indeed, catching insects bad catch.

Guess you like

Origin www.cnblogs.com/Schwarzkopf-Henkal/p/11830117.html