#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
template<class T, class T2> inline void chkmax(T &x, const T2 &y) { if(x < y) x = y; }
template<class T, class T2> inline void chkmin(T &x, const T2 &y) { if(x > y) x = y; }
const int MAXN = (1 << 20);
const int MAXLOG = 20;
struct dsu
{
int sz;
vector<int> par, psz;
void init(int n)
{
sz = n;
par.assign(sz + 1, 0);
psz.assign(sz + 1, 0);
for(int i = 0; i <= sz; i++)
par[i] = i, psz[i] = 1;
}
int root(int u) { return par[u] = ((u == par[u]) ? u : root(par[u])); }
bool connected(int x, int y) { return root(x) == root(y); }
void unite(int x, int y)
{
x = root(x), y = root(y);
if(psz[x] > psz[y]) swap(x, y);
par[x] = y, psz[y] += psz[x];
}
};
int n = 0, q;
vector<int> G[MAXN];
string type[MAXN];
int argg[MAXN];
void read()
{
cin >> q;
for(int i = 0; i < q; i++)
{
cin >> type[i] >> argg[i];
if(type[i] == "B")
{
n++;
if(argg[i] != -1)
{
G[argg[i]].push_back(n);
G[n].push_back(argg[i]);
}
}
}
}
int dep[MAXN], st[MAXN], en[MAXN], dfs_time = 0;
int par[MAXN][MAXLOG];
void dfs_lca(int u)
{
st[u] = ++dfs_time;
for(int i = 1; i < MAXLOG; i++)
par[u][i] = par[par[u][i - 1]][i - 1];
for(int v: G[u])
if(v != par[u][0])
{
par[v][0] = u;
dep[v] = dep[u] + 1;
dfs_lca(v);
}
en[u] = dfs_time;
}
inline bool upper(int u, int v) { return st[u] <= st[v] && en[v] <= en[u]; }
int lca(int u, int v)
{
if(upper(u, v)) return u;
if(upper(v, u)) return v;
int a = u;
for(int i = MAXLOG - 1; i >= 0; i--)
if(!upper(par[a][i], v))
a = par[a][i];
return par[a][0];
}
int parent(int u, int up)
{
int a = u;
for(int i = MAXLOG - 1; i >= 0; i--)
if(up & (1 << i))
a = par[a][i];
return a;
}
void lca_precompute(int root)
{
for(int i = 0; i < MAXLOG; i++)
par[root][i] = root;
dfs_time = 0;
dfs_lca(root);
}
bool used[MAXN];
dsu d;
void dfs_check(int u)
{
used[u] = 1;
for(int v: G[u])
if(!used[v])
dfs_check(v);
}
int d1[MAXN], d2[MAXN];
int dist(int u, int v) { return dep[u] + dep[v] - 2 * dep[lca(u, v)]; }
void merge(int a, int b)
{
d.unite(a, b);
vector<int> cands = {d1[a], d2[a], d1[b], d2[b]};
int root = d.root(a);
int mx = 0;
for(int i = 0; i < (int)cands.size(); i++)
for(int j = i + 1; j < (int)cands.size(); j++)
{
int d = dist(cands[i], cands[j]);
if(d > mx)
{
mx = d;
d1[root] = cands[i];
d2[root] = cands[j];
}
}
}
void solve()
{
for(int i = 1; i <= n; i++)
if(!used[i])
{
lca_precompute(i);
dfs_check(i);
}
d.init(n);
for(int i = 1; i <= n; i++)
d1[i] = i, d2[i] = i;
int c_ver = 0;
for(int i = 0; i < q; i++)
{
if(type[i] == "B")
{
c_ver++;
if(argg[i] != -1)
merge(d.root(argg[i]), d.root(c_ver));
}
else
{
int u = argg[i], c = d.root(u);
cout << max(dist(u, d1[c]), dist(u, d2[c])) << endl;
}
}
}
int main()
{
freopen("newbarn.in", "r", stdin);
freopen("newbarn.out", "w", stdout);
ios_base::sync_with_stdio(false);
cin.tie(NULL);
read();
solve();
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNkZWZpbmUgZW5kbCAnXG4nCgp1c2luZyBuYW1lc3BhY2Ugc3RkOwp0ZW1wbGF0ZTxjbGFzcyBULCBjbGFzcyBUMj4gaW5saW5lIHZvaWQgY2hrbWF4KFQgJngsIGNvbnN0IFQyICZ5KSB7IGlmKHggPCB5KSB4ID0geTsgfQp0ZW1wbGF0ZTxjbGFzcyBULCBjbGFzcyBUMj4gaW5saW5lIHZvaWQgY2hrbWluKFQgJngsIGNvbnN0IFQyICZ5KSB7IGlmKHggPiB5KSB4ID0geTsgfQpjb25zdCBpbnQgTUFYTiA9ICgxIDw8IDIwKTsKY29uc3QgaW50IE1BWExPRyA9IDIwOwoKc3RydWN0IGRzdQp7Cglpb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1tpXSksIGQucm9vdChjX3ZlcikpOwoJCX0KCQllbHNlCgkJewoJCQlpbnQgdSA9IGFyZ2dbaV0sIGMgPSBkLnJvb3QodSk7CgkJCWNvdXQgPDwgbWF4KGRpc3QodSwgZDFbY10pLCBkaXN0KHUsIGQyW2NdKSkgPDwgZW5kbDsKCQl9Cgl9Cn0KCmludCBtYWluKCkKewoJZnJlb3BlbigibmV3YmFybi5pbiIsICJyIiwgc3RkaW4pOwoJZnJlb3BlbigibmV3YmFybi5vdXQiLCAidyIsIHN0ZG91dCk7Cglpb3NfYmFzZTo6c3luY193aXRoX3N0ZGlvKGZhbHNlKTsKCWNpbi50aWUoTlVMTCk7CgoJcmVhZCgpOwoJc29sdmUoKTsKCXJldHVybiAwOwp9Cgo=
Main.java:1: error: illegal character: '#'
#include <bits/stdc++.h>
^
Main.java:1: error: class, interface, or enum expected
#include <bits/stdc++.h>
^
Main.java:2: error: illegal character: '#'
#define endl '\n'
^
Main.java:5: error: class, interface, or enum expected
template<class T, class T2> inline void chkmax(T &x, const T2 &y) { if(x < y) x = y; }
^
Main.java:5: error: '{' expected
template<class T, class T2> inline void chkmax(T &x, const T2 &y) { if(x < y) x = y; }
^
Main.java:5: error: '{' expected
template<class T, class T2> inline void chkmax(T &x, const T2 &y) { if(x < y) x = y; }
^
Main.java:5: error: <identifier> expected
template<class T, class T2> inline void chkmax(T &x, const T2 &y) { if(x < y) x = y; }
^
Main.java:5: error: ';' expected
template<class T, class T2> inline void chkmax(T &x, const T2 &y) { if(x < y) x = y; }
^
Main.java:5: error: illegal start of type
template<class T, class T2> inline void chkmax(T &x, const T2 &y) { if(x < y) x = y; }
^
Main.java:5: error: <identifier> expected
template<class T, class T2> inline void chkmax(T &x, const T2 &y) { if(x < y) x = y; }
^
Main.java:5: error: ';' expected
template<class T, class T2> inline void chkmax(T &x, const T2 &y) { if(x < y) x = y; }
^
Main.java:5: error: illegal start of type
template<class T, class T2> inline void chkmax(T &x, const T2 &y) { if(x < y) x = y; }
^
Main.java:5: error: ';' expected
template<class T, class T2> inline void chkmax(T &x, const T2 &y) { if(x < y) x = y; }
^
Main.java:6: error: illegal start of type
template<class T, class T2> inline void chkmin(T &x, const T2 &y) { if(x > y) x = y; }
^
Main.java:6: error: <identifier> expected
template<class T, class T2> inline void chkmin(T &x, const T2 &y) { if(x > y) x = y; }
^
Main.java:6: error: <identifier> expected
template<class T, class T2> inline void chkmin(T &x, const T2 &y) { if(x > y) x = y; }
^
Main.java:6: error: <identifier> expected
template<class T, class T2> inline void chkmin(T &x, const T2 &y) { if(x > y) x = y; }
^
Main.java:6: error: invalid method declaration; return type required
template<class T, class T2> inline void chkmin(T &x, const T2 &y) { if(x > y) x = y; }
^
Main.java:6: error: <identifier> expected
template<class T, class T2> inline void chkmin(T &x, const T2 &y) { if(x > y) x = y; }
^
Main.java:6: error: ';' expected
template<class T, class T2> inline void chkmin(T &x, const T2 &y) { if(x > y) x = y; }
^
Main.java:6: error: illegal start of type
template<class T, class T2> inline void chkmin(T &x, const T2 &y) { if(x > y) x = y; }
^
Main.java:6: error: <identifier> expected
template<class T, class T2> inline void chkmin(T &x, const T2 &y) { if(x > y) x = y; }
^
Main.java:6: error: ';' expected
template<class T, class T2> inline void chkmin(T &x, const T2 &y) { if(x > y) x = y; }
^
Main.java:6: error: illegal start of type
template<class T, class T2> inline void chkmin(T &x, const T2 &y) { if(x > y) x = y; }
^
Main.java:6: error: ';' expected
template<class T, class T2> inline void chkmin(T &x, const T2 &y) { if(x > y) x = y; }
^
Main.java:7: error: illegal start of type
const int MAXN = (1 << 20);
^
Main.java:7: error: ';' expected
const int MAXN = (1 << 20);
^
Main.java:7: error: <identifier> expected
const int MAXN = (1 << 20);
^
Main.java:8: error: illegal start of type
const int MAXLOG = 20;
^
Main.java:8: error: ';' expected
const int MAXLOG = 20;
^
Main.java:8: error: <identifier> expected
const int MAXLOG = 20;
^
Main.java:10: error: ';' expected
struct dsu
^
Main.java:15: error: illegal start of expression
void init(int n)
^
Main.java:15: error: ';' expected
void init(int n)
^
Main.java:15: error: ';' expected
void init(int n)
^
Main.java:21: error: ';' expected
par[i] = i, psz[i] = 1;
^
Main.java:24: error: ';' expected
int root(int u) { return par[u] = ((u == par[u]) ? u : root(par[u])); }
^
Main.java:24: error: ';' expected
int root(int u) { return par[u] = ((u == par[u]) ? u : root(par[u])); }
^
Main.java:26: error: ';' expected
bool connected(int x, int y) { return root(x) == root(y); }
^
Main.java:26: error: <identifier> expected
bool connected(int x, int y) { return root(x) == root(y); }
^
Main.java:26: error: not a statement
bool connected(int x, int y) { return root(x) == root(y); }
^
Main.java:26: error: ';' expected
bool connected(int x, int y) { return root(x) == root(y); }
^
Main.java:28: error: illegal start of expression
void unite(int x, int y)
^
Main.java:28: error: ';' expected
void unite(int x, int y)
^
Main.java:28: error: <identifier> expected
void unite(int x, int y)
^
Main.java:28: error: not a statement
void unite(int x, int y)
^
Main.java:28: error: ';' expected
void unite(int x, int y)
^
Main.java:30: error: ';' expected
x = root(x), y = root(y);
^
Main.java:32: error: ';' expected
par[x] = y, psz[y] += psz[x];
^
Main.java:37: error: ']' expected
vector<int> G[MAXN];
^
Main.java:37: error: illegal start of type
vector<int> G[MAXN];
^
Main.java:37: error: <identifier> expected
vector<int> G[MAXN];
^
Main.java:37: error: ';' expected
vector<int> G[MAXN];
^
Main.java:38: error: ']' expected
string type[MAXN];
^
Main.java:38: error: ';' expected
string type[MAXN];
^
Main.java:39: error: ']' expected
int argg[MAXN];
^
Main.java:39: error: illegal start of type
int argg[MAXN];
^
Main.java:39: error: <identifier> expected
int argg[MAXN];
^
Main.java:39: error: ';' expected
int argg[MAXN];
^
Main.java:41: error: invalid method declaration; return type required
void read()
^
Main.java:43: error: not a statement
cin >> q;
^
Main.java:46: error: not a statement
cin >> type[i] >> argg[i];
^
Main.java:59: error: ']' expected
int dep[MAXN], st[MAXN], en[MAXN], dfs_time = 0;
^
Main.java:59: error: illegal start of type
int dep[MAXN], st[MAXN], en[MAXN], dfs_time = 0;
^
Main.java:59: error: <identifier> expected
int dep[MAXN], st[MAXN], en[MAXN], dfs_time = 0;
^
Main.java:59: error: ';' expected
int dep[MAXN], st[MAXN], en[MAXN], dfs_time = 0;
^
Main.java:59: error: illegal start of type
int dep[MAXN], st[MAXN], en[MAXN], dfs_time = 0;
^
Main.java:59: error: ';' expected
int dep[MAXN], st[MAXN], en[MAXN], dfs_time = 0;
^
Main.java:59: error: ']' expected
int dep[MAXN], st[MAXN], en[MAXN], dfs_time = 0;
^
Main.java:59: error: ';' expected
int dep[MAXN], st[MAXN], en[MAXN], dfs_time = 0;
^
Main.java:59: error: <identifier> expected
int dep[MAXN], st[MAXN], en[MAXN], dfs_time = 0;
^
Main.java:60: error: ']' expected
int par[MAXN][MAXLOG];
^
Main.java:60: error: illegal start of type
int par[MAXN][MAXLOG];
^
Main.java:60: error: <identifier> expected
int par[MAXN][MAXLOG];
^
Main.java:60: error: ';' expected
int par[MAXN][MAXLOG];
^
Main.java:60: error: illegal start of type
int par[MAXN][MAXLOG];
^
Main.java:60: error: <identifier> expected
int par[MAXN][MAXLOG];
^
Main.java:60: error: ';' expected
int par[MAXN][MAXLOG];
^
Main.java:62: error: invalid method declaration; return type required
void dfs_lca(int u)
^
Main.java:79: error: ';' expected
inline bool upper(int u, int v) { return st[u] <= st[v] && en[v] <= en[u]; }
^
Main.java:79: error: invalid method declaration; return type required
inline bool upper(int u, int v) { return st[u] <= st[v] && en[v] <= en[u]; }
^
Main.java:113: error: ']' expected
bool used[MAXN];
^
Main.java:113: error: illegal start of type
bool used[MAXN];
^
Main.java:113: error: <identifier> expected
bool used[MAXN];
^
Main.java:113: error: ';' expected
bool used[MAXN];
^
Main.java:114: error: <identifier> expected
dsu d;
^
Main.java:124: error: ']' expected
int d1[MAXN], d2[MAXN];
^
Main.java:124: error: illegal start of type
int d1[MAXN], d2[MAXN];
^
Main.java:124: error: <identifier> expected
int d1[MAXN], d2[MAXN];
^
Main.java:124: error: ';' expected
int d1[MAXN], d2[MAXN];
^
Main.java:124: error: illegal start of type
int d1[MAXN], d2[MAXN];
^
Main.java:124: error: ';' expected
int d1[MAXN], d2[MAXN];
^
Main.java:159: error: ';' expected
d1[i] = i, d2[i] = i;
^
Main.java:173: error: not a statement
cout << max(dist(u, d1[c]), dist(u, d2[c])) << endl;
^
Main.java:182: error: not a statement
ios_base::sync_with_stdio(false);
^
Main.java:182: error: ';' expected
ios_base::sync_with_stdio(false);
^
Main.java:188: error: reached end of file while parsing
}
^
97 errors