import os, shutil
from random import *
import random
seed("omg")

class TestGen:
	def __init__(self):
		self.p = [0]*1000
		self.test_number = 0
	
	def print_test(self, N, K, EDGES,):
		self.test_number += 1
		test_name = "../tests/" + "{0:0=2d}".format(self.test_number)
		test_file = open(test_name, "w")
		print(N, K, file=test_file)
		print(''.join(map(lambda s : s + "\n", (' '.join(str(element) for element in triple) for triple in EDGES))), file=test_file, end="")
		test_file.close()	
	def get(self, v):
		if (self.p[v] == v):
			return v
		self.p[v] = self.get(self.p[v])
		return self.p[v]
	
	def Union(self, a, b):
		a = self.get(a)
		b = self.get(b)
		self.p[b] = a
	
	def gen_tree(self, N):
		for i in range(1, N + 1):
			self.p[i] = i
		A = []
		for i in range(0, N - 1):
			a = random.randint(1, N)
			b = random.randint(1, N)
			while (self.get(a) == self.get(b)):
				a = random.randint(1, N)
				b = random.randint(1, N)
			A.append((a, b))
			self.Union(a, b)	 
		return A
	def gen_test(self):
		self.print_test(5, 3, self.gen_tree(5))
		self.print_test(5, 2, self.gen_tree(5))
		for i in range(5):
			n = random.randint(5, 10)
			self.print_test(n, random.randint(2, n//2), self.gen_tree(n))	
		for i in range(5):
			n = random.randint(20, 50)
			self.print_test(n, random.randint(2, n//2), self.gen_tree(n))
		
		for i in range(5):
			n = random.randint(20, 50)
			self.print_test(n, random.randint(2, n // 3), self.gen_tree(n))
		
		for i in range(5):
			n = random.randint(50, 100)
			self.print_test(n, random.randint(2, n), self.gen_tree(n))
		for i in range(5):
			n = random.randint(50, 100)
			self.print_test(n, random.randint(2, n), self.gen_tree(n))
		
		for i in range(5):
			n = random.randint(50, 100)
			self.print_test(n, random.randint(2, 10), self.gen_tree(n))
		
		for i in range(5):
			n = random.randint(50, 100)
			self.print_test(n, random.randint(2, 5), self.gen_tree(n))
		
		for i in range(5):
			n = random.randint(200, 250)
			self.print_test(n, random.randint(2, 10), self.gen_tree(n))
		
		for i in range(5):
			n = random.randint(200, 250)
			self.print_test(n, random.randint(2, 5), self.gen_tree(n))
		
		for i in range(5):
			n = random.randint(200, 250)
			self.print_test(n, random.randint(2, n // 3), self.gen_tree(n))
		
		for i in range(5):
			n = random.randint(90, 100)
			self.print_test(n, random.randint(10, n // 3), self.gen_tree(n))
		
		self.print_test(250, 250, self.gen_tree(250))
		A = []
		N = 250
		for i in range(1, N):
			A.append((i, i + 1))
		self.print_test(N, N, A)
		self.print_test(N, N // 2, A)
		self.print_test(N, 2, A)
		self.print_test(N, 3, A)
		self.print_test(N, N // 2 + 1, A)
	
					

									
if os.path.exists('../tests'):
	shutil.rmtree('../tests', ignore_errors=False, onerror=None)
try:
	os.makedirs('../tests')
except OSError:
	pass

writer = TestGen()
writer.gen_test()
