Advertisement
Guest User

Untitled

a guest
Mar 28th, 2020
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define stergere copii[parinti[init][0]].erase(copii[parinti[init][0]].begin());
  3.  
  4.  
  5. using namespace std;
  6.  
  7. vector< vector <int> > copii,parinti;
  8.  
  9. vector<int> val,visited,inc;
  10.  
  11. stack<int> st;
  12.  
  13. int root,max1,n,x,t,c,s,nr;
  14.  
  15. void DFS(int st)
  16. {
  17. for(int i=0; i<copii[st].size() ;i++)
  18. if(visited[copii[st][i]]==0)
  19. {
  20. visited[copii[st][i]]=visited[st]+1;
  21. max1=max(max1,visited[copii[st][i]]);
  22. DFS(copii[st][i]);
  23. }
  24. }
  25.  
  26. void DFSSPEC(int start)
  27. {
  28. if(copii[copii[start][0]].size()==0)
  29. {
  30. st.push(start);
  31. s+=val[start];
  32. }
  33. else
  34. {
  35. s+=val[copii[start][0]];
  36. DFSSPEC(copii[start][0]);
  37. }
  38.  
  39. }
  40.  
  41. void parc(int init,vector< vector <int> > copii,vector< vector <int> > parinti)
  42. {
  43. int s=val[init];
  44. if(!parinti[init].empty())
  45. st.push(parinti[init][0]);
  46. stergere;
  47. while(!st.empty())
  48. {
  49. if(copii[st.top()].size())
  50. DFSSPEC(st.top());
  51. int cap=st.top();
  52. while(!copii[cap].empty())
  53. {
  54. s+=copii[cap][0];
  55. if(s%3==0)
  56. nr++;
  57. s-=copii[cap][0];
  58. copii[cap].erase(copii[cap].begin());
  59. }
  60. if(s%3==0)
  61. nr++;
  62. s-=cap;
  63. st.pop();
  64. if(parinti[parinti[cap][0]].size())
  65. copii[parinti[cap][0]].erase(copii[parinti[cap][0]].begin());
  66. }
  67. }
  68.  
  69. void fct()
  70. {
  71. for(int i=1;i<=n;i++)
  72. if(parinti[i].empty())
  73. root=i;
  74. visited.resize(n+1);
  75. visited[root]=1;
  76. DFS(root);
  77. for(int i=1;i<=n;i++)
  78. if(visited[i]==max1)
  79. inc.push_back(i);
  80. for(int i=0;i<inc.size();i++)
  81. parc(inc[i],copii,parinti);
  82. }
  83.  
  84. int main()
  85. {
  86. cin>>n;
  87. copii.resize(n+1);
  88. parinti.resize(n+1);
  89. val.push_back(0);
  90. for(int i=1;i<=n;i++)
  91. cin>>x,val.push_back(x);
  92. for(int i=1;i<n;i++)
  93. {
  94. cin>>t>>c;
  95. copii[t].push_back(c);
  96. parinti[c].push_back(t);
  97. }
  98. fct();
  99. cout<<nr;
  100. return 0;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement