Description
As the popularity of the West Lake in Hangzhou is further improved, the garden planning expert Yao hopes to design a new classic sightseeing line. according to the instructions of MA xiaoteng, the new landscape line is best built into a ring, if there is no condition to build a ring, the longer the building, the better.
Now N locations have been determined for construction, and m routes can be designed and their lengths have been determined for exploration between them. Can we build a circular landscape? If not, what is the maximum value of a landscape line?
The routes that can be built are bidirectional, And the length between them is greater than 0.
Input
There are multiple groups of test data. The first row of each group of test data has two numbers, N and M. For more information, see the topic description;
Next, the M line has three numbers u v w, representing the start point, end point, and length of the Line.
[Technical Specification]
1. n <= 100000
2. m <= 1000000
3. 1 <= u, v <= N
4. W <= 1000
Output
For each group of test data, if a ring can be built (not all scenic spots need to be connected), yes is output; otherwise, the maximum length is output and one row is output for each group of data.
Sample Input
3 31 2 12 3 13 1 1
Sample output
Yes idea: When re reaches the point of vomiting blood, HDU actually has a stack initialization: # pragma comment (linker, "/Stack: 1024000000,1024000000"), which is sprayed out of old blood, first Judge the ring: and query the set, then the tree-like DP# Include <iostream> # include <cstdio> # include <cstring> # include <algorithm> # include <vector> # pragma comment (linker, "/Stack: 10241000000,1024000000 ") using namespace STD; const int maxn = 100005; struct edge {int V, F; edge () {} edge (int v, int f) {This-> V = V; this-> F = f ;}}; vector <edge> ve [maxn]; int n, m, ans; int Fa [maxn], DP [maxn]; int find (int x) {If (X! = Fa [x]) Fa [x] = find (Fa [x]); Return Fa [X];} void DFS (int p, int U) {int max = 0; For (INT I = 0; I <ve [u]. size (); I ++) {int v = ve [u] [I]. v, F = ve [u] [I]. f; If (V = P) continue; DFS (u, v); ans = max (ANS, DP [v] + F + max ); // max = max (max, DP [v] + F);} DP [u] = max;} void solve () {for (INT I = 1; I <= N; I ++) if (DP [I] =-1) DFS (0, I ); printf ("% d \ n", ANS);} int main () {int A, B, C, X, Y; while (scanf (" % D ", & N, & M )! = EOF) {ans = 0; int flag = 0; For (INT I = 1; I <= N; I ++) {DP [I] =-1; fa [I] = I; ve [I]. clear () ;}for (INT I = 0; I <m; I ++) {scanf ("% d", & A, & B, & C); If (FLAG) continue; X = find (A), Y = find (B); If (X! = Y) Fa [x] = y; else flag = 1; ve [A]. push_back (edge (B, c); ve [B]. push_back (edge (a, c);} If (FLAG) {printf ("Yes \ n"); continue;} solve ();} return 0 ;}
HDU-4514 cutting-edge series-Design Landscape