Advertisement
binh27112004

Untitled

Apr 5th, 2020
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.78 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4.  
  5. int main(){
  6.     int n,req;
  7.     cin >> n >> req;
  8.     vector <int> graph[n];
  9.     int val[n];
  10.     int a;
  11.     for(int i=0;i<n;i++){
  12.         cin >> val[i];
  13.     }
  14.     int b;
  15.     for(int i=1;i<n;i++){
  16.         cin >> b;
  17.         graph[b-1].push_back(i);
  18.     }
  19.     int count=0;
  20.     for(int i=0;i<n;i++){
  21.         int value = val[i];
  22.         int sum = req - value;
  23.         vector <int> children;
  24.         for(int j=0;j<graph[i].size();j++){
  25.             children.push_back(graph[i][j]);
  26.         }
  27.         for(int j=0;j<children.size();j++){
  28.             for(int k=0;k<graph[children[j]].size();k++)
  29.                 children.push_back(graph[children[j]][k]);
  30.         }
  31.  
  32.         for(int j=0;j<children.size();j++){
  33.             for(int k=j+1;k<children.size();k++)
  34.                 if(val[children[j]]+val[children[k]]>=sum){
  35.                     count++;
  36.                 }
  37.         }
  38.     }
  39.  
  40.     cout << count << endl;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement