이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <vector>
#include <algorithm>
#include <cassert>
#include <cstring>
#warning That's the baby, that's not my baby
typedef long long ll;
const int NMAX = 100'000;
const int LGMAX = 18;
std::vector<int> g[NMAX + 1];
int parent[NMAX + 1];
int tin[NMAX + 1], tout[NMAX + 1], timer;
int jump[LGMAX + 1][NMAX + 1];
void dfs(int u) {
tin[u] = ++timer;
for (const auto &v : g[u]) {
if (v != parent[u]) {
parent[v] = u;
dfs(v);
}
}
tout[u] = ++timer;
}
bool ancestor(int u, int v) {
return tin[u] <= tin[v] && tout[v] <= tout[u];
}
int lca(int u, int v) {
if (ancestor(u, v)) {
return u;
}
for (int k = LGMAX; k >= 0; k--) {
if (jump[k][u] != 0 && !ancestor(jump[k][u], v)) {
u = jump[k][u];
}
}
return jump[0][u];
}
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(0);
int n, m, q;
std::cin >> n >> m >> q;
for (int i = 1; i < n; i++) {
int u, v;
std::cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
dfs(1);
for (int i = 1; i <= n; i++) {
jump[0][i] = parent[i];
}
for (int j = 1; j <= LGMAX; j++) {
for (int i = 1; i <= n; i++) {
jump[j][i] = jump[j - 1][jump[j - 1][i]];
}
}
std::vector<int> a(m);
for (auto &x : a) {
std::cin >> x;
}
a.insert(a.begin(), 0);
while (q--) {
int l, r;
std::cin >> l >> r;
std::vector<bool> vis(n + 1, false);
std::vector<int> nodes;
for (int i = l; i <= r; i++) {
nodes.push_back(a[i]);
}
int w = -1;
for (const auto &node : nodes) {
if (w == -1) {
w = node;
} else {
while (!ancestor(w, node)) {
vis[w] = true;
w = parent[w];
}
}
int u = node;
while (u != w) {
vis[u] = true;
u = parent[u];
}
assert(u == w);
vis[u] = true;
}
std::cout << std::count(vis.begin(), vis.end(), true) << '\n';
}
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
tourism.cpp:6:2: warning: #warning That's the baby, that's not my baby [-Wcpp]
6 | #warning That's the baby, that's not my baby
| ^~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |