树分治--点分树模板

存个板子,内容以后补

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+50;
int ecnt=-1,head[maxn];
int son[maxn],siz[maxn];
int tot,n,root;
bool vis[maxn];
struct mint
{
	int nxt,v,w;	
}e[maxn<<1];
struct node
{
	vector<int> anc;
	vector<int> child;	
}nd[maxn];
inline void addline(int u,int v,int w)
{
	e[++ecnt].nxt=head[u];
	e[ecnt].v=v;
	e[ecnt].w=w;
	head[u]=ecnt;	
}

void getG(int now,int fa)
{
	siz[now]=1;
	son[now]=0;
	for(int i=head[now];~i;i=e[i].nxt)
	{
		int v=e[i].v;
		if(v==fa || vis[v]) continue;
		getG(v,now);
		siz[now]+=siz[v];
		son[now]=max(son[now],siz[v]);
	}
	son[now]=max(son[now],tot-siz[now]);
	if(son[now]<maxp)
	{
		maxp=son[now];
		root=now;	
	}
}

void build(int now,ntot)
{
	tot=ntot;
	maxp=0x7f7f7f7f;
	getG(now,0);
	int g=root;
	vis[g]=1;
	for(int i=head[g];~i;i=e[i].nxt)
	{
		int v=e[i].v;
		if(vis[g]) continue;
		int tmp=build(v,ntot-son[v]);
		nd[tmp].fa=now;
		nd[now].child.push_back(tmp);
	}
	return g;
}

int main()
{
	memset(head,-1,sizeof(head));
	scanf("%d",&n);
	for(int i=1;i<=n;++i) 
	{
		int u,v,w;
		scanf("%d%d%d",&u,&v,&w);
		addline(u,v,w);
		addline(v,u,w);	
	}
	int virtroot=build(1,n); 
	return 0;	
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值