AhmedAshraff

Untitled

May 12th, 2025
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define boAshraf ios_base::sync_with_stdio(false); cin.tie(NULL);
  4. #define ll long long
  5. #define sz(s) (int)(s).size()
  6. #define all(s) (s).begin(),(s).end()
  7. using namespace std;
  8.  
  9. void File();
  10.  
  11. void sol();
  12.  
  13. const int N = 1e6;
  14. int lpf[N + 1];
  15. int freq[N + 1];
  16. int val[(int)2e5+1];
  17. vector<int> prime;
  18.  
  19. void sieve() {
  20. for (int i = 2; i <= N; i++) {
  21. if (lpf[i] == 0) {
  22. lpf[i] = i;
  23. prime.push_back(i);
  24. }
  25. for (int j: prime) {
  26. if (j > lpf[i] || 1LL * i * j > N)break;
  27. lpf[i * j] = j;
  28. }
  29. }
  30. }
  31.  
  32. vector<vector<int>> adj;
  33. vector<vector<int> *> nodes;
  34. vector<int> cnt;
  35. int tot=0,ans=0;
  36. void add(int u) {
  37. int x=val[u];
  38. while(x>1){
  39. int c=lpf[x];
  40. if(!freq[c]++)tot++;
  41. while(x%c==0)x/=c;
  42. }
  43.  
  44. }
  45.  
  46. void del(int u) {
  47. int x=val[u];
  48. while(x>1){
  49. int c=lpf[x];
  50. if(!--freq[c])tot--;
  51. while(x%c==0)x/=c;
  52. }
  53. }
  54.  
  55. void dfsSize(int u = 1, int p = -1) {
  56. cnt[u] = 1;
  57. for (int v: adj[u]) {
  58. if (v == p) continue;
  59. dfsSize(v, u);
  60. cnt[u] += cnt[v];
  61. }
  62. }
  63.  
  64. void dfs(int u, int p, bool keep = 1) {
  65. int max_size = 0, bigChild = -1;
  66. for (int v: adj[u]) {
  67. if (v == p || cnt[v] <= max_size) continue;
  68. max_size = cnt[v];
  69. bigChild = v;
  70. }
  71. for (int v: adj[u]) {
  72. if (v == p || v == bigChild) continue;
  73. dfs(v, u, 0);
  74. }
  75. if (bigChild == -1) {
  76. nodes[u] = new vector<int>();
  77. } else {
  78. dfs(bigChild, u, 1);
  79. nodes[u] = nodes[bigChild];
  80. }
  81. nodes[u]->push_back(u);
  82. add(u);
  83. for (int v: adj[u]) {
  84. if (v == p || v == bigChild) continue;
  85. for (int ch: *nodes[v]) {
  86. add(ch);
  87. nodes[u]->push_back(ch);
  88. }
  89. }
  90. ans+=tot&1;
  91. if (keep == 0) {
  92. for (int v: *nodes[u]) {
  93. del(v);
  94. }
  95. }
  96. }
  97.  
  98. void init(int n, int root = 1) {
  99. nodes = vector<vector<int> *>(n + 1);
  100. cnt = vector<int>(n + 1);
  101. dfsSize(root, -1);
  102. dfs(1,0);
  103. }
  104.  
  105.  
  106. int main() {
  107. boAshraf
  108. File();
  109. int t = 1;
  110. // cin >> t;
  111. sieve();
  112. while (t--) {
  113. sol();
  114. }
  115. return 0;
  116. }
  117.  
  118. void sol() {
  119. int n;
  120. cin>>n;
  121. for(int i=1;i<=n;i++)cin>>val[i];
  122. adj.resize(n+1);
  123. for(int i=1;i<n;i++){
  124. int u,v;cin>>u>>v;
  125. adj[u].emplace_back(v);
  126. adj[v].emplace_back(u);
  127. }
  128. init(n);
  129. cout<<ans;
  130. }
  131.  
  132. void File() {
  133. #ifndef ONLINE_JUDGE
  134. freopen("input.txt", "r", stdin);
  135. freopen("output.txt", "w", stdout);
  136. #endif
  137. }
  138.  
Advertisement
Add Comment
Please, Sign In to add comment