The meaning of the question is unknown .. Is it the longest path or a minimal spanning tree?
I only know this data
4 3
1 2 1
1 3 1
1 4 3
4 is obtained by the longest path, and 5 is obtained by the smallest spanning tree... the two are too many...
However, I also learned some things this time, that is, the size of the given stack is very small and there are no 10 ^ 5. So if I want to use DFS to traverse the tree, I need to add the stack space myself.
# Pragma comment (linker, "/Stack: 102400000,102400000") // This is a good thing
The other is better...
Longest pathCode
# Pragma Comment (linker, "/Stack: 102400000,102400000 ") # Include <Stdio. h> # Include < String . H> # Include <Iostream>Using Namespace STD; # Define N 100100 # Define M 10001000 Int N, m; Int Bin [N]; Int CNT, pre [N]; Int Mark [N]; Int MX; Struct Node { Int To, next, W;} edge [m]; Void Add_edge ( Int X, Int Y, Int W) {edge [CNT]. = Y; edge [CNT]. W = W; edge [CNT]. Next = Pre [X]; Pre [x] = CNT ++ ;} Int Find ( Int X ){ Int S = X; While (Bin [s]! = S) S = Bin [s]; While (Bin [x]! = S ){ Int TMP = Bin [X]; bin [x] = S; x = TMP ;} Return S ;} Int DFS (Int S ){ Int FI = 0 , Se = 0 ; Mark [s] = 1 ; For ( Int P = pre [s]; P! =- 1 ; P = Edge [p]. Next ){ Int V = Edge [p].; If (MARK [v] = 0 ){ Int TMP = DFS (v) + Edge [p]. W; If (TMP> FI) {fi = TMP ;} Else { If (TMP> Se) Se = TMP ;}}} If (FI + Se> MX) MX = Fi +Se; Return FI ;} Int Main (){ While (Scanf ( " % D " , & N, & M )! = EOF) {CNT = 0 ; MX =- 1 ; Memset (PRE, - 1 ,Sizeof (Pre); memset (mark, 0 , Sizeof (Mark )); For ( Int I = 0 ; I <n; I ++ ) Bin [I] = I; Int Flag = 0 ; For (Int I = 0 ; I <m; I ++ ){ Int X, Y, key; scanf ( " % D " , & X, & Y ,& Key ); Int A = 0 , B = 1 ; = Find (x); B =Find (y); bin [A] = B; If (A = B) {flag = 1 ;} Add_edge (X, Y, key); add_edge (Y, X, key );} If (Flag = 1 ) {Printf ( " Yes \ n " ); Continue ;} For ( Int I = 1 ; I <= N; I ++ ){ If (MARK [I] = 0 ){ Int TMP; TMP = DFS (I) ;}} printf ( " % D \ n " , MX );} Return 0 ;}