Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.87 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4. const int maxn = 50005;
  5.  
  6. vector<int> adj[maxn];
  7. int valores[maxn],passei[maxn],cont=0;
  8. bool achei = false;
  9. void dfs(int u,int p,int valor){
  10.     if(achei) return;
  11.     for(int v : adj[u]){
  12.         if(valores[v]==valor and v!=p){
  13.             passei[v]=1;
  14.             achei=true;
  15.             break;
  16.         }  
  17.         if(v!=p){
  18.             dfs(v,u,valor);
  19.         }
  20.     }
  21.     if(achei) ++cont;
  22. }
  23. int main(int argc, char const *argv[])
  24. {
  25.     /*ios_base::sync_with_stdio(false);
  26.     cin.tie(NULL);*/
  27.     int n;
  28.     scanf("%d",&n);
  29.     for(int i=0;i<n;i++){
  30.         scanf("%d",&valores[i]);
  31.     }
  32.     for(int i=0;i<n-1;i++){
  33.         int a,b;
  34.         scanf("%d %d",&a,&b);
  35.         --a;--b;
  36.         adj[a].push_back(b);
  37.         adj[b].push_back(a);
  38.     }
  39.     int res=0;
  40.     for(int i=0;i<n;i++){
  41.         if(!passei[i]){
  42.             passei[i]=1;
  43.             dfs(i,-1,valores[i]);
  44.             if(achei) res+=cont;
  45.         }
  46.         achei=false;
  47.         cont=0;
  48.     }
  49.     printf("%d\n",res);
  50.     return 0;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement