#abc372e. E - K-th Largest Connected Components

E - K-th Largest Connected Components

Score : 475475 points

问题陈述

有一个无向图,包含 NN 个顶点和 00 条边。顶点编号从 11NN

你需要按顺序处理 QQ 个查询。每个查询是以下两种类型之一:

  • 类型 11:格式为 1 u v。在顶点 uuvv 之间添加一条边。
  • 类型 22:格式为 2 v k。打印与顶点 vv 相连的顶点中第 kk 大的顶点编号。如果与 vv 相连的顶点少于 kk 个,则打印 -1

以上为大语言模型 kimi 翻译,仅供参考。

Problem Statement

There is an undirected graph with NN vertices and 00 edges. The vertices are numbered 11 to NN.

You are given QQ queries to process in order. Each query is of one of the following two types:

  • Type 11: Given in the format 1 u v. Add an edge between vertices uu and vv.
  • Type 22: Given in the format 2 v k. Print the kk-th largest vertex number among the vertices connected to vertex vv. If there are fewer than kk vertices connected to vv, print -1.

Constraints

  • 1N,Q2×1051 \leq N, Q \leq 2 \times 10^5
  • In a Type 11 query, 1u<vN1 \leq u < v \leq N.
  • In a Type 22 query, 1vN1 \leq v \leq N, 1k101 \leq k \leq 10.
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

NN QQ

query1\mathrm{query}_1

query2\mathrm{query}_2

\vdots

queryQ\mathrm{query}_Q

Here, queryi\mathrm{query}_i is the ii-th query and is given in one of the following formats:

11 uu vv

22 vv kk

Output

Let qq be the number of Type 22 queries. Print qq lines. The ii-th line should contain the answer to the ii-th Type 22 query.

Sample Input 1

4 10
1 1 2
2 1 1
2 1 2
2 1 3
1 1 3
1 2 3
1 3 4
2 1 1
2 1 3
2 1 5

Sample Output 1

2
1
-1
4
2
-1
  • In the first query, an edge is added between vertices 11 and 22.
  • In the second query, two vertices are connected to vertex 11: 11 and 22. Among them, the 11-st largest vertex number is 22, which should be printed.
  • In the third query, two vertices are connected to vertex 11: 11 and 22. Among them, the 22-nd largest vertex number is 11, which should be printed.
  • In the fourth query, two vertices are connected to vertex 11: 11 and 22, which is fewer than 33, so print -1.
  • In the fifth query, an edge is added between vertices 11 and 33.
  • In the sixth query, an edge is added between vertices 22 and 33.
  • In the seventh query, an edge is added between vertices 33 and 44.
  • In the eighth query, four vertices are connected to vertex 11: 1,2,3,41,2,3,4. Among them, the 11-st largest vertex number is 44, which should be printed.
  • In the ninth query, four vertices are connected to vertex 11: 1,2,3,41,2,3,4. Among them, the 33-rd largest vertex number is 22, which should be printed.
  • In the tenth query, four vertices are connected to vertex 11: 1,2,3,41,2,3,4, which is fewer than 55, so print -1.

Sample Input 2

6 20
1 3 4
1 3 5
2 1 1
2 3 1
1 1 5
2 6 9
2 1 3
2 6 1
1 4 6
2 2 1
2 6 2
2 4 7
1 1 4
2 6 2
2 3 4
1 2 5
2 4 1
1 1 6
2 3 3
2 1 3

Sample Output 2

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