c++14 下隨機生成資料
阿新 • • 發佈:2018-11-11
c++14 下隨機生成 序列 區間 樹 圖等資料
class RandomData { private: static const int MAXN = 1e6 + 10; public: RandomData() { srand((unsigned)time(0)); } int random(int n) { return (long long)rand() * rand() % n; } int random(int l, int r) { return random(r - l + 1) + l; } auto random_vector(int n, int val) { vector<int>vec; for(int i = 1; i <= n; i++ ) { vec.push_back(random(n) + val); } return vec; } auto random_interval(int n, int l, int r) { vector<pair<int, int> >vec; for(int i = 1; i <= n; i++ ) { int L = random(l, r); int R = random(l, r); if(L > R) { swap(L, R); } vec.push_back(make_pair(L, R)); } return vec; } auto random_tree(int n, int m, int l = 1, int r = 1) { vector<tuple<int, int, int> >vec; //id father_id value if(n <= 2) { vec.push_back(make_tuple(1, 0, 1)); return vec; } for(int i = 2; i <= n; i++ ) { int fa = random(i - 1) + 1; int val = random(l, r); vec.push_back(make_tuple(i, fa, val)); } return vec; } auto random_graph(int n, int m, int l = 1, int r = 1) { bool open = 1; // 1 is direct graph and 0 is not vector<tuple<int, int, int> >vec = random_tree(n, m, l, r); map< pair<int, int>, bool > has; for(unsigned i = 0; i < vec.size(); i++ ) { int u = get<0>(vec[i]); int v = get<1>(vec[i]); has[ make_pair(u, v) ] = true; if(open == 0) { has[ make_pair(v, u) ] = true; } } for(int i = n; i <= m; i++ ) { int x, y; do { x = random(n) + 1, y = random(y) + 1; } while(x == y || has[ make_pair(x, y) ]); int val = random(l, r); vec.push_back(make_tuple(x, y, val)); has[ make_pair(x, y) ] = true; if(open) { has[ make_pair(y, x) ] = true; } } random_shuffle(vec.begin(), vec.end()); return vec; } } random_data;