Skip to content

Latest commit

 

History

History
126 lines (122 loc) · 3.22 KB

Fixed-Length_Paths_I.md

File metadata and controls

126 lines (122 loc) · 3.22 KB

Algorithm: Centroid Decomposition

Problem: Cycle Finding

#include <bits/stdc++.h>
using namespace std;
//#include<ext/pb_ds/assoc_container.hpp>
//#include<ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds; // find_by_order, order_of_key
#define ll long long
#define vi vector<int>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vvi vector<vi>
#define vll vector<ll>
#define vvll vector<vector<ll>>
#define vpii vector<pair<int,int>>
#define vpll vector<pair<ll,ll>>
int Set(int N, int pos) {return  N = N | (1<<pos);}
int Reset(int N, int pos) {return  N = N & ~(1<<pos);}
bool Cheek(int N, int pos) {return  (bool)(N & (1<<pos));}
#define least_one_pos(x) __builtin_ffs(x)
#define leading_zeros(x) __builtin_clz(x)
#define tailing_zeros(x) __builtin_ctz(x)
#define num_of_one(x) __builtin_popcount(x)
///............x...........///
#define all(a) a.begin(), a.end()
#define allr(a) a.rbegin(), a.rend()
#define mp(a, b) make_pair(a, b)
#define pb push_back
#define UNIQUE(X) (X).erase(unique(all(X)), (X).end())
#define ft cout << "for test"<<endl;
#define print(v) for (auto it : v)cout << it<<" ";cout << endl;
#define PI acos(-1.0)
#define FIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define t_c int test, cs = 1;cin>>test;while (test--)
///................function.....................///
 
//#define mod 1000000007
//........constant........///
const ll N = 5e4+5;
void file(){
   freopen("input.txt","r",stdin);
   freopen("output.txt","w",stdout);
}
class CentroidDecomposition{
public:
	vector<vector<int>>adj;
	vector<bool>removed;
	vector<int>subtree,parent;
	vector<int>Count;
	int n,root,k,mxd;
	ll ans;
	CentroidDecomposition(int _n, int _k){
		n = _n;
		k = _k;
		root=1;
		Count.resize(n+1);
		adj.resize(n+1);
		removed.resize(n+1);
		subtree.resize(n+1);
		parent.resize(n+1);
		ans=0;
	}
	void readTree(){
		for(int i=1; i<n; i++){
			int a,b;
			cin>>a>>b;
			adj[a].push_back(b);
			adj[b].push_back(a);
		}
		centroid_decomposition(1);
		cout<<ans<<endl;
	}
	int get_subtree_size(int node, int par=-1){
		subtree[node]=1;
		for(auto x:adj[node])
			if(!removed[x] and par!=x)
				subtree[node] += get_subtree_size(x,node);
		return subtree[node];
	}
 
	int get_centroid(int node, int desired, int par=-1){
		for(auto x:adj[node])
			if(!removed[x] and x!=par and desired<=subtree[x])
				return get_centroid(x,desired,node);
		return node;
	}
	void get_cnt(int nd, int par, int d, bool tp){
		if(d>k)return;
		mxd = max(mxd,d);
		if(tp)Count[d]++;
		else ans += Count[k-d];
		for(auto x:adj[nd])
			if(!removed[x] and x!=par)
				get_cnt(x,nd,d+1,tp);
	}
	void centroid_decomposition(int node){
		int centroid = get_centroid(node,get_subtree_size(node)>>1);
		removed[centroid]=true;
		Count[0]=1;
		mxd=0;
		for(auto x:adj[centroid]){
			if(!removed[x]){
				get_cnt(x,centroid,1,0);
				get_cnt(x,centroid,1,1);
			}
		}
		for(int i=1; i<=mxd; i++)Count[i]=0;
		for(auto x:adj[centroid]){
			if(!removed[x])
				centroid_decomposition(x);
		}
	}
};
int main(){
	FIO;
	// file();
	int n,k;
	cin>>n>>k;
	CentroidDecomposition obj(n,k);
	obj.readTree();
}