fork download
  1. #include<bits/stdc++.h>
  2.  
  3. typedef long long int ll;
  4. typedef long double ld;
  5.  
  6. #define fast ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
  7. #define endl '\n'
  8. #define ff first
  9. #define ss second
  10. #define pb push_back
  11. #define mp make_pair
  12. #define pii pair<int,int>
  13. #define full(a) a.begin(),a.end()
  14. #define mem(a,x) memset(a,x,sizeof(a))
  15.  
  16. const int MAXN = 1e5+5;
  17. const int MOD = 1e9+7;
  18.  
  19. using namespace std;
  20.  
  21. ll value[MAXN];
  22. ll goal[MAXN];
  23. vector<int> v[MAXN];
  24. int n;
  25. int ans;
  26.  
  27. void dfs(int root, int par, int height , vector<int> parity) {
  28.  
  29. int c = 0;
  30.  
  31. if((parity[height%2] == 0) && (value[root]!=goal[root])) {
  32. c = 1;
  33. }
  34. else if((parity[height%2] == 1) && (value[root]==goal[root])) {
  35. c = 1;
  36. }
  37.  
  38. if(c == 1) {
  39. parity[height%2] = (parity[height%2] + 1)%2;
  40. ans+=c;
  41. }
  42.  
  43. for(auto next : v[root]) {
  44. if(next != par) {
  45. dfs(next , root , height + 1 , parity) ;
  46. }
  47. }
  48. }
  49.  
  50. int main() {
  51. fast;
  52. cin >> n;
  53. for(int i=1;i<=n-1;i++) {
  54. int x,y;
  55. cin >> x >> y;
  56. v[x].pb(y);
  57. v[y].pb(x);
  58. }
  59. for(int i=1;i<=n;i++) cin >> value[i];
  60. for(int i=1;i<=n;i++) cin >> goal[i];
  61. vector<int> parity = {0,0};
  62. dfs(1 , 1 , 1 , parity);
  63. cout << ans << endl;
  64.  
  65.  
  66. }
Success #stdin #stdout 0s 5416KB
stdin
3
1 2
2 3
1 0 1
0 1 0
stdout
2