This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define rep(i,m,n) for(int i=(m); i<=(n); i++)
#define reb(i,m,n) for(int i=(m); i>=(n); i--)
#define iter(id, v) for(auto id : v)
#define fs first
#define se second
#define MP make_pair
#define pb push_back
#define bit(msk, i) ((msk >> i) & 1)
#define SZ(v) (ll)v.size()
#define ALL(v) v.begin(),v.end()
using namespace std;
mt19937_64 rd(chrono :: steady_clock :: now ().time_since_epoch().count());
ll Rand (ll l, ll r) { return uniform_int_distribution<ll> (l, r) (rd); }
const int N = 1e6 + 7;
const int Mod = 1e9 + 2022; ///loonf mod sai
const int INF = 1e9;
const ll BASE = 137;
const int szBL = 350;
int n, m, Q;
vector<int> ke[N];
pii qr[N];
int C[N];
int high[N], par[N][25];
void pdfs (int u, int p) {
high[u] = high[p] + 1;
par[u][0] = p;
rep (i, 1, 20) par[u][i] = par[par[u][i - 1]][i - 1];
iter (&v, ke[u]) {
if (v != p) {
pdfs(v, u);
}
}
}
int LCA (int u, int v) {
if (high[u] > high[v]) swap(u, v);
rep (i, 0, 20) if (bit(high[v] - high[u], i)) v = par[v][i];
if (v == u) return v;
reb (i, 20, 0) if (par[u][i] != par[v][i]) v = par[v][i], u = par[u][i];
return par[u][0];
}
namespace sub2 {
int pre[N];
void dfs (int u, int p, int &res) {
iter (&v, ke[u]) {
if (v != p) {
dfs(v, u, res);
pre[u] += pre[v];
}
}
if (pre[u] > 0) ++res;
}
void solution () {
rep (q, 1, Q) {
rep (i, 1, n) pre[i] = 0;
int L = qr[q].fs, R = qr[q].se;
int Lca = C[L];
rep (i, L, R) {
Lca = LCA(Lca, C[i]);
}
rep (i, L, R) {
pre[C[i]]++;
pre[par[Lca][0]]--;
}
int res = 0;
dfs(1, 0, res);
cout << res <<"\n";
}
}
}
void solution() {
cin >> n >> m >> Q;
rep (i, 1, n - 1) {
int u, v;
cin >> u >> v;
ke[u].pb(v);
ke[v].pb(u);
}
rep (i, 1, m) cin >> C[i];
rep (i, 1, Q) cin >> qr[i].fs >> qr[i].se;
pdfs(1, 0);
if (n <= 2000 && m <= 2000 && Q <= 2000) sub2 :: solution();
}
#define file(name) freopen(name".inp","r",stdin); \
freopen(name".out","w",stdout);
int main () {
// file("c");
ios_base :: sync_with_stdio(false); cin.tie(0); cout.tie(0);
int num_Test = 1;
// cin >> num_Test;
while (num_Test--)
solution();
}
/*
no bug +8
chu y break hay return se lam hong logic
xet transition cua i va i + 1
construct ket qua
chu y truong hop : KHONG CO GI
ko làm được
hướng 1: đổi hướng làm
hướng 2: đưa ra nhận xét
2 3 2
0 0
1 0
1 1
0 1
0 2
1 2
0 5
5 0
*/
Compilation message (stderr)
tourism.cpp: In function 'int LCA(int, int)':
tourism.cpp:47:36: warning: suggest parentheses around '-' inside '>>' [-Wparentheses]
47 | rep (i, 0, 20) if (bit(high[v] - high[u], i)) v = par[v][i];
| ~~~~~~~~^~~~~~~~~
tourism.cpp:12:27: note: in definition of macro 'bit'
12 | #define bit(msk, i) ((msk >> i) & 1)
| ^~~
# | 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... |