Advertisement
Saleh127

CF 902B.cpp

Sep 23rd, 2020
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. vector<int>edge[10001];
  4. int ans=0;
  5. int v[10001];
  6. int color[10001];
  7.  
  8. void dfs(int x)
  9. {
  10. if(v[x]==1) return;
  11.  
  12. v[x]=1;
  13. for(auto z:edge[x])
  14. {
  15. if(!v[z])
  16. {
  17. if(color[z]!=color[x])
  18. {
  19. ans++;
  20. }
  21. dfs(z);
  22. }
  23. }
  24. }
  25.  
  26.  
  27. int main()
  28. {
  29. ios_base::sync_with_stdio(0);
  30. cin.tie(0);cout.tie(0);
  31.  
  32. int n,i,j;
  33. cin>>n;
  34.  
  35. for(i=2;i<=n;i++)
  36. {
  37. int k;
  38. cin>>k;
  39. edge[i].push_back(k);
  40. edge[k].push_back(i);
  41. }
  42. for(i=1;i<=n;i++)
  43. {
  44. cin>>color[i];
  45. }
  46.  
  47. memset(v,0,sizeof(v));
  48.  
  49. dfs(1);
  50.  
  51. cout<<ans+1<<endl;
  52. return 0;
  53. }
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement