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>
using namespace std;
const int nx=1e5+5;
int n, m, q, u, v, on[nx], x, dp[nx];
vector<pair<int, int>> d[nx], t[2*nx];
void dfs(int u, int p, int l)
{
dp[u]=1;
for (auto [v, idx]:d[u])
{
if (v==p||t[idx].empty()) continue;
auto itr=lower_bound(t[idx].begin(), t[idx].end(), make_pair(l, INT_MAX));
if (itr==t[idx].begin()) continue;
dfs(v, u, min(prev(itr)->second, l));
dp[u]+=dp[v];
}
}
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n>>m>>q;
for (int i=1; i<n; i++) cin>>u>>v, d[u].push_back({v, i}), d[v].push_back({u, i});
for (int i=1; i<=m; i++)
{
cin>>x;
if (on[x]) t[x].push_back({on[x], i}), on[x]=0;
else on[x]=i;
}
for (int i=1; i<n; i++) if (on[i]) t[i].push_back({on[i], m});
cin>>x;
dfs(x, x, m);
cout<<dp[x];
}
/*
5 6 3
1 2
1 3
2 4
2 5
1
2
1
4
4
3
4
*/
# | 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... |