#include <iostream>
#include <cstdio>
#include <math.h>
#include <algorithm>
#include <vector>
#include <string.h>
#include <set>
#include <map>
#include <bitset>
#include <time.h>
using namespace std;
typedef vector <int> vi;
#define ll long long
#define pb push_back
#define ld double
#define pld pair <ld, ld>
#define pll pair <ll, ll>
#define fi first
#define pii pair <int, int>
#define se second
#define mp make_pair
const int MAXN = 100 * 1000;
const int INF = 1000 * 1000;
int cnt[MAXN], pr[MAXN], ind[MAXN], num[MAXN];
int cntt = 0;
vector <int> g[MAXN];
bool is_heavy(int v, int pr)
{
return (cnt[v] < cnt[pr]) && (cnt[v] * 2 >= cnt[pr]);
}
class tree
{
public:
int len;
int ne;
vector <int> t;
vector <int> node;
void build(int v)
{
while (is_heavy(v, pr[v]))
{
ind[v] = (int)node.size();
node.pb(v);
num[v] = cntt;
v = pr[v];
}
if (v)
{
ind[v] = (int)node.size();
node.pb(v);
num[v] = cntt;
ne = pr[v];
}
for (len = 1; len < (int)node.size(); len *= 2);
t.assign(2 * len, INF);
}
void update(int ind, int v, int l, int r)
{
if (l == r)
{
if (t[v] == INF)
t[v] = node[v - len];
else
t[v] = INF;
return;
}
int s = (l + r) / 2;
if (ind <= s)
update(ind, 2 * v, l, s);
else
update(ind, 2 * v + 1, s + 1, r);
if (t[2 * v] == INF)
t[v] = t[2 * v + 1];
else
t[v] = t[2 * v];
}
int find(int tl, int tr, int v, int l, int r)
{
if (tl > tr) return INF;
if (tl == l && tr == r)
return t[v];
int s = (l + r) /s;
int vall = find(tl, min(s, tr), 2 * v, l, s);
int valr = find(max(tl, s + 1), tr, 2 * v + 1, s + 1, r);
if (vall == INF) return valr;
return vall;
}
};
tree t[MAXN];
int dfs(int v, int _pr)
{
pr[v] = _pr;
cnt[v] = 1;
for (int i = 0; i < (int)g[v].size(); i++)
if (g[v][i] != _pr)
cnt[v] += dfs(g[v][i], v);
return cnt[v];
}
void find_path(int v)
{
bool ok = 1;
for (int i = 0; i < (int)g[v].size(); i++)
if (g[v][i] != pr[v])
{
if (is_heavy(g[v][i], v))
ok = 0;
find_path(g[v][i]);
}
if (ok)
t[cntt].build(v), cntt++;
}
int color[MAXN];
int _type[MAXN * 4], _v[MAXN * 4];
int main()
{
//freopen("subsequences.in", "r", stdin);
//freopen("subsequences.out", "w", stdout);
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
ios_base::sync_with_stdio(false);
int n, q;
cin >> n >> q;
for (int i = 0; i < n - 1; i++)
{
int a, b;
cin >> a >> b;
a--, b--;
g[a].pb(b);
g[b].pb(a);
}
for (int i = 0; i < q; i++)
cin >> _type[i] >> _v[i];
dfs(0, 0);
find_path(0);
//for (int i = 0; i < cntt; i++)
// {
// for (int j = 0; j < (int)t[i].node.size(); j++)
// cout << t[i].node[j] << " ";
// cout << endl;
// }
// for (int i = 0; i < n; i++)
// cout << ind[i] << endl;
//return 0;
for (int i = 0; i < q; i++)
{
int type = _type[i];
int v = _v[i];
v--;
if (type)
{
int len = t[num[v]].len;
int ans = INF;
while (v && ans == INF)
{
ans = t[num[v]].find(ind[v] + len, len * 2 - 1, 1, len, len * 2 - 1);
v = t[num[v]].ne;
}
if (ans == INF && color[0]) ans = 0;
if (ans == INF)
cout << -1;
else
cout << ans + 1;
cout << '\n';
}
else
{
color[v] = 1 - color[v];
if (v)
{
int len = t[num[v]].len;
t[num[v]].update(ind[v] + len, 1, len, len * 2 - 1);
}
}
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8Y3N0ZGlvPgojaW5jbHVkZSA8bWF0aC5oPgojaW5jbHVkZSA8YWxnb3JpdGhtPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8c3RyaW5nLmg+CiNpbmNsdWRlIDxzZXQ+CiNpbmNsdWRlIDxtYXA+CiNpbmNsdWRlIDxiaXRzZXQ+CiNpbmNsdWRlIDx0aW1lLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgp0eXBlZGVmIHZlY3RvciA8aW50PiB2aTsKC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W50IGogPSAwOyBqIDwgKGludCl0W2ldLm5vZGUuc2l6ZSgpOyBqKyspCi8vCQkJY291dCA8PCB0W2ldLm5vZGVbal0gPDwgIiAiOwovLwkJY291dCA8PCBlbmRsOwovLwl9Ci8vCWZvciAoaW50IGkgPSAwOyBpIDwgbjsgaSsrKQovLwkJY291dCA8PCBpbmRbaV0gPDwgZW5kbDsKCS8vcmV0dXJuIDA7Cglmb3IgKGludCBpID0gMDsgaSA8IHE7IGkrKykKCXsKCQlpbnQgdHlwZSA9IF90eXBlW2ldOwoJCWludCB2ID0gX3ZbaV07CgkJdi0tOwoJCWlmICh0eXBlKQoJCXsKCQkJaW50IGxlbiA9IHRbbnVtW3ZdXS5sZW47CgkJCWludCBhbnMgPSBJTkY7CgkJCXdoaWxlICh2ICYmIGFucyA9PSBJTkYpCgkJCXsKCQkJCWFucyA9IHRbbnVtW3ZdXS5maW5kKGluZFt2XSArIGxlbiwgbGVuICogMiAtIDEsIDEsIGxlbiwgbGVuICogMiAtIDEpOwoJCQkJdiA9IHRbbnVtW3ZdXS5uZTsKCQkJfQoJCSAgICBpZiAoYW5zID09IElORiAmJiBjb2xvclswXSkgYW5zID0gMDsKCQkJaWYgKGFucyA9PSBJTkYpCgkJCQljb3V0IDw8IC0xOwoJCQllbHNlCgkJCQljb3V0IDw8IGFucyArIDE7CgkJCWNvdXQgPDwgJ1xuJzsKCQl9CgkJZWxzZQoJCXsKCQkJY29sb3Jbdl0gPSAxIC0gY29sb3Jbdl07CgkJCWlmICh2KQoJCQl7CgkJCQlpbnQgbGVuID0gdFtudW1bdl1dLmxlbjsKCQkJCXRbbnVtW3ZdXS51cGRhdGUoaW5kW3ZdICsgbGVuLCAxLCBsZW4sIGxlbiAqIDIgLSAxKTsKCQkJfQoJCQkJCgkJfQoJfQoJcmV0dXJuIDA7Cn0KCg==
Main.java:1: error: illegal character: '#'
#include <iostream>
^
Main.java:1: error: class, interface, or enum expected
#include <iostream>
^
Main.java:2: error: illegal character: '#'
#include <cstdio>
^
Main.java:3: error: illegal character: '#'
#include <math.h>
^
Main.java:4: error: illegal character: '#'
#include <algorithm>
^
Main.java:5: error: illegal character: '#'
#include <vector>
^
Main.java:6: error: illegal character: '#'
#include <string.h>
^
Main.java:7: error: illegal character: '#'
#include <set>
^
Main.java:8: error: illegal character: '#'
#include <map>
^
Main.java:9: error: illegal character: '#'
#include <bitset>
^
Main.java:10: error: illegal character: '#'
#include <time.h>
^
Main.java:13: error: class, interface, or enum expected
typedef vector <int> vi;
^
Main.java:15: error: illegal character: '#'
#define ll long long
^
Main.java:15: error: class, interface, or enum expected
#define ll long long
^
Main.java:16: error: illegal character: '#'
#define pb push_back
^
Main.java:17: error: illegal character: '#'
#define ld double
^
Main.java:18: error: illegal character: '#'
#define pld pair <ld, ld>
^
Main.java:19: error: illegal character: '#'
#define pll pair <ll, ll>
^
Main.java:20: error: illegal character: '#'
#define fi first
^
Main.java:21: error: illegal character: '#'
#define pii pair <int, int>
^
Main.java:22: error: illegal character: '#'
#define se second
^
Main.java:23: error: illegal character: '#'
#define mp make_pair
^
Main.java:26: error: class, interface, or enum expected
const int INF = 1000 * 1000;
^
Main.java:28: error: class, interface, or enum expected
int cnt[MAXN], pr[MAXN], ind[MAXN], num[MAXN];
^
Main.java:29: error: class, interface, or enum expected
int cntt = 0;
^
Main.java:30: error: class, interface, or enum expected
vector <int> g[MAXN];
^
Main.java:31: error: class, interface, or enum expected
bool is_heavy(int v, int pr)
^
Main.java:34: error: class, interface, or enum expected
}
^
Main.java:38: error: illegal start of type
public:
^
Main.java:38: error: ';' expected
public:
^
Main.java:39: error: <identifier> expected
int len;
^
Main.java:98: error: class, interface, or enum expected
tree t[MAXN];
^
Main.java:99: error: class, interface, or enum expected
int dfs(int v, int _pr)
^
Main.java:102: error: class, interface, or enum expected
cnt[v] = 1;
^
Main.java:103: error: class, interface, or enum expected
for (int i = 0; i < (int)g[v].size(); i++)
^
Main.java:103: error: class, interface, or enum expected
for (int i = 0; i < (int)g[v].size(); i++)
^
Main.java:103: error: class, interface, or enum expected
for (int i = 0; i < (int)g[v].size(); i++)
^
Main.java:106: error: class, interface, or enum expected
return cnt[v];
^
Main.java:107: error: class, interface, or enum expected
}
^
Main.java:113: error: class, interface, or enum expected
for (int i = 0; i < (int)g[v].size(); i++)
^
Main.java:113: error: class, interface, or enum expected
for (int i = 0; i < (int)g[v].size(); i++)
^
Main.java:113: error: class, interface, or enum expected
for (int i = 0; i < (int)g[v].size(); i++)
^
Main.java:118: error: class, interface, or enum expected
find_path(g[v][i]);
^
Main.java:119: error: class, interface, or enum expected
}
^
Main.java:122: error: class, interface, or enum expected
}
^
Main.java:126: error: class, interface, or enum expected
int _type[MAXN * 4], _v[MAXN * 4];
^
Main.java:127: error: class, interface, or enum expected
int main()
^
Main.java:134: error: class, interface, or enum expected
int n, q;
^
Main.java:135: error: class, interface, or enum expected
cin >> n >> q;
^
Main.java:136: error: class, interface, or enum expected
for (int i = 0; i < n - 1; i++)
^
Main.java:136: error: class, interface, or enum expected
for (int i = 0; i < n - 1; i++)
^
Main.java:136: error: class, interface, or enum expected
for (int i = 0; i < n - 1; i++)
^
Main.java:139: error: class, interface, or enum expected
cin >> a >> b;
^
Main.java:140: error: class, interface, or enum expected
a--, b--;
^
Main.java:141: error: class, interface, or enum expected
g[a].pb(b);
^
Main.java:142: error: class, interface, or enum expected
g[b].pb(a);
^
Main.java:143: error: class, interface, or enum expected
}
^
Main.java:144: error: class, interface, or enum expected
for (int i = 0; i < q; i++)
^
Main.java:144: error: class, interface, or enum expected
for (int i = 0; i < q; i++)
^
Main.java:146: error: class, interface, or enum expected
dfs(0, 0);
^
Main.java:147: error: class, interface, or enum expected
find_path(0);
^
Main.java:157: error: class, interface, or enum expected
for (int i = 0; i < q; i++)
^
Main.java:157: error: class, interface, or enum expected
for (int i = 0; i < q; i++)
^
Main.java:157: error: class, interface, or enum expected
for (int i = 0; i < q; i++)
^
Main.java:160: error: class, interface, or enum expected
int v = _v[i];
^
Main.java:161: error: class, interface, or enum expected
v--;
^
Main.java:162: error: class, interface, or enum expected
if (type)
^
Main.java:165: error: class, interface, or enum expected
int ans = INF;
^
Main.java:166: error: class, interface, or enum expected
while (v && ans == INF)
^
Main.java:169: error: class, interface, or enum expected
v = t[num[v]].ne;
^
Main.java:170: error: class, interface, or enum expected
}
^
Main.java:172: error: class, interface, or enum expected
if (ans == INF)
^
Main.java:174: error: class, interface, or enum expected
else
^
Main.java:176: error: class, interface, or enum expected
cout << '\n';
^
Main.java:177: error: class, interface, or enum expected
}
^
Main.java:181: error: class, interface, or enum expected
if (v)
^
Main.java:184: error: class, interface, or enum expected
t[num[v]].update(ind[v] + len, 1, len, len * 2 - 1);
^
Main.java:185: error: class, interface, or enum expected
}
^
Main.java:190: error: class, interface, or enum expected
}
^
79 errors