Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- const int maxn = 50005;
- vector<int> adj[maxn];
- int valores[maxn],passei[maxn],cont=0;
- bool achei = false;
- void dfs(int u,int p,int valor){
- if(achei) return;
- for(int v : adj[u]){
- if(valores[v]==valor and v!=p){
- passei[v]=1;
- achei=true;
- break;
- }
- if(v!=p){
- dfs(v,u,valor);
- }
- }
- if(achei) ++cont;
- }
- int main(int argc, char const *argv[])
- {
- /*ios_base::sync_with_stdio(false);
- cin.tie(NULL);*/
- int n;
- scanf("%d",&n);
- for(int i=0;i<n;i++){
- scanf("%d",&valores[i]);
- }
- for(int i=0;i<n-1;i++){
- int a,b;
- scanf("%d %d",&a,&b);
- --a;--b;
- adj[a].push_back(b);
- adj[b].push_back(a);
- }
- int res=0;
- for(int i=0;i<n;i++){
- if(!passei[i]){
- passei[i]=1;
- dfs(i,-1,valores[i]);
- if(achei) res+=cont;
- }
- achei=false;
- cont=0;
- }
- printf("%d\n",res);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement