-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path132.cpp
More file actions
53 lines (47 loc) · 867 Bytes
/
Copy path132.cpp
File metadata and controls
53 lines (47 loc) · 867 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n;
vector<ll> arr(100001);
vector<bool> red(100001,false);
vector<vector<int>> tree(100001);
bool isAiming(ll n)
{
ll root = sqrt(n);
if(root*root == n) return true;
else return false;
}
int dfs(int u,int parent)
{
int count = 0;
for(int v:tree[u])
{
if(v==parent)continue;
count += dfs(v,u);
if(!red[u]&&!red[v]&&isAiming(arr[u]*arr[v]))
{
red[u] = true;
red[v] = true;
count += 2;
}
}
return count;
}
int main()
{
cin>>n;
for(int i = 1;i<=n;i++)
{
cin>>arr[i];
}
int u,v;
for(int i = 1;i<n;i++)
{
cin>>u>>v;
tree[u].push_back(v);
tree[v].push_back(u);
}
int res = dfs(1,-1);
cout<<res<<endl;
return 0;
}