Read problems statements [Hindi] , [Vietnamese] , [Mandarin Chinese] , [Russian] and [Bengali] as well.

You are given a directed acyclic graph with $N$ vertices (numbered $1$ through $N$) and $M$ edges. You want to destroy it all! To destroy the graph, you should delete all of its vertices. 

In one step, you are allowed to delete at most two vertices; you may only delete a vertex when there is no edge from it to another (not yet deleted) vertex. You may not delete two vertices when there is an edge between them either.

Find a way to destroy the graph in the minimum number of steps.

------  Input ------
The first line of the input contains two space-separated integers $N$ and $M$.
Each of the next $M$ lines contains two space-separated integers $U$ and $V$ denoting an edge from vertex $U$ to vertex $V$.

------  Output ------
The first line of the output should contain a single integer $S$ denoting the minimum number of steps needed to destroy the graph.
Each of the next $S$ lines should describe one step; it should contain an integer $K$ ($1 ≤ K ≤ 2$), followed by a space and $K$ space-separated integers denoting the numbers of vertices deleted in this step.

------  Constraints  ------
$1 ≤ N, M ≤ 10^{6}$
the graph on the input does not contain any cycles or self-loops

------  Scoring ------
The score for each test file is $10 \cdot (C/S)^2$, where $C$ is the number of steps in which you deleted exactly $2$ vertices. The final score of your submission is equal to the sum of scores for each test file.

There are twenty test cases. During the contest, the displayed score will account for exactly four test files, i.e. your score reflects your submission's performance on 20% (4/20) of the test files. However, if your program gets a non-AC verdict on any test file, your submission's verdict will be non-AC. In other words, an AC verdict denotes that your program runs successfully on all the test files. After the end of the contest, your score will be changed to include the sum of your program's scores over the other sixteen test files.

------  Example Input ------

6 6
1 2
2 3
1 3
3 4
3 5
1 6

------  Example Output ------

4
2 4 5
2 6 3
1 2
1 1

Difficulty: VERY_HARD

Solutions:
(n, m) = map(int, input().split())
ind = [[] for i in range(n)]
out = [0] * n
for i in range(m):
	e = list(map(int, input().split()))
	e[0] = e[0] - 1
	e[1] = e[1] - 1
	out[e[0]] += 1
	ind[e[1]].append(e)
q = []
for i in range(n):
	if out[i] == 0:
		q.append(i)
ans = []
while q:
	a = q.pop(0)
	a1 = ind[a]
	if q:
		b = q.pop(0)
		b1 = ind[b]
		for elem in b1:
			out[elem[0]] = out[elem[0]] - 1
		for elem in b1:
			if out[elem[0]] == 0:
				q.append(elem[0])
		ans.append([2, a + 1, b + 1])
	else:
		ans.append([1, a + 1])
	for elem in a1:
		out[elem[0]] -= 1
	for elem in a1:
		if out[elem[0]] == 0:
			q.append(elem[0])
print(len(ans))
for i in ans:
	print(*i)


import queue
[n, m] = list(map(int, input().split()))
adjlist = {}
adjlist2 = {}
outlist = {}
step = 0
res = []
l = queue.Queue(maxsize=n)
for i in range(1, n + 1):
	adjlist[i] = []
	adjlist2[i] = []
	outlist[i] = 0
while m:
	[u, v] = list(map(int, input().split()))
	adjlist[u].append(v)
	adjlist2[v].append(u)
	outlist[u] += 1
	m -= 1
for (key, value) in outlist.items():
	if value == 0:
		l.put(key)
while l.empty() != True:
	if l.qsize() == 1:
		step += 1
		data = l.get()
		res.append([1, data])
		for key in list(adjlist2[data]):
			outlist[key] -= 1
			if outlist[key] == 0:
				l.put(key)
			adjlist[key].remove(data)
	else:
		step += 1
		data1 = l.get()
		data2 = l.get()
		res.append([2, data1, data2])
		for data in [data1, data2]:
			for key in adjlist2[data]:
				outlist[key] -= 1
				if outlist[key] == 0:
					l.put(key)
				adjlist[key].remove(data)
print(step)
for i in res:
	print(*i)


(n, m) = map(int, input().split())
child = {}
l = {}
for i in range(m):
	(a, b) = map(int, input().split())
	if a not in child:
		child[a] = 1
	else:
		child[a] += 1
	if b not in l:
		l[b] = [a]
	else:
		l[b].append(a)
mainans = []
startans = []
for i in l:
	if i not in child:
		startans.append(i)
mainans.append(startans)
expnodes = []
for i in range(1, n + 1):
	if i not in l and i not in child:
		expnodes.append(i)
parcount = {}
for count in range(1000000):
	levansc = []
	for i in mainans[count]:
		if i in l:
			for j in l[i]:
				if j not in parcount:
					parcount[j] = child[j] - 1
				else:
					parcount[j] -= 1
				if parcount[j] == 0:
					levansc.append(j)
	if len(levansc) == 0:
		break
	mainans.append(levansc)
	count += 1
ans = []
count = 0
prev = 0
bt = 0
for i in mainans:
	j = 0
	while j < len(i):
		new = i[j]
		if count % 2 == 1:
			if i[j] in child:
				if child[i[j]] == 0:
					ans.append(i[j])
					ans.insert(-2, '2')
					bt += 1
					count = 0
					if i[j] in l:
						for a in l[i[j]]:
							child[a] -= 1
					if prev in l:
						for b in l[prev]:
							child[b] -= 1
				elif len(expnodes) != 0:
					w = expnodes.pop()
					ans.append(w)
					ans.insert(-2, '2')
					bt += 1
					for b in l[prev]:
						child[b] -= 1
					ans.append(i[j])
					count = 1
					prev = i[j]
				else:
					ans.insert(-1, '1')
					bt += 1
					for b in l[prev]:
						child[b] -= 1
					ans.append(i[j])
					count = 1
					prev = i[j]
			else:
				ans.append(i[j])
				ans.insert(-2, '2')
				bt += 1
				count = 0
				for a in l[i[j]]:
					child[a] -= 1
				for b in l[prev]:
					child[b] -= 1
		else:
			ans.append(i[j])
			prev = i[j]
			count = 1
		j += 1
if count == 1:
	if len(expnodes) != 0:
		w = expnodes.pop()
		ans.append(w)
		ans.insert(-2, '2')
		count = 0
		bt += 1
		while len(expnodes) > 0:
			if count == 1:
				w = expnodes.pop()
				ans.append(w)
				ans.insert(-2, '2')
				bt += 1
				count = 0
			else:
				w = expnodes.pop()
				ans.append(w)
				count = 1
		if count == 1:
			ans.insert(-1, '1')
			bt += 1
	else:
		ans.insert(-1, '1')
		bt += 1
else:
	while len(expnodes) > 0:
		if count == 1:
			w = expnodes.pop()
			ans.append(w)
			ans.insert(-2, '2')
			bt += 1
			count = 0
		else:
			w = expnodes.pop()
			ans.append(w)
			count = 1
	if count == 1:
		ans.insert(-1, '1')
		bt += 1
print(bt)
s = ''
i = 0
while i < len(ans):
	if ans[i] == '1':
		print(ans[i], ans[i + 1])
		i = i + 2
	else:
		print(ans[i], ans[i + 1], ans[i + 2])
		i = i + 3


k = [int(j) for j in input().split()]
a = [[0] * k[0]] * k[0]
outg1 = [0] * k[0]
inc = [0] * k[0]
outge = {}
inco = {}
for j in range(k[1]):
	b = [int(k) for k in input().split()]
	a[b[0] - 1][b[1] - 1] = 1
	outg1[b[0] - 1] = outg1[b[0] - 1] + 1
	inc[b[1] - 1] = inc[b[1] - 1] + 1
	if b[0] - 1 in outge.keys():
		outge[b[0] - 1].append(b[1] - 1)
	else:
		outge[b[0] - 1] = [b[1] - 1]
	if b[1] - 1 in inco.keys():
		inco[b[1] - 1].append(b[0] - 1)
	else:
		inco[b[1] - 1] = [b[0] - 1]
curr = []
ans = []
for i in range(k[0]):
	if outg1[i] == 0:
		curr.append(i)
while len(curr) > 0:
	if len(curr) >= 2:
		ans.append([curr[0], curr[1]])
		if curr[0] in inco.keys():
			for i in inco[curr[0]]:
				outg1[i] = outg1[i] - 1
				if outg1[i] == 0:
					curr.append(i)
		if curr[1] in inco.keys():
			for i in inco[curr[1]]:
				outg1[i] = outg1[i] - 1
				if outg1[i] == 0:
					curr.append(i)
		curr.pop(0)
		curr.pop(0)
	elif len(curr) == 1:
		ans.append([curr[0]])
		if curr[0] in inco.keys():
			for i in inco[curr[0]]:
				outg1[i] -= 1
				if outg1[i] == 0:
					curr.append(i)
		curr.pop(0)
print(len(ans))
temp = 0
for i in ans:
	if len(ans[0]) == 2 and len(ans[1]) == 1 and (i == ans[0] or i == ans[1]) and (outg1[ans[1][0]] == 0):
		if i == ans[0]:
			print('1 ' + str(ans[1][0] + 1))
		elif i == ans[1]:
			print('2 ' + str(ans[0][0] + 1) + ' ' + str(ans[0][1] + 1))
	elif len(i) == 2:
		print('2 ' + str(i[0] + 1) + ' ' + str(i[1] + 1))
	elif len(i) == 1:
		print('1 ' + str(i[0] + 1))


class MaxHeap:

	def __init__(self):
		self.arr = [0]
		self.heap_size = 0

	def parent(self, i):
		return (i - 1) // 2

	def left(self, i):
		return 2 * i + 1

	def right(self, i):
		return 2 * i + 2

	def insertKey(self, k):
		self.heap_size += 1
		i = self.heap_size - 1
		try:
			self.arr[i] = k
		except:
			self.arr.append(k)
		while i != 0 and deg[self.arr[self.parent(i)]] < deg[self.arr[i]]:
			(self.arr[i], self.arr[self.parent(i)]) = (self.arr[self.parent(i)], self.arr[i])
			i = self.parent(i)

	def extractMax(self):
		if self.heap_size == 1:
			self.heap_size -= 1
			return self.arr[0]
		root = self.arr[0]
		self.arr[0] = self.arr[self.heap_size - 1]
		self.heap_size -= 1
		self.MaxHeapify(0)
		return root

	def MaxHeapify(self, i):
		l = self.left(i)
		r = self.right(i)
		smallest = i
		if l < self.heap_size and deg[self.arr[l]] >= deg[self.arr[i]]:
			smallest = l
		if r < self.heap_size and deg[self.arr[r]] >= deg[self.arr[smallest]]:
			smallest = r
		if smallest != i:
			(self.arr[i], self.arr[smallest]) = (self.arr[smallest], self.arr[i])
			self.MaxHeapify(smallest)
c = 0
v = []
(n, m) = map(int, input().split())
d = {}
back = {}
l = {}
d1 = {}
deg = {}
back1 = {}
l1 = {}
z = MaxHeap()
co = 0
for _ in range(1, n + 1):
	back[_] = []
	deg[_] = 0
	l[_] = 0
	d1[_] = []
	back1[_] = []
	l1[_] = 0
for _ in range(m):
	(u, v) = map(int, input().split())
	deg[v] += 1
	back[v].append(u)
	l[u] += 1
	d1[u].append(v)
	back1[v].append(u)
	l1[u] += 1
c = 0
v = []
c1 = 0
v1 = []
z1 = []
m = n
m1 = n
for i in range(1, n + 1):
	try:
		if l[i] == 0:
			z1.append(i)
			z.insertKey(i)
			co += 1
	except:
		pass
while m > 0:
	if co == 0:
		break
	if co == 1:
		m -= 1
		c += 1
		k = z.extractMax()
		v.append('1 ' + str(k))
		for i in back[k]:
			try:
				l[i] -= 1
				if l[i] == 0:
					z.insertKey(i)
					co += 1
			except:
				pass
		co -= 1
	else:
		m -= 2
		c += 1
		k1 = z.extractMax()
		k2 = z.extractMax()
		v.append('2 ' + str(k1) + ' ' + str(k2))
		for k in back[k1]:
			try:
				l[k] -= 1
				if l[k] == 0:
					z.insertKey(k)
					co += 1
			except:
				pass
		for k in back[k2]:
			try:
				l[k] -= 1
				if l[k] == 0:
					z.insertKey(k)
					co += 1
			except:
				pass
		co -= 2
while m1 > 0:
	if len(z1) == 1:
		v1.append('1 ' + str(z1[0]))
		d1.pop(z1[0])
		m1 -= 1
		c1 += 1
		for i in back[z1[0]]:
			try:
				d1[i].remove(z1[0])
				l1[i] -= 1
				if l1[i] == 0:
					z1.append(i)
			except:
				pass
		del z1[0]
	else:
		m1 -= 2
		c1 += 1
		v1.append('2 ' + str(z1[0]) + ' ' + str(z1[1]))
		d1.pop(z1[0])
		d1.pop(z1[1])
		for k in back1[z1[0]]:
			try:
				d1[k].remove(z1[0])
				l1[k] -= 1
				if l1[k] == 0:
					z1.append(k)
			except:
				pass
		for k in back1[z1[1]]:
			try:
				d1[k].remove(z1[1])
				l1[k] -= 1
				if l1[k] == 0:
					z1.append(k)
			except:
				pass
		del z1[0]
		del z1[0]
if c < c1:
	print(c)
	for i in v:
		print(i)
else:
	print(c1)
	for i in v1:
		print(i)


class MaxHeap:

	def __init__(self):
		self.arr = [0]
		self.heap_size = 0

	def parent(self, i):
		return (i - 1) // 2

	def left(self, i):
		return 2 * i + 1

	def right(self, i):
		return 2 * i + 2

	def insertKey(self, k):
		self.heap_size += 1
		i = self.heap_size - 1
		try:
			self.arr[i] = k
		except:
			self.arr.append(k)
		while i != 0 and deg[self.arr[self.parent(i)]] > deg[self.arr[i]]:
			(self.arr[i], self.arr[self.parent(i)]) = (self.arr[self.parent(i)], self.arr[i])
			i = self.parent(i)

	def extractMax(self):
		if self.heap_size == 1:
			self.heap_size -= 1
			return self.arr[0]
		root = self.arr[0]
		self.arr[0] = self.arr[self.heap_size - 1]
		self.heap_size -= 1
		self.MaxHeapify(0)
		return root

	def MaxHeapify(self, i):
		l = self.left(i)
		r = self.right(i)
		smallest = i
		if l < self.heap_size and deg[self.arr[l]] <= deg[self.arr[i]]:
			smallest = l
		if r < self.heap_size and deg[self.arr[r]] <= deg[self.arr[smallest]]:
			smallest = r
		if smallest != i:
			(self.arr[i], self.arr[smallest]) = (self.arr[smallest], self.arr[i])
			self.MaxHeapify(smallest)
(n, m) = map(int, input().split())
back = {}
l = {}
deg = {}
for _ in range(1, n + 1):
	back[_] = []
	l[_] = 0
	deg[_] = 0
for _ in range(m):
	(u, v) = map(int, input().split())
	back[v].append(u)
	l[u] += 1
	deg[v] += 1
c = 0
v = []
z = MaxHeap()
m = n
co = 0
for i in range(1, n + 1):
	if l[i] == 0:
		z.insertKey(i)
		co += 1
while m > 0:
	if co == 0:
		break
	if co == 1:
		m -= 1
		c += 1
		k = z.extractMax()
		v.append('1 ' + str(k))
		for i in back[k]:
			try:
				l[i] -= 1
				if l[i] == 0:
					z.insertKey(i)
					co += 1
			except:
				pass
		co -= 1
	else:
		m -= 2
		c += 1
		k1 = z.extractMax()
		k2 = z.extractMax()
		v.append('2 ' + str(k1) + ' ' + str(k2))
		for k in back[k1]:
			try:
				l[k] -= 1
				if l[k] == 0:
					z.insertKey(k)
					co += 1
			except:
				pass
		for k in back[k2]:
			try:
				l[k] -= 1
				if l[k] == 0:
					z.insertKey(k)
					co += 1
			except:
				pass
		co -= 2
if m <= 0:
	print(c)
	for i in v:
		print(i)
else:
	print('hi')


import sys

def fprint(x):
	print(x, flush=True)

def qprint(x):
	sys.stdout.write(str(x) + '\n')

def input():
	return sys.stdin.readline()
import itertools

class node:

	def __init__(self, id):
		self.id = id
		self.parent = []
		self.children = []
		self.deleted = False
(N, M) = map(int, input().split())
edges = []
for x in range(M):
	edges.append([])
nodes = []
for x in range(N + 1):
	nodes.append(node(x))
for x in range(M):
	(u, v) = map(int, input().split())
	edges[u].append(v)
	nodes[v].parent.append(u)
	nodes[u].children.append(v)
noChildQueue = []
num_steps = 0
steps = []
for ind in range(1, len(nodes)):
	_node = nodes[ind]
	if len(_node.children) == 0:
		noChildQueue.append(_node.id)
while len(noChildQueue) != 0:
	num_steps += 1
	if len(noChildQueue) == 1:
		cur_node = nodes[noChildQueue.pop(0)]
		steps.append('1 ' + str(cur_node.id))
		for parent in cur_node.parent:
			nodes[parent].children.remove(cur_node.id)
			if len(nodes[parent].children) == 0:
				noChildQueue.append(parent)
	else:
		steps.append('2 ' + str(nodes[noChildQueue[0]].id) + ' ' + str(nodes[noChildQueue[1]].id))
		for x in range(0, 2):
			cur_node = nodes[noChildQueue.pop(0)]
			for parent in cur_node.parent:
				nodes[parent].children.remove(cur_node.id)
				if len(nodes[parent].children) == 0:
					noChildQueue.append(parent)
print(num_steps)
for entry in steps:
	print(entry)