Advertisement
andreisophie

mere

Jan 23rd, 2020
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.58 KB | None | 0 0
  1. #include <fstream>
  2.  
  3. using namespace std;
  4.  
  5.  
  6. ifstream fin("mere.in");
  7. ofstream fout("mere.out");
  8.  
  9. int a[1001][1001],v[10001],n,viz[1001],s;
  10.  
  11. void citire()
  12. {
  13.     fin>>n;
  14.     for(int i=1;i<=n;i++)
  15.         fin>>v[i];
  16.     int x,y;
  17.     while(fin>>x>>y)
  18.         a[x][y]=a[y][x]=1;
  19. }
  20.  
  21.  
  22. void dfs(int x)
  23. {
  24.     viz[x]=1;
  25.     int pozMax=0;
  26.     s+=v[x];
  27.     for(int i=1;i<=n;i++)
  28.         if(a[x][i]==1 && !viz[i] && v[pozMax]<v[i])
  29.             pozMax=i;
  30.     if(pozMax!=0)
  31.         dfs(pozMax);
  32. }
  33.  
  34. int main()
  35. {
  36.     citire();
  37.     dfs(1);
  38.     fout<<s;
  39.     return 0;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement