#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
int n, a, b;
// Adjacency list representation of the tree
vector<int> graph[200001];
// Size of subtree rooted at each node
int subtree[200001];
// ans stores the final answer, bit is for Fenwick tree
// (Binary Indexed Tree)
ll ans = 0, bit[200001];
// Maximum depth encountered during centroid decomposition
int mx_depth;
// To mark nodes as processed during centroid decomposition
bool processed[200001];
// Function to calculate subtree sizes rooted at each node
int get_subtree_sizes(int node, int parent = 0)
{
subtree[node] = 1;
for (int i : graph[node])
if (!processed[i] && i != parent)
subtree[node] += get_subtree_sizes(i, node);
return subtree[node];
}
// Function to find the centroid of a subtree
int get_centroid(int desired, int node, int parent = 0)
{
for (int i : graph[node])
if (!processed[i] && i != parent
&& subtree[i] >= desired)
return get_centroid(desired, i, node);
return node;
}
// Function to update Fenwick tree
void update(int pos, ll val)
{
for (pos++; pos <= n; pos += pos & -pos)
bit[pos] += val;
}
// Function to query Fenwick tree for sum in a range
ll query(int l, int r)
{
ll ans = 0;
for (r++; r; r -= r & -r)
ans += bit[r];
for (; l; l -= l & -l)
ans -= bit[l];
return ans;
}
// Function to count paths in a subtree within a certain
// depth range
void get_cnt(int node, int parent, bool filling,
int depth = 1)
{
// Depth exceeds limit, stop recursion
if (depth > b)
return;
// Update maximum depth encountered
mx_depth = max(mx_depth, depth);
// Fill the Fenwick tree if filling is true
if (filling)
update(depth, 1);
// Otherwise, query the Fenwick tree for counts
else
ans += query(max(0, a - depth), b - depth);
for (int i : graph[node])
if (!processed[i] && i != parent)
get_cnt(i, node, filling, depth + 1);
}
// Centroid decomposition of the tree
void centroid_decomp(int node = 1)
{
// Find centroid of the subtree
int centroid
= get_centroid(get_subtree_sizes(node) >> 1, node);
// Mark centroid as processed
processed[centroid] = true;
// Initialize maximum depth encountered to 0
mx_depth = 0;
// Iterate through centroid's neighbors
for (int i : graph[centroid])
if (!processed[i]) {
// Count paths passing through each neighbor
get_cnt(i, centroid, false);
// Update Fenwick tree for each neighbor
get_cnt(i, centroid, true);
}
// Clear Fenwick tree values after processing
for (int i = 1; i <= mx_depth; i++)
update(i, -query(i, i));
// Recursively decompose remaining subtrees
for (int i : graph[centroid])
if (!processed[i])
centroid_decomp(i);
}
int main()
{
// Static input
n = 5;
a = 1; // or K1
b = 4; // or K2
vector<vector<int> > edges
= { { 1, 2 }, { 2, 3 }, { 3, 4 }, { 3, 5 } };
for (int i = 0; i < edges.size(); i++) {
int u = edges[i][0];
int v = edges[i][1];
graph[u].push_back(v);
graph[v].push_back(u);
}
// Initialize Fenwick tree with root node
update(0, 1);
// Perform centroid decomposition
centroid_decomp();
// Output the final answer
cout << ans;
return 0;
}