Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. ifstream fin("apm.in");
  6. ofstream fout("apm.out");
  7.  
  8. const int nmax = 260;
  9. struct el
  10. {
  11. int nod, cost;
  12. }tt[nmax];
  13.  
  14. struct muchie
  15. {
  16. int x, y, c;
  17. }mc[nmax*nmax];
  18.  
  19. int n, m;
  20. vector <el> g[nmax];
  21. int cost[nmax];
  22. int mat[nmax][nmax];
  23.  
  24. inline void read()
  25. {
  26. fin >> n;
  27. for (int i = 1; i <= n-1; i++)
  28. {
  29. int a, b, c;
  30. fin >> a >> b >> c;
  31. mat[a][b] = mat[b][a] = 1;
  32. g[a].push_back({b, c});
  33. g[b].push_back({a, c});
  34. }
  35. int x;
  36. while (fin >> x)
  37. {
  38. cost[++m] = x;
  39. }
  40. }
  41.  
  42. inline void dfs(int nod,int stop, int tata)
  43. {
  44. if (nod == stop)
  45. return;
  46. for (auto it : g[nod])
  47. {
  48. int vecin = it.nod;
  49. int c = it.cost;
  50. if (vecin == tata)
  51. continue;
  52. tt[vecin].nod = nod;
  53. tt[vecin].cost = c;
  54. dfs(vecin, stop, nod);
  55. }
  56. }
  57.  
  58. inline int caut(int i, int j)
  59. {
  60. int maxim = 0;
  61. while (i != j)
  62. maxim = max(maxim, tt[j].cost), j = tt[j].nod;
  63. return maxim+1;
  64. }
  65.  
  66. int cnt;
  67.  
  68. inline void getgraph()
  69. {
  70. for (int i = 1; i < n; i++)
  71. {
  72. for (int j = i+1; j <= n; j++)
  73. {
  74. //gasim maximul de pe ruta (i, j)
  75. if (mat[i][j])
  76. continue;
  77. dfs(i, j, -1);
  78. mc[++cnt] = {i, j, caut(i, j)};
  79. }
  80. }
  81. for (int i = 1; i <= cnt; i++)
  82. cout << mc[i].x << " " << mc[i].y << " " << mc[i].c << "\n";
  83. }
  84.  
  85. int main()
  86. {
  87. read();
  88. getgraph();
  89. return 0;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement