mitkonikov

Pointers

Jan 3rd, 2023
909
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.47 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define ll long long
  3.  
  4. using namespace std;
  5.  
  6. vector<ll> sizes;
  7.  
  8. struct file {
  9.     ll sum = 0;
  10.     string name;
  11.     vector<file*> files;
  12.     file* parent = nullptr;
  13.     ll size = 0;
  14.     void create(string name, int size = 0) {
  15.         file* new_dir = new file({ size, name });
  16.         new_dir->parent = this;
  17.         files.push_back(new_dir);
  18.  
  19.         file pointer = *new_dir; // ogromna memorija u funkcija
  20.         file* pointer = new_dir; // pointer nakaj toj file
  21.  
  22.         pointer->create();
  23.         (*pointer).create();
  24.     }
  25.     file* moveToDir(string name) {
  26.         for (int i = 0; i < files.size(); i++) {
  27.             if (files[i]->name == name) {
  28.                 return files[i];
  29.             }
  30.         }
  31.         return nullptr;
  32.     }
  33.     ll dfs() {
  34.         ll count = 0;
  35.         ll child_size = 0;
  36.         for (int i = 0; i < files.size(); i++) {
  37.             count += files[i]->dfs();
  38.             child_size += files[i]->size;
  39.         }
  40.  
  41.         size = child_size + sum;
  42.         if (files.size()) sizes.push_back(size);
  43.         return (count + (files.size() > 0 && size <= 100000 ? size : 0));
  44.     }
  45. };
  46.  
  47. // EXAMPLE
  48. // -------------------------
  49. vector<vector<int>> adj; //
  50. vector<int> values; // { 3, 5, 1, 2, 5, 7 .... }
  51.  
  52. int dfs(int u) {
  53.     if (values[u] == 1) return u;
  54.  
  55.     for (auto v: adj) {
  56.         int result = dfs(v);
  57.         if (result != -1) {
  58.             return result;
  59.         }
  60.     }
  61.  
  62.     return -1;
  63. }
  64.  
  65. struct node {
  66.     vector<node*> childs;
  67.     int value = 0;
  68.  
  69.     node* dfs() {
  70.         if (value == 1) return this;
  71.  
  72.         for (auto child: childs) {
  73.             node* result = child->dfs();
  74.             if (result != nullptr) {
  75.                 return result;
  76.             }
  77.         }
  78.  
  79.         return nullptr;
  80.     }
  81. };
  82.  
  83. /// -----------------------
  84.  
  85. int main() {
  86.     int n;
  87.     cin >> n;
  88.     file* root = new file({ 0, "/" });
  89.     root->parent = root;
  90.     file* current = root;
  91.     bool read_ls = false;
  92.     for (int i = 0; i < n; i++) {
  93.         string what;
  94.         cin >> what;
  95.         if (what == "$") {
  96.             if (read_ls) {
  97.                 read_ls = false;
  98.             }
  99.  
  100.             string command;
  101.             cin >> command;
  102.             if (command == "cd") {
  103.                 string where;
  104.                 cin >> where;
  105.                 if (where == "/") {
  106.                     current = root;
  107.                 } else if (where == "..") {
  108.                     current = current->parent;
  109.                 } else {
  110.                     current = current->moveToDir(where);
  111.                     assert(current != nullptr);
  112.                 }
  113.             } else if (command == "ls") {
  114.                 read_ls = true;
  115.             } else {
  116.                 cout << "Unknown command at line " << i + 1 << endl;
  117.                 return 0;
  118.             }
  119.         } else if (read_ls) {
  120.             if (what == "dir") {
  121.                 string name;
  122.                 cin >> name;
  123.                 current->create(name);
  124.             } else {
  125.                 string name;
  126.                 cin >> name;
  127.                 current->create(name, stoll(what));
  128.             }
  129.         }
  130.     }
  131.     cout << "Count : " << root->dfs() << endl;
  132.     sort(sizes.begin(), sizes.end());
  133.     for (int i = 0; i < sizes.size(); i++) {
  134.         if (70000000 - root->size + sizes[i] >= 30000000) {
  135.             cout << "Delete: " << sizes[i] << endl;
  136.             return 0;
  137.         }
  138.     }
  139.     return 0;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment