生成一课n个节点的树。
n表示点数,通过并查集判断无环
#include <ctime>
#include <iostream>
#include<random>
using namespace std;
const int N = 1e6 + 10, INF = 1e9;
int a[N];
int p[N];
vector<int> edge[N];
int find(int x)
{
if(p[x] != x)
return p[x] = find(p[x]);
return p[x];
}
int main() {
mt19937 rnd(time(0));
freopen("5.in","w",stdout);
int n= 1000;
for(int i = 1; i <= n; i ++)
p[i] = i;
printf("%d\n", n);
for(int i=1;i <= n; i ++){
a[i]= rnd()% INF + 1;
printf("%d ", a[i]);
}
printf("\n");
int cnt = 0;
while(cnt < n - 1)
{
int u = rnd() % n + 1;
int v = rnd() % n + 1;
int fu = find(u);
int fv = find(v);
if(fu != fv)
{
cnt ++;
p[fu] = fv;
edge[u].push_back(v);
edge[v].push_back(u);
printf("%d %d\n", u, v);
}
}
}