华为OD机试-目录删除

// Online C++ compiler to run C++ program online
#include
#include
#include
#include
#include
#include
#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;

map> folder_map;

void delFload(int id) {
    // 无子节点
    if(folder_map[id].size()) {
        for (int i = 0; i < folder_map[id].size(); i++) {
            delFload(folder_map[id][i]);
        }
    }
    folder_map.erase(id);
}

int main() {
    // 输入
    int m;
    cin >> m;

    //构造父子关系
    for (int i = 0; i < m; i++) {
        int a, b;
        cin >> a >> b;

        if (folder_map.find(a) == folder_map.end()) {
            folder_map[a] = {};
        }

        if (folder_map.find(b) != folder_map.end()) {
            folder_map[b].push_back(a);
        } else {
            folder_map[b] = { a };
        }
    }

    // 删元素
    int d;
    cin >> d;
    delFload(d);

    for (auto x : folder_map) {
        if (x.first == 0)
            continue;
        cout << x.first << " ";
    }

    return 0;
}

你可能感兴趣的:(c++,算法,开发语言)