Advertisement
Josif_tepe

Untitled

Jul 1st, 2023
876
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.03 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. //#include<bits/stdc++.h>
  4. using namespace std;
  5. const int maxn = 3e5 + 10;
  6. vector<int> graph[maxn];
  7. int salary[maxn];
  8. int parent[maxn];
  9. int n, B;
  10. int cnt;
  11. void dfs(int node) {
  12.     if(node == 0) {
  13.         return;
  14.     }
  15.     for(int i = 0; i < (int) graph[node].size(); i++) {
  16.         int neighbour = graph[node][i];
  17.         if(neighbour == parent[node]) {
  18.             if(salary[node] > salary[neighbour]) {
  19.                 cnt++;
  20.                 salary[neighbour] = salary[node];
  21.                 dfs(neighbour);
  22.             }
  23.         }
  24.     }
  25. }
  26. int main() {
  27.     ios_base::sync_with_stdio(false);
  28.     cin >> n >> B;
  29.     salary[0] = B;
  30.     for(int i = 1; i <= n; i++) {
  31.         int r, p;
  32.         cin >> salary[i] >> p;
  33.         parent[i] = p;
  34.         graph[i].push_back(p);
  35.         graph[p].push_back(i);
  36.    
  37.     }
  38.     for(int i = 1; i <= n; i++) {
  39.         cnt = 0;
  40.         dfs(i);
  41.         cout << cnt << "\n";
  42.     }
  43.    return 0;
  44. }
  45. /*
  46.  7
  47.  1 2
  48.  1 3
  49.  2 4
  50.  2 5
  51.  3 6
  52.  3 7
  53.  **/
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement