跳到正文

个人记录

数据生成器

YxYe约 2 分钟
原文
文章信息与版本

文章信息

发布
2023年10月26日
收录
2025年12月12日
更新
2026年11月15日
篇幅
约 2 分钟

版本

  1. v2当前
  2. v1

树

C++
#include<cstdlib>
#include<iostream>
#include<ctime>
#include<cstdio>
#include<algorithm>
#define re register
using namespace std;
int n,q,qx,qy,w;
int x[10010],y[10010],z[10010];
int a[10010],fa[10010];
int cnt2,cnt;
struct Edge{
	int u,v,w;
}e[10010];
int find(int x)
{
	return fa[x]==x?x:find(fa[x]);
}
int flag;
int main()
{
	srand(time(0));
	n=rand()%10+1;
	printf("%d\n",n);
	for(re int i=1;i<=n;++i)
	{
		for(re int j=i+1;j<=n;++j)
		{
			x[++cnt]=i;
			y[cnt]=j;
			z[cnt]=rand()%100+1;
		}
	}
	for(re int i=1;i<=cnt;++i) a[i]=i,fa[i]=i;
	random_shuffle(a+1,a+cnt+1);
	for(re int i=1;i<=cnt;++i)
	{
		int pos=a[i];
		int eu=find(x[pos]),ev=find(y[pos]);
		if(eu==ev) continue;
		fa[ev]=eu;
		e[++cnt2].u=x[pos],e[cnt2].v=y[pos],e[cnt2].w=z[pos];
		if(cnt2==n-1) break;
	}
	for(re int i=1;i<=cnt2;++i)
	{
		printf("%d %d %d\n",e[i].u,e[i].v,e[i].w);
	}

	return 0;
}
原文