Advertisement
bluetastymelon

subarbore2

Feb 24th, 2020
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.55 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. ifstream f("subarbore2.in");
  7. ofstream g("subarbore2.out");
  8.  
  9. int t[101],c[101],viz[101],sum,n,m,k;
  10.  
  11. void citire()
  12. {
  13. f>>n>>k;
  14. for(int i=1;i<=n;i++)
  15. f>>t[i];
  16. for(int i=1;i<=n;i++)
  17. f>>c[i];
  18. }
  19.  
  20. void DFS(int x)
  21. {
  22. viz[x]=1;
  23. for(int i=1;i<=n;i++)
  24. if(t[i]==x and viz[i]==0)
  25. DFS(i);
  26. }
  27.  
  28. int main()
  29. {
  30. citire();
  31. DFS(k);
  32. for(int i=1;i<=n;i++)
  33. if(viz[i])
  34. sum+=c[i];
  35. g<<sum;
  36. return 0;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement