当前位置: 首页 > news >正文

随机数生成与样例构造

随机数生成与样例构造

mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
int r(int a, int b) {return rnd() % (b - a + 1) + a;
}void graph(int n, int root = -1, int m = -1) {vector<pair<int, int>> t;for (int i = 1; i < n; i++) { // 先建立一棵以0为根节点的树t.emplace_back(i, r(0, i - 1));}vector<pair<int, int>> edge;set<pair<int, int>> uni;if (root == -1) root = r(0, n - 1); // 确定根节点for (auto [x, y] : t) { // 偏移建树x = (x + root) % n + 1;y = (y + root) % n + 1;edge.emplace_back(x, y);uni.emplace(x, y);}if (m != -1) { // 如果是图,则在树的基础上继续加边for (int i = n; i <= m; i++) {while (true) {int x = r(1, n), y = r(1, n);if (x == y) continue; // 拒绝自环if (uni.count({x, y})) continue; // 拒绝重边edge.emplace_back(x, y);uni.emplace(x, y);}}}random_shuffle(edge.begin(), edge.end()); // 打乱节点for (auto [x, y] : edge) {cout << x << " " << y << endl;}
}
http://www.jsqmd.com/news/21249/

相关文章:

  • 都是
  • 单机集群部署(redis6.2.20)
  • 快读
  • int128 输入输出流控制
  • 高精度快速幂
  • smartproxy API 代理—构建一体化可观测与可回滚体系 - Smart
  • 我爱学算法之—— 模拟(下) - 教程
  • cout 输出流控制
  • 完整教程:Torch-Rechub学习笔记-task3
  • 日期换算(基姆拉尔森公式)
  • sersync for docker 实时同步
  • 最长严格/非严格递增子序列 (LIS)
  • 【Python爬虫】反爬虫入门与基础(一) - 教程
  • sg
  • 解决复制 Ubuntu Server 虚拟机后网络不通的问题(IP冲突问题)
  • postgresql查询数据sql无法使用到索引
  • 博弈1
  • Day3综合案例一:个人简介
  • 后缀数组 SA
  • 自动机
  • 1024程序员节福利!参与互动,5分钟赢好礼!
  • 马拉车
  • 具身智能/智能体 定义
  • 【数据挖掘】基于随机森林回归模型的二手车价格预测分析(信息集+源码)
  • 实用指南:flink批处理-水位线
  • 字符串模式匹配算法 KMP
  • Z函数(扩展 KMP)
  • 常用例题
  • 实验报告3
  • 2025年环评公司权威推荐排行榜,环评手续,环评报告,环评验收,专业高效服务助力企业合规发展