# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
27171 | 2017-07-09T19:00:44 Z | topology | 트리 (KOI16_tree) | C++14 | 789 ms | 54928 KB |
#include <bits/stdc++.h> #ifdef TOPOLOGY #define debug(...) fprintf(stderr, __VA_ARGS__) #else #define debug(...) #endif using namespace std; struct fenwick { vector<int> tree; int n; fenwick(int n_){ n = n_; tree.resize(n+1); } void upd(int i){ assert(i); while (i <= n){ tree[i]++; i += i&-i; } } int sum(int i){ int res = 0; while (i){ res += tree[i]; i -= i&-i; } return res; } }; int n, q, par[200003][20], depth[200003], hidx[200003], subsz[200003]; vector<int> graph[200003]; vector<vector<int> > heavy; vector<fenwick> trees; int dfs(int node, int p){ for (int i=1; i<20; i++){ par[node][i] = par[par[node][i-1]][i-1]; } subsz[node] = 1; for (auto &nxt : graph[node]){ if (p == nxt) continue; depth[nxt] = depth[node] + 1; subsz[node] += dfs(nxt, node); } return subsz[node]; } int lca(int u, int v){ if (depth[u] > depth[v]) swap(u, v); int dif = depth[v] - depth[u]; for (int i=0; (1<<i)<=dif; i++){ if (dif & (1<<i)){ v = par[v][i]; } } if (u == v) return u; for (int i=19; i>=0; i--){ if (par[u][i] != par[v][i]){ u = par[u][i]; v = par[v][i]; } } return par[u][0]; } void hld(){ queue<int> q; for (auto &nxt : graph[1]){ q.push(nxt); } while (!q.empty()){ int node = q.front(); q.pop(); for (auto &nxt : graph[node]){ if (nxt == par[node][0]) continue; q.push(nxt); } if (subsz[node]*2 >= subsz[par[node][0]] && par[node][0] != 1){ int ppi = hidx[par[node][0]]; heavy[ppi].push_back(node); hidx[node] = ppi; } else { hidx[node] = (int) heavy.size(); vector<int> tmp = {par[node][0], node}; heavy.push_back(tmp); } } } int query(int u, int v){ if (u == v) return 0; int uidx = hidx[u], vidx = hidx[v]; if (uidx == vidx){ int rt = heavy[uidx][0]; return trees[uidx].sum(depth[v]-depth[rt]) - trees[vidx].sum(depth[u]-depth[rt]); } return query(u, heavy[vidx][0]) + trees[vidx].sum(depth[v]-depth[heavy[vidx][0]]); } void cut(int u){ if (u == 1) return; int uidx = hidx[u]; trees[uidx].upd(depth[u] - depth[heavy[uidx][0]]); } int main(){ scanf("%d%d", &n, &q); for (int i=0; i<n-1; i++){ int u; scanf("%d", &u); par[i+2][0] = u; graph[u].push_back(i+2); } dfs(1, -1); hld(); for (auto &v : heavy){ fenwick fen(v.size()); trees.push_back(fen); } for (int i=0; i<q; i++){ int b, c, d; scanf("%d%d%d", &b, &c, &d); int l = lca(b, c); if (!d){ if (query(l, c) || query(l, b)) puts("NO"); else puts("YES"); } else { if (query(l, c) || query(l, b)){ puts("NO"); cut(c); } else { puts("YES"); cut(b); } } } return 0; }
Compilation message
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 0 ms | 24684 KB | Output is correct |
2 | Correct | 0 ms | 24684 KB | Output is correct |
3 | Correct | 0 ms | 24684 KB | Output is correct |
4 | Correct | 0 ms | 24684 KB | Output is correct |
5 | Correct | 3 ms | 24684 KB | Output is correct |
6 | Correct | 0 ms | 24684 KB | Output is correct |
7 | Correct | 0 ms | 24684 KB | Output is correct |
8 | Correct | 3 ms | 24684 KB | Output is correct |
9 | Correct | 0 ms | 24684 KB | Output is correct |
10 | Correct | 3 ms | 24684 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 0 ms | 24684 KB | Output is correct |
2 | Correct | 0 ms | 24684 KB | Output is correct |
3 | Correct | 0 ms | 24684 KB | Output is correct |
4 | Correct | 0 ms | 24684 KB | Output is correct |
5 | Correct | 3 ms | 24684 KB | Output is correct |
6 | Correct | 0 ms | 24684 KB | Output is correct |
7 | Correct | 0 ms | 24684 KB | Output is correct |
8 | Correct | 3 ms | 24684 KB | Output is correct |
9 | Correct | 0 ms | 24684 KB | Output is correct |
10 | Correct | 3 ms | 24684 KB | Output is correct |
11 | Correct | 3 ms | 24832 KB | Output is correct |
12 | Correct | 0 ms | 24828 KB | Output is correct |
13 | Correct | 0 ms | 24832 KB | Output is correct |
14 | Correct | 0 ms | 24828 KB | Output is correct |
15 | Correct | 3 ms | 24828 KB | Output is correct |
16 | Correct | 3 ms | 24832 KB | Output is correct |
17 | Correct | 0 ms | 24832 KB | Output is correct |
18 | Correct | 0 ms | 24828 KB | Output is correct |
19 | Correct | 3 ms | 24832 KB | Output is correct |
20 | Correct | 0 ms | 24828 KB | Output is correct |
21 | Correct | 0 ms | 24828 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 0 ms | 24684 KB | Output is correct |
2 | Correct | 0 ms | 24684 KB | Output is correct |
3 | Correct | 0 ms | 24684 KB | Output is correct |
4 | Correct | 0 ms | 24684 KB | Output is correct |
5 | Correct | 3 ms | 24684 KB | Output is correct |
6 | Correct | 0 ms | 24684 KB | Output is correct |
7 | Correct | 0 ms | 24684 KB | Output is correct |
8 | Correct | 3 ms | 24684 KB | Output is correct |
9 | Correct | 0 ms | 24684 KB | Output is correct |
10 | Correct | 3 ms | 24684 KB | Output is correct |
11 | Correct | 3 ms | 24832 KB | Output is correct |
12 | Correct | 0 ms | 24828 KB | Output is correct |
13 | Correct | 0 ms | 24832 KB | Output is correct |
14 | Correct | 0 ms | 24828 KB | Output is correct |
15 | Correct | 3 ms | 24828 KB | Output is correct |
16 | Correct | 3 ms | 24832 KB | Output is correct |
17 | Correct | 0 ms | 24832 KB | Output is correct |
18 | Correct | 0 ms | 24828 KB | Output is correct |
19 | Correct | 3 ms | 24832 KB | Output is correct |
20 | Correct | 0 ms | 24828 KB | Output is correct |
21 | Correct | 0 ms | 24828 KB | Output is correct |
22 | Correct | 116 ms | 24948 KB | Output is correct |
23 | Correct | 116 ms | 24948 KB | Output is correct |
24 | Correct | 146 ms | 24948 KB | Output is correct |
25 | Correct | 109 ms | 24948 KB | Output is correct |
26 | Correct | 109 ms | 24948 KB | Output is correct |
27 | Correct | 119 ms | 24948 KB | Output is correct |
28 | Correct | 119 ms | 24948 KB | Output is correct |
29 | Correct | 159 ms | 24948 KB | Output is correct |
30 | Correct | 119 ms | 24948 KB | Output is correct |
31 | Correct | 119 ms | 24948 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 0 ms | 24684 KB | Output is correct |
2 | Correct | 0 ms | 24684 KB | Output is correct |
3 | Correct | 0 ms | 24684 KB | Output is correct |
4 | Correct | 0 ms | 24684 KB | Output is correct |
5 | Correct | 3 ms | 24684 KB | Output is correct |
6 | Correct | 0 ms | 24684 KB | Output is correct |
7 | Correct | 0 ms | 24684 KB | Output is correct |
8 | Correct | 3 ms | 24684 KB | Output is correct |
9 | Correct | 0 ms | 24684 KB | Output is correct |
10 | Correct | 3 ms | 24684 KB | Output is correct |
11 | Correct | 579 ms | 42860 KB | Output is correct |
12 | Correct | 639 ms | 42856 KB | Output is correct |
13 | Correct | 576 ms | 42860 KB | Output is correct |
14 | Correct | 659 ms | 42856 KB | Output is correct |
15 | Correct | 626 ms | 42860 KB | Output is correct |
16 | Correct | 653 ms | 42856 KB | Output is correct |
17 | Correct | 429 ms | 42856 KB | Output is correct |
18 | Correct | 419 ms | 42860 KB | Output is correct |
19 | Correct | 379 ms | 42860 KB | Output is correct |
20 | Correct | 389 ms | 42860 KB | Output is correct |
21 | Correct | 576 ms | 42856 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 0 ms | 24684 KB | Output is correct |
2 | Correct | 0 ms | 24684 KB | Output is correct |
3 | Correct | 0 ms | 24684 KB | Output is correct |
4 | Correct | 0 ms | 24684 KB | Output is correct |
5 | Correct | 3 ms | 24684 KB | Output is correct |
6 | Correct | 0 ms | 24684 KB | Output is correct |
7 | Correct | 0 ms | 24684 KB | Output is correct |
8 | Correct | 3 ms | 24684 KB | Output is correct |
9 | Correct | 0 ms | 24684 KB | Output is correct |
10 | Correct | 3 ms | 24684 KB | Output is correct |
11 | Correct | 3 ms | 24832 KB | Output is correct |
12 | Correct | 0 ms | 24828 KB | Output is correct |
13 | Correct | 0 ms | 24832 KB | Output is correct |
14 | Correct | 0 ms | 24828 KB | Output is correct |
15 | Correct | 3 ms | 24828 KB | Output is correct |
16 | Correct | 3 ms | 24832 KB | Output is correct |
17 | Correct | 0 ms | 24832 KB | Output is correct |
18 | Correct | 0 ms | 24828 KB | Output is correct |
19 | Correct | 3 ms | 24832 KB | Output is correct |
20 | Correct | 0 ms | 24828 KB | Output is correct |
21 | Correct | 0 ms | 24828 KB | Output is correct |
22 | Correct | 116 ms | 24948 KB | Output is correct |
23 | Correct | 116 ms | 24948 KB | Output is correct |
24 | Correct | 146 ms | 24948 KB | Output is correct |
25 | Correct | 109 ms | 24948 KB | Output is correct |
26 | Correct | 109 ms | 24948 KB | Output is correct |
27 | Correct | 119 ms | 24948 KB | Output is correct |
28 | Correct | 119 ms | 24948 KB | Output is correct |
29 | Correct | 159 ms | 24948 KB | Output is correct |
30 | Correct | 119 ms | 24948 KB | Output is correct |
31 | Correct | 119 ms | 24948 KB | Output is correct |
32 | Correct | 579 ms | 42860 KB | Output is correct |
33 | Correct | 639 ms | 42856 KB | Output is correct |
34 | Correct | 576 ms | 42860 KB | Output is correct |
35 | Correct | 659 ms | 42856 KB | Output is correct |
36 | Correct | 626 ms | 42860 KB | Output is correct |
37 | Correct | 653 ms | 42856 KB | Output is correct |
38 | Correct | 429 ms | 42856 KB | Output is correct |
39 | Correct | 419 ms | 42860 KB | Output is correct |
40 | Correct | 379 ms | 42860 KB | Output is correct |
41 | Correct | 389 ms | 42860 KB | Output is correct |
42 | Correct | 576 ms | 42856 KB | Output is correct |
43 | Correct | 653 ms | 54928 KB | Output is correct |
44 | Correct | 619 ms | 54928 KB | Output is correct |
45 | Correct | 633 ms | 54924 KB | Output is correct |
46 | Correct | 653 ms | 54928 KB | Output is correct |
47 | Correct | 789 ms | 54924 KB | Output is correct |
48 | Correct | 236 ms | 54608 KB | Output is correct |
49 | Correct | 336 ms | 54876 KB | Output is correct |
50 | Correct | 309 ms | 54880 KB | Output is correct |
51 | Correct | 419 ms | 35244 KB | Output is correct |