1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
#include <cstdio> #include <cmath> #include <cctype> #include <cstring> #include <vector> #include <queue>
#define debug(...) fprintf(stderr, __VA_ARGS__) #define Debug(s) debug("The message in line %d, Function %s: %s\n", __LINE__, __FUNCTION__, s) #define getchar() (ipos == iend and (iend = (ipos = _ibuf) + fread(_ibuf, 1, __bufsize, stdin), ipos == iend) ? EOF : *ipos++) #define putchar(ch) (opos == oend ? fwrite(_obuf, 1, __bufsize, stdout), opos = _obuf : 0, *opos++ = (ch)) #define __bufsize (1 << 21 | 1)
char _ibuf[__bufsize], _obuf[__bufsize], _stk[50]; char *ipos = _ibuf, *iend = _ibuf, *opos = _obuf, *oend = _obuf + __bufsize, *stkpos = _stk;
struct END { ~END() { fwrite(_obuf, 1, opos - _obuf, stdout); } } __;
inline int read() { register int x = 0, flag = 1; register char ch; while (!isdigit(ch = getchar())) if (ch == '-') flag = -1; while (x = x * 10 + (ch & 15), isdigit(ch = getchar())); return x * flag; }
template <typename _INT> inline void write(_INT x) { if (x < 0) putchar('-'), x = -x; while (*++stkpos = x % 10 ^ 48, x /= 10, x); while (stkpos != _stk) putchar(*stkpos--); }
template <typename _Tp> inline bool Chkmax(_Tp& x, const _Tp& y) { return x < y ? x = y, true : false; }
template <typename _Tp> inline bool Chkmin(_Tp& x, const _Tp& y) { return x > y ? x = y, true : false; }
const int maxN = 1e5 + 5; const int maxM = 1e5 + 5;
int n, m, len; int dep[maxN], fa[maxN], top[maxN], son[maxN], size[maxN]; int mindis[maxN], dis[maxN]; int opt[maxM], node[maxM], id[maxM]; std::vector<int> G[maxN]; std::queue<int> q;
void DFS1(int u) { size[u] = 1; for (int v : G[u]) { if (fa[u] == v) continue; fa[v] = u, dep[v] = dep[u] + 1; DFS1(v); size[u] += size[v]; if (size[son[u]] < size[v]) son[u] = v; } }
void DFS2(int u) { if (son[u]) top[son[u]] = top[u], DFS2(son[u]); for (int v : G[u]) if (!top[v]) top[v] = v, DFS2(v); }
int Dis(int u, int v) { int res = dep[u] + dep[v]; while (top[u] != top[v]) { if (dep[top[u]] < dep[top[v]]) std::swap(u, v); u = fa[top[u]]; } if (dep[u] > dep[v]) std::swap(u, v); return res - (dep[u] << 1); }
int main() { #ifndef ONLINE_JUDGE freopen("wib.in", "r", stdin); freopen("wib.out", "w", stdout); #endif n = read(), m = read(); len = sqrt(n * log(n) / log(2)); for (int i = 2; i <= n; ++i) { int u = read(), v = read(); G[u].push_back(v), G[v].push_back(u); } DFS1(1), memcpy(mindis + 1, dep + 1, n * sizeof(int)); top[1] = 1, DFS2(1); for (int i = 1; i <= m; ++i) opt[i] = read(), node[i] = read(); id[0] = 1; for (int i = 1; i <= m; ++i) id[i] = (i - 1) / len + 1; for (int i = 1; i <= m; ++i) { if (id[i] != id[i - 1]) { memset(dis + 1, 0x3F, n * sizeof(int)); for (int j = i - len; j < i; ++j) if (opt[j] == 1) q.push(node[j]), dis[node[j]] = 0; while (!q.empty()) { int u = q.front(); q.pop(); for (int v : G[u]) if (Chkmin(dis[v], dis[u] + 1)) q.push(v); } for (int i = 1; i <= n; ++i) Chkmin(mindis[i], dis[i]); } if (opt[i] == 2) { int ans = mindis[node[i]]; for (int j = i - 1; j and id[j] == id[i]; --j) if (opt[j] == 1) Chkmin(ans, Dis(node[j], node[i])); write(ans), putchar('\n'); } } return 0; }
|