제출 #1100239

#제출 시각아이디문제언어결과실행 시간메모리
1100239vjudge1Birthday gift (IZhO18_treearray)C++17
0 / 100
1 ms592 KiB
#include <iostream> #include <vector> #include <algorithm> using namespace std; const int N = 100; int a[N], n, m, q; vector<int> g[N]; int depth[N], parent[N][20]; void dfs(int v, int p) { parent[v][0] = p; for (int i = 1; i < 20; i++) { parent[v][i] = parent[parent[v][i-1]][i-1]; } for (int to : g[v]) { if (to != p) { depth[to] = depth[v] + 1; dfs(to, v); } } } int lca(int u, int v) { if (depth[u] < depth[v]) swap(u, v); for (int i = 19; i >= 0; i--) { if (depth[u] - (1 << i) >= depth[v]) { u = parent[u][i]; } } if (u == v) return u; for (int i = 19; i >= 0; i--) { if (parent[u][i] != parent[v][i]) { u = parent[u][i]; v = parent[v][i]; } } return parent[u][0]; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> m >> q; for (int i = 1; i < n; i++) { int u, v; cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } for (int i = 1; i <= m; i++) { cin >> a[i]; } depth[1] = 0; dfs(1, 0); while (q--) { int type; cin >> type; if (type == 1) { int pos, v; cin >> pos >> v; a[pos] = v; } else { int l, r, v; cin >> l >> r >> v; bool found = false; for (int x = l; x <= r; x++) { for (int y = x; y <= r; y++) { if (lca(a[x], a[y]) == v) { cout << x << " " << y << "\n"; found = true; break; } } if (found) break; } if (!found) { cout << "-1 -1\n"; } } } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...