Advertisement
nicuvlad76

Untitled

Dec 17th, 2020
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. //Complexitate O(N^2) timp si memorie
  2. //Metoda: Greedy pe arbore
  3.  
  4. #include <fstream>
  5. using namespace std;
  6.  
  7. const int N = 1005;
  8.  
  9. ifstream fin ("mere.in");
  10. ofstream fout ("mere.out");
  11.  
  12. int n, v[N], sum;
  13. bool a[N][N];
  14.  
  15. void dfs(int x, int father) {
  16. sum += v[x];
  17. int poz = 0;
  18. for (int i = 1; i <= n; ++i)
  19. if (i != father && a[x][i] && v[poz] < v[i])
  20. poz = i;
  21. if (poz)
  22. dfs(poz, x);
  23. }
  24.  
  25. int main() {
  26. fin >> n;
  27. for (int i = 1; i <= n; ++i)
  28. fin >> v[i];
  29. for (int i = 0, x, y; i < n; ++i) {
  30. fin >> x >> y;
  31. a[x][y] = a[y][x] = 1;
  32. }
  33. dfs(1, -1);
  34. fout << sum;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement