跳到正文

剪贴板

剪贴板 0678558n

原文
剪贴板信息

剪贴板信息

收录
更新
C++
#include <bits/stdc++.h>

#define liyixi_i_is_lovely liyixi_i_love_haimian
#define x first
#define y second

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;

namespace generator{
	const int N = 1e6 + 5;
	// mt19937 生成随机 int 型整数  
	mt19937 mrand(time(0));
	// 均匀生成 [l,r] 的整数  
	int random(int l, int r)
	{
		uniform_int_distribution<int> u(l, r);
		return u(mrand);
	}
	// 生成 long long 型整数 
	LL llmrand()
	{
		return (LL)mrand() << 32 | mrand();
	}
	// 生成长度为 n 的随机数组,值在区间 [l,r] 中 
	void random_array(int n, int a[], int l, int r)
	{
		uniform_int_distribution<int> u(l, r);
		for(int i = 1; i <= n; i ++ ) a[i] = u(mrand);
	}
	// 生成长度为 n 的随机排列  
	void random_permutation(int n, int a[])
	{
		for(int i = 1; i <= n; i ++ ) a[i] = i;
		shuffle(a + 1, a + n + 1, mrand);
	}
	// 生成长度为 n 的数组,使和为 m 且较均匀  
	void random_split(int n, int a[], int m)
	{
		uniform_int_distribution<int> u(1, n);
		for(int i = 1; i <= n; i ++ ) a[i] = 0;
		for(int i = 1; i <= m; i ++ ) a[u(mrand)] ++ ;
	}
	// 生成随机区间  
	PII random_interval(int n)
	{
		int x = random(0, n), y = random(1, n);
		if(!x) return make_pair(y, y);
		else
		{
			if(x > y) swap(x, y);
			return make_pair(x, y);
		}
	}
	// 生成一棵树 
	// shape: 0 随机生成 
	// 		  1 较高 
	// 		  2 链 
	// 		  3 菊花 
	// 		  4 完全二叉树 
	// 		  5 几乎链 
	// 		  6 几乎菊花 
	// 		  7 几乎完全二叉树 
	// 		  8 菊花挂链 
	// 		  9 卡满重链剖分 
	void random_tree(int n, PII e[], int shape)
	{
		if(shape == 1)
		{
			int k = random(10, 100);
			for(int i = 1; i < n; i ++ )
			{
				e[i].x = i + 1;
				e[i].y = random(max(1, i - k), i);
			}
		}
		else if(shape == 2)
		{
			for(int i = 1; i < n; i ++ )
			{
				e[i].x = i;
				e[i].y = i + 1;
			}
		}
		else if(shape == 3)
		{
			for(int i = 1; i < n; i ++ )
			{
				e[i].x = 1;
				e[i].y = i + 1;
			}
		}
		else if(shape == 4)
		{
			for(int i = 2; i <= n; i ++ )
			{
				e[i - 1].x = i / 2;
				e[i - 1].y = i;
			}
		}
		else if(shape == 5)
		{
			for(int i = 1; i < n - 100; i ++ )
			{
				int t = random(0, 10000);
				if(t)
				{
					e[i].x = i;
					e[i].y = i + 1;
				}
				else
				{
					e[i].x = random(1, i);
					e[i].y = i + 1;
				}
			}
			for(int i = n - 100; i < n; i ++ ) e[i].x = random(1, i), e[i].y = i + 1;
		}
		else if(shape == 6)
		{
			for(int i = 1; i < n - 100; i ++ )
			{
				int t = random(0, 10000);
				if(t)
				{
					e[i].x = 1;
					e[i].y = i + 1;
				}
				else
				{
					e[i].x = random(1, i);
					e[i].y = i + 1;
				}
			}
		}
		else if(shape == 7)
		{
			for(int i = 2; i <= n - 100; i ++ )
			{
				int t = random(0, 10000);
				if(t)
				{
					e[i - 1].x = i / 2;
					e[i - 1].y = i;
				}
				else
				{
					e[i - 1].x = random(1, i);
					e[i - 1].y = i + 1;
				}
			}
			for(int i = n - 100; i < n; i ++ ) e[i].x = random(1, i), e[i].y = i + 1;
		}
		else if(shape == 8)
		{
			
		}
		else if(shape == 9)
		{
			
		}
		else
		{
			for(int i = 1; i < n; i ++ )
			{
				e[i].x = i + 1;
				e[i].y = random(1, i);
			}
		}
		static int a[N];
		random_permutation(n, a);
		for(int i = 1; i < n; i ++ )
		{
			e[i].x = a[e[i].x];
			e[i].y = a[e[i].y];
			if(mrand() % 2) swap(e[i].x, e[i].y);
		}
		shuffle(e + 1, e + n, mrand);
	}
	// 生成一张 n 个点 m 条边的连通图,shape 是生成树的参数,rep_edge 表示是否有重边,self_loop 表示是否有自环  
	void random_connected_graph(int n, int m, PII e[], int shape, bool rep_edge, bool self_loop)
	{
		random_tree(n, e, shape);
		if(rep_edge)
		{
			int k = m - 1 - self_loop;
			for(int i = n; i <= k; i ++ )
			{
				int x, y;
				if(self_loop)
				{
					x = random(1, n);
					y = random(1, n);
				}
				else
				{
					x = random(1, n);
					y = random(1, n - 1);
					if(y >= x) y ++ ;
				}
				e[i].x = x, e[i].y = y;
			}
			if(self_loop) e[m - 1].x = e[m - 1].y = random(1, n);
			e[m] = e[random(1, m - 1)];
			if(mrand() % 2) swap(e[m].x, e[m].y);
		}
		else
		{
			unordered_set<LL> s;
			int k = m - self_loop;
			for(int i = 1; i < n; i ++ )
			{
				LL t = 1LL * e[i].x * n + e[i].y;
				s.insert(t);
				t = 1LL * e[i].y * n + e[i].x;
				s.insert(t);
			}
			for(int i = n; i <= k; i ++ )
			{
				int x, y;
				if(self_loop)
				{
					x = random(1, n);
					y = random(1, n);
				}
				else
				{
					x = random(1, n);
					y = random(1, n - 1);
					if(y >= x) y ++ ;
				}
				LL t = 1LL * x * n + y;
				if(!s.count(t))
				{
					e[i].x = x, e[i].y = y;
					s.insert(t);
					t = 1LL * y * n + x;
					s.insert(t);
				}
				else i -- ;
			}
			if(self_loop) e[m].x = e[m].y = random(1, n);
		}
		shuffle(e + 1, e + m + 1, mrand);
	}
};