a53

arborelantmaxim

a53
Nov 18th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define N 100001
  3. using namespace std;
  4. ifstream fin("arborelantmaxim.in");
  5. ofstream fout("arborelantmaxim.out");
  6. int n, Maxim, sol;
  7. int Up[N], Down1[N], Down2[N], Sol[N];
  8. bool Use[N];
  9. vector < pair <int , int> >G[N];
  10. void citire()
  11. {
  12. fin >> n;
  13. for(int i=1; i<=n-1; i++)
  14. {
  15. int x, y, c;
  16. fin >> x >> y >> c;
  17. G[x].push_back(make_pair(y,c));
  18. G[y].push_back(make_pair(x,c));
  19. }
  20. }
  21.  
  22. void DFS1(int Nod)
  23. {
  24. Use[Nod]=1;
  25. for(int i=0; i<(int)G[Nod].size(); ++i)
  26. {
  27. int Vecin = G[Nod][i].first;
  28. int Cost = G[Nod][i].second;
  29. if(!Use[Vecin])
  30. {
  31. DFS1(Vecin);
  32. int Drum = Down1[Vecin] + Cost;
  33. if(Drum >= Down1[Nod])
  34. {
  35. Down2[Nod] = Down1[Nod];
  36. Down1[Nod] = Drum;
  37. }
  38. else
  39. if(Drum > Down2[Nod])
  40. Down2[Nod]=Drum;
  41. }
  42.  
  43. }
  44. }
  45. void DFS2(int Nod)
  46. {
  47. Use[Nod]=1;
  48. for(int i=0; i<(int)G[Nod].size(); ++i)
  49. {
  50. int Vecin = G[Nod][i].first;
  51. int Cost = G[Nod][i].second;
  52. if(!Use[Vecin])
  53. {
  54. Up[Vecin] = Up[Nod] + Cost;
  55. if(Down1[Nod] == (Down1[Vecin] + Cost))
  56. Up[Vecin] = max(Up[Vecin],Down2[Nod] + Cost);
  57. else
  58. Up[Vecin] = max(Up[Vecin],Down1[Nod] + Cost);
  59. DFS2(Vecin);
  60. }
  61. }
  62. Sol[Nod] = max(Up[Nod],Down1[Nod]);
  63. }
  64. int main()
  65. {
  66. citire();
  67. DFS1(1);
  68. memset(Use,0,sizeof(Use));
  69. DFS2(1);
  70. for(int i=1; i<=n; i++)
  71. if(Sol[i]>Maxim)
  72. Maxim=Sol[i];
  73. fout<<Maxim;
  74. return 0;
  75. }
Add Comment
Please, Sign In to add comment