[黑科技]造一组树的数据

本文介绍了通过随机生成数据来构造树形结构的方法,主要思路是随意生成数据,确保数组满足构建树的条件,同时插入一些特定数据以确保树的正确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

思路就是xjb乱搞,随便跑数据,只要数组\color{red}思路就是xjb乱搞,随便跑数据,只要数组xjb,便,

够用就一直随机数据,当然也要可以的搞几个让数据\color{orange}够用就一直随机数据,当然也要可以的搞几个让数据,

一定成立的数据,然后跑一遍生成树滑稽\color{green}一定成立的数据,然后跑一遍生成树 滑稽,

#include <bits/stdc++.h>
#pragma GCC optimize(5)
#define ha 10000
#define maxn 20000000
using namespace std ; 
int n , m ,tot ;
int fa[maxn] ;
int find(int x) {
	if(fa[x] != x) fa[x] = find(fa[x]) ;
	return fa[x] ;
}
void unionn(int x ,int y) {
	x = find(x) , y = find(y) ;
	fa[x] = y ;
}
struct dy{
	int x ;int y ;int z ;
}a[maxn] ;
int t ;
void add(int x,int y ,int z) {
	a[++t].x = x ;
	a[t].y = y ;
	a[t].z = z ;
}
int main () {
	srand(time(0)) ;
	cin >> n ;
	freopen("d.in","w",stdout) ;
	cout << n << endl ;
	for(int i = 1 ; i <= n ; i ++) {
		fa[i] = i ;
	}
	for(int i = 1 ; i <= n ; i ++) {
		int x = (rand()+ha)%n , y = (rand()+ha)%n , z = rand()%ha ;
		if(x == 0) x = n ;if(y == 0) y = n ;if(z == 0) z = ha ;
		add(x,y,z) ;
		tot ++ ;
	}
	for(int i = 1 ; i <= n ; i ++) {
		int x = (rand()+ha)%n+1 , y = rand()%n+1 , z = rand()%ha +1;
		add(x,y,z) ;
		tot ++ ;
	}
	for(int i = 1 ; i <= n ; i ++) {
		int x = (rand()+n)%n+1 , y = (rand()+n)%n+1 , z = rand()%ha +1;
		add(x,y,z) ;
		tot ++ ;
	}
	for(int i = 1 ; i <= n ; i ++) {
		int x = (rand()+n)%n+1 , y = (rand()+n)%n+1 , z = rand()%ha +1;
		add(x,y,z) ;
		tot ++ ;
	}
	for(int i = 1 ; i <= n ; i ++) {
		int x = (rand()+n)%n+1 , y = (rand()+n)%n+1 , z = rand()%ha +1;
		add(x,y,z) ;
		tot ++ ;
	}
	for(int i = 1 ; i <= n ; i ++) {
		int x = (rand()+n)%n+1 , y = (rand()+n)%n+1 , z = rand()%ha +1;
		add(x,y,z) ;
		tot ++ ;
	}
	for(int i = 1 ; i <= n ; i ++) {
		int x = (rand()+ha)%n+1 , y = (rand()+n)%n+1 , z = rand()%ha +1;
		add(x,y,z) ;
		tot ++ ;
	}
	for(int i = 1 ; i <= n ; i ++) {
		int x = (rand()+ha)%n+1 , y = (rand()+n)%n+1 , z = rand()%ha +1;
		add(x,y,z) ;
		tot ++ ;
	}
	for(int i = 1 ; i <= n ; i ++) {
		int x = (rand()+ha)%n+1 , y = (rand()+n)%n+1 , z = rand()%ha +1;
		add(x,y,z) ;
		tot ++ ;
	}
	for(int i = 1 ; i <= n ; i ++) {
		int x = rand()%n+1 , y = rand()%n +1, z = rand()%ha +1;
		add(x , y , z );
		tot ++ ;
	}
	for(int i = 1 ; i <= n ; i ++) {
		int opt = rand()%2 ;
		if(opt == 1) 
		add(i,(rand()+ha)%n,rand()%ha) ;
		else add((rand()+ha)%n,i,rand()%ha) ;
		tot ++ ;
	}
	for(int i = 1 ; i <= n ; i ++) {
		int x = rand()%n +1, y = rand()%n+1 , z = rand()%ha +1;
		add(x , y , z );
		tot ++ ;
	}
	for(int i = 1 ; i <= n ; i ++) {
		int opt = rand()%2 ;
		if(opt == 1) 
		add(i,(rand()+ha)%n,rand()%ha) ;
		else add((rand()+ha)%n,i,rand()%ha) ;
		tot ++ ;
	}
//	for(int i = 1 ; i <= n ; i ++) {
//		add(i ,(i +2)%n+1 ,rand() % ha+1 );
//		tot ++ ;
//	}
	int k = 0 ;
	for(int i = 1 ; i <= t ; i ++) {
		if(find(a[i].x) != find(a[i].y)) {
			unionn(a[i].x,a[i].y) ;
			k ++ ;
			cout << a[i].x << " " << a[i].y << " " <<a[i].z <<endl ;;
		}if(k == n-1) break ;
	}
	cout << k << endl ;
	return 0 ;	
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值