前言
代码
#include <bits/stdc++.h>
using namespace std;
int fa[100005],f[100005];
int find(int x){return fa[x]==x?x:fa[x]=find(fa[x]);}
int main()
{
srand(time(0));
int n=10;//节点个数,自行更改
for(int i=1;i<=n;i++)fa[i]=i;
int root=rand()%n+1;
cout<<n<<endl;
for(int i=1;i<=n;i++)
{
if(i==root) continue;
int father;
do{
father=rand()%n+1;
}while(find(i)==find(father));
f[i]=father;
fa[find(i)]=find(father);
}
for(int i=1;i<=n;i++)
{
if(root==i) continue;
cout<<i<<' '<<f[i]<<' '<<rand()%100+1<<endl;//相连的两个节点及边权
}
}