Advertisement
Guest User

Untitled

a guest
May 24th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.34 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <vector>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. int label;
  10. int* d;
  11. int n;
  12. int result = 0;
  13. int waiting = 0;
  14. vector<int> arr[222222];
  15. vector<int> ans[222222];
  16. void dfs(int vertex, vector<int>* arr) {
  17.     d[vertex - 1] = 0;
  18.     if (arr[vertex - 1].size() > 0) {
  19.         for (auto i = arr[vertex - 1].begin(); i != arr[vertex - 1].end(); ++i) {
  20.             d[vertex - 1]++;
  21.             dfs(*i, arr);
  22.             d[vertex - 1] += d[*i - 1];
  23.         }
  24.     }
  25. }
  26.  
  27. vector<int> main_dfs(int vertex, vector<int>* array, int ind) {
  28.     vector<vector<int>> V;
  29.     if (array[vertex - 1].size() > 0) {
  30.         for (int i = 0; i < array[vertex - 1].size(); ++i) {
  31.             V.push_back(main_dfs(array[vertex - 1][i], array, ind - 1));
  32.         }
  33.     }
  34.     else {
  35.         vector<int> labels(n);
  36.         labels[ind] = vertex;
  37.         V.push_back(labels);
  38.     }
  39.     sort(V.begin(), V.end());
  40.     vector<int> res(n);
  41.     int I = ind;
  42.     for (int i = V.size() - 1; i >= 0;i--) {
  43.         for (int j = V[i].size() - 1; j >= 0;j--) {
  44.             if (V[i][j] != 0)
  45.                 res[I--]=V[i][j];
  46.         }
  47.     }
  48.  
  49.     return res;
  50. }
  51.  
  52. bool comp(const int& a, const int& b) {
  53.     if (d[a - 1] < d[b - 1]) {
  54.         return true;
  55.     }
  56.     else if (d[a - 1] == d[b - 1] && a > b) {
  57.         return true;
  58.     }
  59.     else return false;
  60. }
  61.  
  62. int main() {
  63.     ifstream in("input.txt");
  64.     int  root;
  65.     in >> n >> root;
  66.     string str = "";
  67.     int index = 0, amount = 0;
  68.     getline(in, str);
  69.     while (!in.eof()) {
  70.         getline(in, str);
  71.         str += ":";
  72.         int ind1, ind2;
  73.         ind1 = str.find(":");
  74.         int index = atoi(str.substr(0, ind1).c_str());
  75.         ind2 = str.find(":", ind1 + 1);
  76.         int amount = atoi(str.substr(ind1 + 1, ind2).c_str());
  77.         ind1 = ind2;
  78.         ind2 = str.find(":", ind1 + 1);
  79.         while (ind2 != string::npos) {
  80.             arr[index - 1].push_back(atoi(str.substr(ind1 + 1, ind2).c_str()));
  81.             ind1 = ind2;
  82.             ind2 = str.find(":", ind1 + 1);
  83.         }
  84.     }
  85.     in.close();
  86.     label = n;
  87.     vector<int> labels(n);
  88.     d = new int[n];
  89.     dfs(root, arr);
  90.     for (int i = 0; i < n; i++) {
  91.         sort(arr[i].begin(), arr[i].end(), comp);
  92.     }
  93.     vector<int> answer = main_dfs(root, arr, n - 1);
  94.     for (int i = 0; i < n; i++) {
  95.         result += waiting;
  96.         waiting -= arr[labels[i] - 1].size();
  97.         waiting++;
  98.     }
  99.     ofstream out("output.txt");
  100.     out << result << endl;
  101.     for (int i = 0; i < n - 1; i++) {
  102.         out << labels[i] << " ";
  103.     }
  104.     out << labels[n - 1];
  105.     out.close();
  106.     //system("pause");
  107.     return 0;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement