Advertisement
Tooster

treegen.cpp

Dec 1st, 2016
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.64 KB | None | 0 0
  1. #include <algorithm>
  2. #include <utility>
  3. #include <functional>
  4. #include <vector>
  5. #include <utility>
  6. #include <random>
  7. #include <iostream>
  8.  
  9. using namespace std;
  10.  
  11. //-----Tooster-----
  12.  
  13. int n, maxChildCount, firstVertex;
  14. int main(int argc, char *argv[]) {
  15.     ios_base::sync_with_stdio(false);
  16.     if (argc == 1) {
  17.         int a;
  18.         cin >> a >> n >> maxChildCount >> firstVertex;
  19.         srand(atoi("treegen") + a);
  20.     }
  21.     else if (argc == 5) {
  22.         srand(atoi(argv[0]) + atoi(argv[1]));
  23.         n = atoi(argv[2]);
  24.         maxChildCount = atoi(argv[3]);
  25.         firstVertex = atoi(argv[4]);
  26.     }
  27.     else {
  28.         cerr << "Usage: <randomSeed> <vertices> <maxChildCount> <firstVertex>" << endl;
  29.         cerr << "   randomSeed     - seed for random number generator, e.g. test number" << endl;
  30.         cerr << "   vertices       - number of vertices" << endl;
  31.         cerr << "   maxChildCount  - maximum number of edges connected to one vertex" << endl;
  32.         cerr << "   firstVertex    - vertices enumeration start" << endl;
  33.         cerr << endl;
  34.         cerr << " output format: N pairs {A, B} : A is connected with bidirectional edge with B" << endl;
  35.         return 1;
  36.     }
  37.  
  38.     vector<vector <int> > V(n);
  39.     vector<int> hashTable(n);
  40.     for (int i = 0; i < n; i++) hashTable[i] = i;
  41.     random_shuffle(hashTable.begin(), hashTable.end());
  42.  
  43.     int v = 1;
  44.     while (v < n) {
  45.         int u = rand() % v;
  46.         int children = rand() % maxChildCount + 1;
  47.         while (v < n && children > 0 && V[u].size() < maxChildCount - 1) {
  48.             V[u].push_back(v++);
  49.             children--;
  50.         }
  51.     }
  52.  
  53.     for (int i = 0; i < V.size(); i++)
  54.         for (int j = 0; j < V[i].size(); j++)
  55.             cout << hashTable[i] + firstVertex << " " << hashTable[V[i][j]] + firstVertex << endl;
  56.  
  57.     return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement