This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#ifdef Home
#define _GLIBCXX_DEBUG
#endif // Home
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const int N = 100100;
const int LOGN = 17;
vector < int > adj[N];
int up[N][LOGN], depth[N], use[N], T, ans, C[N];
void dfs(int v, int p) {
depth[v] = depth[p] + 1;
up[v][0] = p;
for(int i = 1; i < LOGN; ++ i) {
up[v][i] = up[ up[v][i - 1] ][i - 1];
}
for(auto &u : adj[v]) {
if(u != p) {
dfs(u, v);
}
}
}
int slow_LCA(int a, int b) {
if(depth[a] < depth[b]) {
swap(a, b);
}
ans += use[a] != T;
ans += use[b] != T;
use[a] = T;
use[b] = T;
for(; depth[a] > depth[b];) {
a = up[a][0];
if(use[a] == T) {
return b;
}
++ ans;
use[a] = T;
}
for(; a != b;) {
a = up[a][0];
b = up[b][0];
if(a == b) {
use[b] = T;
++ ans;
return a;
}
ans += 2;
use[a] = T;
use[b] = T;
}
return b;
}
main() {
#ifdef Home
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif // Home
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m, q;
cin >> n >> m >> q;
for(int i = 1, a, b; i < n; ++ i) {
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
for(int i = 1; i <= m; ++ i) {
cin >> C[i];
}
dfs(1, 1);
for(int l, r, m; q --> 0;) {
cin >> l >> r;
ans = 1;
m = C[l ++];
use[m] = ++ T;
for(; l <= r; ++ l) {
m = slow_LCA(m, C[l]);
}
cout << ans << '\n';
}
}
Compilation message (stderr)
tourism.cpp:62:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
62 | main() {
| ^~~~
# | 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... |