using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static string InputPattern = "InputX";
static List<string> GetInputList()
{
var WillReturn = new List<string>();
if (InputPattern == "Input1") {
WillReturn.Add("5 5");
WillReturn.Add("1 2 3 2 1");
WillReturn.Add("1 1 2");
WillReturn.Add("1 2 5");
WillReturn.Add("2 1 1");
WillReturn.Add("1 3 4");
WillReturn.Add("2 3 4");
//2
//0
}
else if (InputPattern == "Input2") {
WillReturn.Add("5 4");
WillReturn.Add("2 2 2 2 2");
WillReturn.Add("1 1 2");
WillReturn.Add("1 1 3");
WillReturn.Add("1 2 3");
WillReturn.Add("2 2 2");
//3
}
else if (InputPattern == "Input3") {
WillReturn.Add("12 9");
WillReturn.Add("1 2 3 1 2 3 1 2 3 1 2 3");
WillReturn.Add("1 1 2");
WillReturn.Add("1 3 4");
WillReturn.Add("1 5 6");
WillReturn.Add("1 7 8");
WillReturn.Add("2 2 1");
WillReturn.Add("1 9 10");
WillReturn.Add("2 5 6");
WillReturn.Add("1 4 8");
WillReturn.Add("2 6 1");
//1
//0
//0
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
static long mN;
static long[] mCArr;
static void Main()
{
List<string> InputList = GetInputList();
long[] wkArr = { };
Action<string> SplitAct = pStr =>
wkArr = pStr.Split(' ').Select(pX => long.Parse(pX)).ToArray();
SplitAct(InputList[0]);
mN = wkArr[0];
mCArr = InputList[1].Split(' ').Select(pX => long.Parse(pX)).ToArray();
var InsUnionFindSizeInfo = new UnionFindSizeInfo();
for (long I = 1; I <= mN; I++) {
InsUnionFindSizeInfo.MakeSet(I, mCArr[I - 1]);
}
mCArr = InputList[1].Split(' ').Select(pX => long.Parse(pX)).ToArray();
var sb = new System.Text.StringBuilder();
foreach (string EachStr in InputList.Skip(2)) {
SplitAct(EachStr);
if (wkArr[0] == 1) {
InsUnionFindSizeInfo.Unite(wkArr[1], wkArr[2]);
}
if (wkArr[0] == 2) {
long Result = InsUnionFindSizeInfo.GetClassCnt(wkArr[1], wkArr[2]);
sb.Append(Result);
sb.AppendLine();
}
}
Console.Write(sb.ToString());
}
}
// UnionFindSizeInfoクラス
internal class UnionFindSizeInfo
{
private class NodeInfoDef
{
internal long ParentNode;
internal long Rank;
internal Dictionary<long, long> ClassCntDict;
}
private Dictionary<long, NodeInfoDef> mNodeInfoDict =
new Dictionary<long, NodeInfoDef>();
// 要素が1つである木を森に追加
internal void MakeSet(long pNode, long pClass)
{
NodeInfoDef WillAdd = new NodeInfoDef();
WillAdd.ParentNode = pNode;
WillAdd.Rank = 0;
WillAdd.ClassCntDict = new Dictionary<long, long>();
WillAdd.ClassCntDict[pClass] = 1;
mNodeInfoDict[pNode] = WillAdd;
}
// 合併処理
internal void Unite(long pX, long pY)
{
long XNode = FindSet(pX);
long YNode = FindSet(pY);
// 既に同じ木の場合
if (XNode == YNode) return;
long XRank = mNodeInfoDict[XNode].Rank;
long YRank = mNodeInfoDict[YNode].Rank;
if (XRank > YRank) {
mNodeInfoDict[YNode].ParentNode = XNode;
foreach (var EachPair in mNodeInfoDict[YNode].ClassCntDict) {
if (mNodeInfoDict[XNode].ClassCntDict.ContainsKey(EachPair.Key) == false) {
mNodeInfoDict[XNode].ClassCntDict[EachPair.Key] = 0;
}
mNodeInfoDict[XNode].ClassCntDict[EachPair.Key] += EachPair.Value;
}
}
else {
mNodeInfoDict[XNode].ParentNode = YNode;
if (XRank == YRank) {
mNodeInfoDict[YNode].Rank++;
}
foreach (var EachPair in mNodeInfoDict[XNode].ClassCntDict) {
if (mNodeInfoDict[YNode].ClassCntDict.ContainsKey(EachPair.Key) == false) {
mNodeInfoDict[YNode].ClassCntDict[EachPair.Key] = 0;
}
mNodeInfoDict[YNode].ClassCntDict[EachPair.Key] += EachPair.Value;
}
}
}
// ノードを引数として、木の根を取得
internal long FindSet(long pTargetNode)
{
// 根までの経路上のノードのList
var PathNodeList = new List<long>();
long CurrNode = pTargetNode;
while (CurrNode != mNodeInfoDict[CurrNode].ParentNode) {
PathNodeList.Add(CurrNode);
CurrNode = mNodeInfoDict[CurrNode].ParentNode;
}
// 経路圧縮 (親ポインタの付け替え)
foreach (long EachPathNode in PathNodeList) {
mNodeInfoDict[EachPathNode].ParentNode = CurrNode;
}
return CurrNode;
}
// ノードとクラスを引数として、木に所属するクラスの人数を取得
internal long GetClassCnt(long pNode, long pClass)
{
long RootNode = FindSet(pNode);
if (mNodeInfoDict[RootNode].ClassCntDict.ContainsKey(pClass)) {
return mNodeInfoDict[RootNode].ClassCntDict[pClass];
}
return 0;
}
internal void DebugPrint()
{
foreach (var EachPair in mNodeInfoDict.OrderBy(pX => pX.Key)) {
Console.WriteLine("mNodeInfoDict[{0}].ParentNode={1}",
EachPair.Key, EachPair.Value.ParentNode);
}
}
}