# Cut Tree

### Problem Statement :

```Given a tree T with n nodes, how many subtrees (T') of T have at most K edges connected to (T - T')?

Input Format

The first line contains two integers n and K followed by n-1 lines each containing two integers a & b denoting that there's an edge between a & b.

Constraints

1 <= K <= n <= 50
Every node is indicated by a distinct number from 1 to n.

Output Format

A single integer which denotes the number of possible subtrees.```

### Solution :

```                            ```Solution in C :

In C++ :

#include<iostream>
#include<string.h>
using namespace std;

#define MAXN 100
int first[MAXN], next1[MAXN], v[MAXN];
int edgeindex;
int n, k;
long long dp[MAXN][MAXN];
bool vis[MAXN];

{
v[edgeindex] = b;
next1[edgeindex] = first[a];
first[a] = edgeindex++;
}

void solve(int cur, int root)
{
if(vis[cur])
return;

for(int i = first[cur]; i != -1; i = next1[i])
{
if(v[i] == root)
continue;
solve(v[i], cur);
for(int j = k; j > 0; --j)
{
int tmp = 0;
for(int x = j; x > 0; x--)
{
dp[cur][j] += dp[cur][j-x] * dp[v[i]][x];
}
dp[cur][j] += dp[cur][j-1];
}
}
return;
}

int main()
{
while(cin>>n>>k)
{
edgeindex = 0;
memset(first, -1, sizeof(first));
memset(dp, 0, sizeof(dp));
memset(vis, false, sizeof(vis));
int a, b;
for(int i = 1; i < n; ++i)
{
cin>>a>>b;
}

for(int i = 1; i <= n; ++i)
{
dp[i] = 1;
}

solve(1, -1);

long long ans = 0;
for(int i = 1; i <= n; ++i)
{
for(int j = 0; j < k; ++j)
{
ans += dp[i][j];
}
}
ans += dp[k];
cout<<ans+1<<endl;
}
}

In Java :

import java.io.BufferedInputStream;
import java.util.ArrayList;
import java.util.Scanner;

public class Solution {
static long rooted[][] = new long ;
static int size[] = new int;
static int outDegree[] = new int;
static ArrayList<Integer> tree[] = new ArrayList;
static int n;
static int k;

static void dfs(int u, int p) {
size[u] = 1;

for (int v : tree[u]) {
if (v == p) continue;
dfs(v, u);

outDegree[u]++;
size[u] += size[v];
}
//////////////////////////////////////////////////////////////////
long pre[] = rooted[u];
rooted[u] = 1;
for (int v : tree[u]) {
if (v == p) continue;

rooted[u] = new long;
for (int i=0; i<=k; i++) {
if (i >= 1) rooted[u][i] += pre[i - 1];
for (int j=0; j<=i; j++)
rooted[u][i] += pre[i - j] * rooted[v][j];
}
pre = rooted[u];
}
}

public static void main(String[] args) {
Scanner cin = new Scanner(new BufferedInputStream(System.in));
n = cin.nextInt();
k = cin.nextInt();
for (int i=0; i<n; i++) tree[i] = new ArrayList<Integer>();
for (int i=0; i<n-1; i++) {
int u = cin.nextInt(); u--;
int v = cin.nextInt(); v--;
}

dfs(0, -1);

long ans = 0;
for (int i=0; i<n; i++)
if (i == 0)
for (int j=0; j<=k; j++) ans += rooted[i][j];
else
for (int j=1; j<=k; j++) ans += rooted[i][j - 1];
System.out.println(ans + 1);

//		for (int i=0; i<n; i++) {
//			for (int j=0; j<=k; j++)
//				System.out.print(rooted[i][j] + " ");
//			System.out.println();
//		}
//		System.out.println();
}
}

In C :

#include <stdio.h>
#include <stdlib.h>
void dfs(int x,int parent);
int table={0},c={0},p,q={0},N;
long long dp1={0},dp2={0},temp,ans;
int main(){
int K,x,y,i,j,k;
scanf("%d%d",&N,&K);
for(i=0;i<N-1;i++){
scanf("%d%d",&x,&y);
table[x-1][y-1]=-1;
table[y-1][x-1]=-1;
}
dfs(0,0);
for(i=0;i<N;i++)
if(!c[i])
q[++q]=i;
while(q){
x=q[q--];
dp1[x]=dp2[x]=1;
for(i=0;i<N;i++)
if(table[x][i]==1){
for(j=1;j<=K;j++)
temp[j]=0;
for(j=1;j<=K;j++){
dp1[x][j]+=dp1[i][j]+dp2[i][j-1];
for(k=0;k<=j;k++)
if(k==1)
temp[j]+=(dp2[i][k]+1)*dp2[x][j-k];
else
temp[j]+=dp2[i][k]*dp2[x][j-k];
}
for(j=1;j<=K;j++)
dp2[x][j]=temp[j];
}
if(x){
c[p[x]]--;
if(!c[p[x]])
q[++q]=p[x];
}
}
for(i=0,ans=0;i<=K;i++)
ans+=dp1[i]+dp2[i];
printf("%lld",ans);
return 0;
}
void dfs(int x,int parent){
int i;
p[x]=parent;
for(i=0;i<N;i++)
if(table[x][i] && i!=parent){
table[x][i]+=2;
c[x]++;
dfs(i,x);
}
return;
}

In Python3 :

line = input().split()
n = int(line)
K = int(line)

for i in range(n):

for i in range(n - 1):
line = input().split()
x = int(line)
y = int(line)

conlst =  * (K + 1)
dislst =  * (K + 1)
conlst = 1
if chld != par:
tcl, tdl = dfs(chld, node, adjlst, K)
# print(str(tcl))
# print(str(tdl))
dislst += tdl
tcl2 =  * (K + 1)
for i in range(K):
dislst[i + 1] += tcl[i] + tdl[i + 1]
tcl2[i + 1] += conlst[i]
for i in range(K + 1):
for j in range(K + 1):
if i + j <= K:
tcl2[i + j] += tcl[i] * conlst[j]
conlst = list(tcl2)
return conlst, dislst

conlst, dislst = dfs(1, 0, adjlst, K)
# print(str(conlst))
# print(str(dislst))
ans = sum(conlst) + sum(dislst) + 1
print(str(ans))```
```

## Lazy White Falcon

White Falcon just solved the data structure problem below using heavy-light decomposition. Can you help her find a new solution that doesn't require implementing any fancy techniques? There are 2 types of query operations that can be performed on a tree: 1 u x: Assign x as the value of node u. 2 u v: Print the sum of the node values in the unique path from node u to node v. Given a tree wi

## Ticket to Ride

Simon received the board game Ticket to Ride as a birthday present. After playing it with his friends, he decides to come up with a strategy for the game. There are n cities on the map and n - 1 road plans. Each road plan consists of the following: Two cities which can be directly connected by a road. The length of the proposed road. The entire road plan is designed in such a way that if o

## Heavy Light White Falcon

Our lazy white falcon finally decided to learn heavy-light decomposition. Her teacher gave an assignment for her to practice this new technique. Please help her by solving this problem. You are given a tree with N nodes and each node's value is initially 0. The problem asks you to operate the following two types of queries: "1 u x" assign x to the value of the node . "2 u v" print the maxim

## Number Game on a Tree

Andy and Lily love playing games with numbers and trees. Today they have a tree consisting of n nodes and n -1 edges. Each edge i has an integer weight, wi. Before the game starts, Andy chooses an unordered pair of distinct nodes, ( u , v ), and uses all the edge weights present on the unique path from node u to node v to construct a list of numbers. For example, in the diagram below, Andy

## Heavy Light 2 White Falcon

White Falcon was amazed by what she can do with heavy-light decomposition on trees. As a resut, she wants to improve her expertise on heavy-light decomposition. Her teacher gave her an another assignment which requires path updates. As always, White Falcon needs your help with the assignment. You are given a tree with N nodes and each node's value Vi is initially 0. Let's denote the path fr

## Library Query

A giant library has just been inaugurated this week. It can be modeled as a sequence of N consecutive shelves with each shelf having some number of books. Now, being the geek that you are, you thought of the following two queries which can be performed on these shelves. Change the number of books in one of the shelves. Obtain the number of books on the shelf having the kth rank within the ra