/*ㅤ∧_∧
( ・∀・)
( つ┳⊃
ε (_)へ⌒ヽフ
( ( ・ω・)
◎―◎ ⊃ ⊃
BePhuongSuperSuwi
From TK4 - CHT
ㅤㅤ/ ⌒\____
/・ ) \
/ノへ ノ /|
ノ \\ |/_/_/*/
#include<bits/stdc++.h>
#define task "main"
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define endl '\n'
#define int long long
#define pb push_back
#define fi first
#define se second
#define ii pair<int,int>
#define iii pair<int,ii>
#define iv pair<ii, ii>
#define base 341
#define MASK(i) (1ll << i)
#define oo 1e18
#define isOn(x,i) ((x) & MASK(i))
#define bitOn(x,i) ((x) | MASK(i))
#define bitOff(x,i) ((x) & ~MASK(i))
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define __lcm(a,b) (1ll * ((a) / __gcd((a), (b))) * (b))
using namespace std;
//using namespace __gnu_pbds;
const int maxn = 1e5 + 5;
const int lg = 18;
int n, m, test, tin[maxn], timer, c[maxn], h[maxn], par[maxn][lg + 2];
vector <int> g[maxn];
void dfs(int u, int p) {
tin[u] = ++timer;
for (int x : g[u]) {
if (x == p) continue;
h[x] = h[u] + 1;
par[x][0] = u;
dfs(x, u);
}
}
int lca(int u, int v) {
if (h[u] < h[v]) swap(u,v);
for (int i = lg; i >= 0; i--) {
if ((h[u] - h[v]) >= MASK(i)) u = par[u][i];
}
if (u == v) return u;
for (int i = lg; i >= 0; i--) {
if (par[u][i] != par[v][i]) {
u = par[u][i];
v = par[v][i];
}
}
return par[u][0];
}
void prelca() {
dfs(1, -1);
for (int j = 1; j <= lg; j++) {
for (int i = 1; i <= n; i++) {
par[i][j] = par[par[i][j-1]][j-1];
}
}
}
int dist(int l, int r) {
return h[l] + h[r] - 2 * h[lca(l, r)];
}
main() {
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
if(fopen(task".inp","r")) {
freopen(task".inp","r",stdin);
freopen(task".out","w",stdout);
}
cin >> n >> m >> test;
for (int i = 1; i < n; i++) {
int l, r;
cin >> l >> r;
g[l].pb(r); g[r].pb(l);
}
prelca();
for (int i = 1; i <= m; i++) cin >> c[i];
while(test--) {
int l, r;
cin >> l >> r;
vector <ii> v;
for (int i = l; i <= r; i++) {
v.pb({tin[c[i]], c[i]});
}
sort(v.begin(), v.end());
int sum = 0;
for (int i = 0; i < v.size(); i++) {
sum += dist(v[i].se, v[(i + 1) % v.size()].se);
}
cout << sum / 2 + 1 << endl;
}
}
Compilation message (stderr)
tourism.cpp:79:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
79 | main() {
| ^~~~
tourism.cpp: In function 'int main()':
tourism.cpp:82:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
82 | freopen(task".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
tourism.cpp:83:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
83 | freopen(task".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# | 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... |