fork download
  1. // ROOT : DRAGON3012009
  2. #include <bits/stdc++.h>
  3. #define ll long long
  4. #define ld long double
  5. #define el "\n"
  6. #define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  7. #define __ROOT__ int main()
  8. #pragma GCC optimize("O2")
  9. #define FOR(i,l,r) for(int i = l ; i <= r ; i ++)
  10. #define FORD(i,r,l) for(int i = r ; i >= l ; i --)
  11. #define REP(i, a ) for(int i = 0 ; i < a ; i ++ )
  12. #define fi first
  13. #define se second
  14. #define M 1000000007
  15. #define MAXN 1000001
  16. #define INF (1ll<<30)
  17. #define BLOCK_SIZE 425
  18. #define MAX_NODE 1001001
  19. #define LOG 19
  20. #define ALPHA_SIZE 26
  21. #define BASE 311
  22. #define NAME "file"
  23. #define compare(v) sort((v).begin(), (v).end()); (v).erase(unique((v).begin(), (v).end()), (v).end());
  24. using namespace std;
  25. const ll MOD[] = {(ll)1e9 + 2277, (ll)1e9 + 5277, (ll)1e9 + 8277, (ll)1e9 + 9277, (ll) 1e9 + 7 };
  26. const ll NMOD = 1;
  27. const int dx[] = {-1, 0, 1,0};
  28. const int dy[] = {0, 1, 0, -1};
  29. //**Variable**//
  30. ll n, q;
  31. vector<ll> adj[MAXN];
  32. vector<ll> used, temp;
  33. ll d[MAXN];
  34. ll tin[MAXN], tout[MAXN], euler[2 * MAXN], high[2 * MAXN];
  35. ll st[2 * MAXN][LOG + 1];
  36. ll time_dfs = 0;
  37. //**Struct**//
  38.  
  39. //**Function**//
  40. template<class X, class Y>
  41. bool minimize(X &x, const Y &y) {
  42. return x > y ? x = y, 1 : 0;
  43. }
  44. template<class X, class Y>
  45. bool maximize(X &x, const Y &y) {
  46. return x < y ? x = y, 1 : 0;
  47. }
  48.  
  49. void dfs_lca(ll u, ll p, ll h = 0) {
  50. tin[u] = ++time_dfs;
  51. euler[time_dfs] = u;
  52. high[time_dfs] = h;
  53. for (ll v : adj[u]) if (v != p) {
  54. dfs_lca(v, u, h + 1);
  55. euler[++time_dfs] = u;
  56. high[time_dfs] = h;
  57. }
  58. tout[u] = time_dfs;
  59. }
  60.  
  61. void build_sparse() {
  62. FOR(i, 1, time_dfs) st[i][0] = i;
  63. FOR(j, 1, LOG) {
  64. for (int i = 1; i + (1 << j) - 1 <= time_dfs; ++i) {
  65. int l = st[i][j - 1];
  66. int r = st[i + (1 << (j - 1))][j - 1];
  67. st[i][j] = (high[l] < high[r] ? l : r);
  68. }
  69. }
  70. }
  71.  
  72. ll get_lca(ll u, ll v) {
  73. ll l = tin[u], r = tin[v];
  74. if (l > r) swap(l, r);
  75. int k = __lg(r - l + 1);
  76. int lidx = st[l][k], ridx = st[r - (1 << k) + 1][k];
  77. return euler[high[lidx] < high[ridx] ? lidx : ridx];
  78. }
  79.  
  80. ll get_dist(ll u, ll v) {
  81. ll lca = get_lca(u, v);
  82. return high[tin[u]] + high[tin[v]] - 2 * high[tin[lca]];
  83. }
  84.  
  85. void bfs() {
  86. for (ll u : temp) used.push_back(u);
  87. temp.clear();
  88.  
  89. memset(d, 0, sizeof d);
  90. queue<ll> q;
  91. for (ll u : used) {
  92. q.push(u);
  93. d[u] = 1;
  94. }
  95.  
  96. while (!q.empty()) {
  97. ll u = q.front(); q.pop();
  98. for (ll v : adj[u]) {
  99. if (!d[v]) {
  100. d[v] = d[u] + 1;
  101. q.push(v);
  102. }
  103. }
  104. }
  105. }
  106.  
  107. void init() {
  108. cin >> n >> q;
  109. FOR(i, 1, n - 1) {
  110. ll x, y; cin >> x >> y;
  111. adj[x].push_back(y);
  112. adj[y].push_back(x);
  113. }
  114. dfs_lca(1, 0);
  115. build_sparse();
  116. temp.push_back(1);
  117. bfs();
  118. }
  119. void solve() {
  120. FOR(i, 1, q) {
  121. ll t, u;
  122. cin>> t>> u;
  123. if (t == 1) {
  124. temp.push_back(u);
  125. if ((int)temp.size() >= BLOCK_SIZE) bfs();
  126. } else {
  127. ll ans = d[u] - 1 ;
  128. for (ll v : temp) minimize(ans, get_dist(u, v));
  129. cout << ans << el;
  130. }
  131. }
  132. }
  133.  
  134. __ROOT__ {
  135. // freopen("NAME.inp", "r", stdin);
  136. // freopen("NAME.out", "w", stdout);
  137. fast;
  138. int t = 1; // cin >> t;
  139. while (t--) {
  140. init();
  141. solve();
  142. }
  143. }
  144.  
Success #stdin #stdout 0.02s 44452KB
stdin
5 4
1 2
2 3
2 4
4 5
2 1
2 5
1 2
2 5
stdout
0
3
2