首页 > 其他分享 >构造随机树

构造随机树

时间:2023-09-29 13:55:49浏览次数:30  
标签:std randi int long 随机 构造

#include <bits/stdc++.h>

using namespace std;

#define int long long

const int N = 1000001; 		// N = 点数 + 1 

int n, p[N];

struct Edge
{
	int a, b;
}e[N];

int randi(int P)
{
	int res = (((rand() << 15ll) | rand()));
	return res % P + 1;
}

void CreateTree()
{
	n = randi(N - 1);
	for (int i = 1; i <= n; ++ i ) p[i] = i;
	random_shuffle(p + 1, p + n + 1);
	
	for (int i = 2; i <= n; ++ i )
		e[i - 1] = {p[randi(i - 1)], p[i]}; 
	
	random_shuffle(e + 1, e + n);
}

main()
{
	srand(time(0));
	
	CreateTree();
	
	cout << n << '\n';
	
	for (int i = 1; i < n; ++ i ) cout << e[i].a << ' ' << e[i].b << '\n';
	
	return 0;
}

标签:std,randi,int,long,随机,构造
From: https://www.cnblogs.com/2huk/p/17736951.html

相关文章