Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

HackerEarth Directory Deletion problem solution

YASH PAL, 31 July 2024
In this HackerEarth Directory Deletion problem solution, You are given a directory tree of N directories/folders. Each directory is represented by a particular id which ranges from 1 to N. The id of the root directory is 1, then it has some child directories, those directories may contain some other ones and it goes on. Now you are given a list of directories id’s to delete, you need to find the minimum number of directories that need to be deleted so that all the directories in the given list get deleted.
HackerEarth Directory Deletion problem solution

HackerEarth Directory Deletion problem solution.

#include<bits/stdc++.h>
#define LL long long int
#define M 1000000007
#define endl "n"
#define eps 0.00000001
LL pow(LL a,LL b,LL m){ a%=m;LL x=1,y=a;while(b > 0){if(b%2 == 1){x=(x*y);if(x>m) x%=m;}y = (y*y);if(y>m) y%=m;b /= 2;}return x%m;}
LL gcd(LL a,LL b){if(b==0) return a; else return gcd(b,a%b);}
LL gen(LL start,LL end){LL diff = end-start;LL temp = rand()%start;return temp+diff;}
using namespace std;
vector<int> graph[100001];
bool to_delete[100001];
int p[100001];
int ans = 0;
void dfs(int node) {
if(to_delete[node] == 1) {
++ans;
return;
}
for(int i: graph[node]) {
dfs(i);
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
assert(cin >> n);
assert(n >= 1 && n <= 100000);
for(int i = 1; i <= n; i++) {
assert(cin >> p[i]);
assert(p[i] <= n);
if(p[i] != -1)
graph[p[i]].push_back(i);
}
int k;
assert(cin >> k);
for(int i = 1; i <= k; i++) {
int val;
assert(cin >> val);
assert(val <= n && val >= 1);
to_delete[val] = 1;
}
dfs(1);
cout << ans;
}
coding problems solutions

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
©2025 Programming101 | WordPress Theme by SuperbThemes