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 <iostream>
#include <vector>
#define MAX 100000
#define LOG 20
using namespace std;
///ifstream cin ("c.in");
///ofstream cout ("c.out");
vector <int> graph[MAX + 10];
pair <int, int> edges[2 * MAX + 10];
int timer = 1, in[MAX + 10], out[MAX + 10], ancestor[LOG + 10][MAX + 10], tree[MAX + 10], info[MAX + 10], last[MAX + 10], active[MAX + 10];
void update(int pos, int val)
{
for (int i = pos; i <= MAX; i = i + (i & -i))
tree[i] = tree[i] + val;
}
int query(int pos)
{
int ans = 0;
for (int i = pos; i >= 1; i = i - (i & -i))
ans = ans + tree[i];
return ans;
}
void dfs(int node, int parent)
{
ancestor[0][node] = parent;
for (int p = 1; p <= LOG && ancestor[p - 1][node] != 0; p++)
ancestor[p][node] = ancestor[p - 1][ancestor[p - 1][node]];
info[node] = 1;
in[node] = timer;
timer++;
for (auto next : graph[node])
if (next != parent)
dfs(next, node);
out[node] = timer;
}
int findAncestor(int node)
{
for (int p = LOG; p >= 0; p--)
if (ancestor[p][node] != 0 && query(in[ancestor[p][node]]) == query(in[node]))
node = ancestor[p][node];
return node;
}
int main()
{
int n, m, q;
cin >> n >> m >> q;
for (int i = 1; i < n; i++)
{
int x, y;
cin >> x >> y;
graph[x].push_back(y);
graph[y].push_back(x);
edges[i] = {x, y};
}
dfs(1, 0);
for (int i = 1; i <= n; i++)
{
update(in[i], -1);
update(out[i], 1);
}
for (int i = 1; i <= m; i++)
{
int edge;
cin >> edge;
int node1 = edges[edge].first, node2 = edges[edge].second;
if (ancestor[0][node1] == node2)
swap(node1, node2);
if (active[edge] == 1)
{
info[node2] = last[node2] = info[findAncestor(node1)];
update(in[node2], -1);
update(out[node2], 1);
active[edge] = 0;
}
else
{
info[findAncestor(node1)] = info[findAncestor(node1)] + info[node2] - last[node2];
update(in[node2], 1);
update(out[node2], -1);
active[edge] = 1;
}
}
for (int i = 1; i <= q; i++)
{
int node;
cin >> node;
cout << info[findAncestor(node)] << '\n';
}
return 0;
}
# | 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... |