当前位置: 首页 > 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.gsyq.cn/news/29278.html

相关文章:

  • smartproxy API 代理—构建一体化可观测与可回滚体系 - Smart
  • cout 输出流控制
  • sersync for docker 实时同步
  • 动态开点线段树模板
  • 博弈2
  • sg
  • 解决复制 Ubuntu Server 虚拟机后网络不通的问题(IP冲突问题)
  • postgresql查询数据sql无法使用到索引
  • 自动机
  • 标注工具--抹除目标
  • 【数据挖掘】基于随机森林回归模型的二手车价格预测分析(信息集+源码)
  • Z函数(扩展 KMP)
  • 常用例题
  • 实验报告3
  • 2025年环评公司权威推荐排行榜,环评手续,环评报告,环评验收,专业高效服务助力企业合规发展
  • Seata用法
  • Day3多媒体标签——视频与音频
  • 提交一张 PPT,参与 RTE2025 全球语音智能体云展示
  • 完整教程:深入解析AppCrawler:开源自动遍历测试工具配置指南
  • 解释 EIP-4337
  • 材料包含与下载漏洞
  • 完整教程:Elasticsearch面试精讲 Day 23:安全认证与权限控制
  • 求解连续数字的正约数集合——倍数法
  • 欧拉筛(线性筛)
  • 常见数列
  • Markdown数学公式 - -一叶知秋
  • 最小割
  • 查询GPIO状态值(步骤)
  • 欧拉路径/欧拉回路 Hierholzers
  • 无源汇点的最小割问题 Stoer–Wagner