树的直径一定是原联通块4个里的组合
1.LCT,维护树的直径,这题就做完了
2.直接倍增,lca啥的求求距离,也可以吧…
// powered by c++11
// by Isaunoya
#include <bits/stdc++.h>
#define rep(i, x, y) for (register int i = (x); i <= (y); ++i)
#define Rep(i, x, y) for (register int i = (x); i >= (y); --i)
using namespace std;
using db = double;
using ll = long long;
using uint = unsigned int;
// #define int long long
using pii = pair<int, int>;
#define ve vector
#define Tp template
#define all(v) v.begin(), v.end()
#define sz(v) ((int)v.size())
#define pb emplace_back
#define fir first
#define sec second
// the cmin && cmax
Tp<class T> void cmax(T& x, const T& y) {
if (x < y) x = y;
}
Tp<class T> void cmin(T& x, const T& y) {
if (x > y) x = y;
}
// sort , unique , reverse
Tp<class T> void sort(ve<T>& v) { sort(all(v)); }
Tp<class T> void unique(ve<T>& v) {
sort(all(v));
v.erase(unique(all(v)), v.end());
}
Tp<class T> void reverse(ve<T>& v) { reverse(all(v)); }
const int SZ = 0x191981;
struct FILEIN {
~FILEIN() {}
char qwq[SZ], *S = qwq, *T = qwq, ch;
char GETC() { return (S == T) && (T = (S = qwq) + fread(qwq, 1, SZ, stdin), S == T) ? EOF : *S++; }
FILEIN& operator>>(char& c) {
while (isspace(c = GETC()))
;
return *this;
}
FILEIN& operator>>(string& s) {
while (isspace(ch = GETC()))
;
s = ch;
while (!isspace(ch = GETC())) s += ch;
return *this;
}
Tp<class T> void read(T& x) {
bool sign = 1;
while ((ch = GETC()) < 0x30)
if (ch == 0x2d) sign = 0;
x = (ch ^ 0x30);
while ((ch = GETC()) > 0x2f) x = x * 0xa + (ch ^ 0x30);
x = sign ? x : -x;
}
FILEIN& operator>>(int& x) { return read(x), *this; }
// FILEIN& operator>>(signed& x) { return read(x), *this; }
FILEIN& operator>>(unsigned& x) { return read(x), *this; }
} in;
struct FILEOUT {
const static int LIMIT = 0x114514;
char quq[SZ], ST[0x114];
signed sz, O;
~FILEOUT() { sz = O = 0; }
void flush() {
fwrite(quq, 1, O, stdout);
fflush(stdout);
O = 0;
}
FILEOUT& operator<<(char c) { return quq[O++] = c, *this; }
FILEOUT& operator<<(string str) {
if (O > LIMIT) flush();
for (char c : str) quq[O++] = c;
return *this;
}
Tp<class T> void write(T x) {
if (O > LIMIT) flush();
if (x < 0) {
quq[O++] = 0x2d;
x = -x;
}
do {
ST[++sz] = x % 0xa ^ 0x30;
x /= 0xa;
} while (x);
while (sz) quq[O++] = ST[sz--];
return;
}
FILEOUT& operator<<(int x) { return write(x), *this; }
// FILEOUT& operator<<(signed x) { return write(x), *this; }
FILEOUT& operator<<(unsigned x) { return write(x), *this; }
} out;
const int maxn = 3e5 + 10;
int f[maxn];
int find(int x) { return x == f[x] ? x : f[x] = find(f[x]); }
int px[maxn], py[maxn], fa[maxn], sz[maxn], rev[maxn], ch[maxn][2];
#define ls(x) ch[x][0]
#define rs(x) ch[x][1]
bool isroot(int x) { return (x != ls(fa[x])) && (x != rs(fa[x])); }
void pushup(int x) { sz[x] = sz[ls(x)] + sz[rs(x)] + 1; }
void pushr(int x) {
rev[x] ^= 1;
swap(ls(x), rs(x));
}
void pushdown(int x) {
if (!rev[x]) return;
if (ls(x)) pushr(ls(x));
if (rs(x)) pushr(rs(x));
rev[x] = 0;
}
void pushall(int x) {
if (!isroot(x)) pushall(fa[x]);
pushdown(x);
}
void rotate(int x) {
int y = fa[x], z = fa[y], l = (rs(y) == x), r = l ^ 1;
if (!isroot(y)) ch[z][rs(z) == y] = x;
fa[x] = z, fa[y] = x;
fa[ch[x][r]] = y, ch[y][l] = ch[x][r], ch[x][r] = y;
pushup(y), pushup(x);
}
void splay(int x) {
pushall(x);
while (!isroot(x)) {
int y = fa[x], z = fa[y];
if (!isroot(y)) rotate(((rs(y) == x) ^ (rs(z) == y)) ? x : y);
rotate(x);
}
}
void access(int x) {
for (int tp = 0; x; tp = x, x = fa[tp]) splay(x), rs(x) = tp, pushup(x);
}
void makeroot(int x) { access(x), splay(x), pushr(x); }
int dis(int x, int y) {
makeroot(x), access(y), splay(y);
return sz[y];
}
int qwq[5];
void link(int x, int y) {
int fx = find(x), fy = find(y);
int mx = -1, d = 0, rx, ry, cnt = 0;
makeroot(x), fa[x] = y;
qwq[++cnt] = px[fx], qwq[++cnt] = py[fx];
qwq[++cnt] = px[fy], qwq[++cnt] = py[fy];
for (int i = 1; i <= 4; i++)
for (int j = i + 1; j <= 4; j++) {
d = dis(qwq[i], qwq[j]);
if (d > mx) mx = d, rx = qwq[i], ry = qwq[j];
}
f[fx] = fy, px[fy] = rx, py[fy] = ry;
}
int type, n, q, opt, ans = 0;
signed main() {
#ifdef _WIN64
freopen("testdata.in", "r", stdin);
#else
ios_base ::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
#endif
// code begin.
in >> type >> n >> q;
for (int i = 1; i <= n; i++) f[i] = px[i] = py[i] = i, sz[i] = 1;
while (q--) {
int opt;
in >> opt;
if (opt == 1) {
int x, y;
in >> x >> y, x ^= ans, y ^= ans, link(x, y);
} else {
int x, fx;
in >> x, x ^= ans, fx = find(x);
ans = max(dis(x, px[fx]), dis(x, py[fx]));
ans--;
out << ans << '
';
}
if (!type) ans = 0;
}
return out.flush(), 0;
// code end.
}