数组表示法
#include <iostream>
#include <malloc.h>
#include <queue>
#include <string>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <queue>
using namespace std;
priority_queue<int, vector<int>, greater<int> > q;
int t,n,m, Father[105], Right[105], Left[105];
int main(int argc, char *argv[])
{
cin >> t;
while( t-- )
{
cin >> n >> m;
memset(Father, 0, sizeof(Father));
memset(Left, 0, sizeof(Left));
memset(Right, 0, sizeof(Right));
for( int i = 1; i <= n; i++ )
{
int x,y,z;
cin >> x >> y >> z;
Left[x] = y;
Right[x] = z;
Father[y] = x;
Father[z] = x;
}
while( m-- )
{
int type;
cin >> type;
if( type == 1 )
{
int x,y;
cin >> x >>y;
if( Father[x] == Father[y] )
{
swap( Left[Father[x]], Right[Father[x]]);
}
else
{
if( x == Left[Father[x]] )
{
Left[Father[x]] = y;
}
else
{
Right[Father[x]] = y;
}
if( y == Left[Father[y]] )
{
Left[Father[y]] = x;
}
else
{
Right[Father[y]] = x;
}
swap( Father[x], Father[y] );
}
}
else if( type == 2 )
{
int x;
cin >> x;
while( Left[x] != -1 )
{
x = Left[x];
}
cout << x << endl;
}
}
}
return 0;
}