【BZOJ2152/国家集训队】聪聪可可

题目:BZOJ2152

解析:

  点分治/树形DP。
  很明显的点分治,主要说下统计答案的问题。
  假设两点的距离记为两点到当前重心的深度之和,那么对于经过重心的合法点对可以表示为:
   s u m [ 0 ] ∗ ( s u m [ 0 ] − 1 ) + s u m [ 0 ] + s u m [ 1 ] ∗ s u m [ 2 ] ∗ 2 sum[0]*(sum[0]-1)+sum[0]+sum[1]*sum[2]*2 sum[0](sum[0]1)+sum[0]+sum[1]sum[2]2
   s u m [ 0 ] , s u m [ 1 ] , s u m [ 2 ] sum[0],sum[1],sum[2] sum[0],sum[1],sum[2]表示模 3 3 3 0 , 1 , 2 0,1,2 0,1,2的点数量。因为 ( u , v ) , ( v , u ) (u,v),(v,u) (u,v),(v,u)是算两次的, ( u , u ) (u,u) (u,u)是算一次的。

代码:

#include <bits/stdc++.h>
using namespace std;

const int Max=20005;
int n,m,s,rt,ans,sum,tot;
int first[Max],size[Max],num[Max],vis[Max],d[Max],ss[5];
struct shu{int to,next,len;}e[Max<<1];

inline int get_int()
{
	int x=0,f=1;char c;
	for(c=getchar();(!isdigit(c))&&(c!='-');c=getchar());
	if(c=='-') f=-1,c=getchar();
	for(;isdigit(c);c=getchar()) x=(x<<3)+(x<<1)+c-'0';
	return x*f;
}

inline void build(int x,int y,int z)
{
	e[++s].next=first[x],first[x]=s,e[s].to=y,e[s].len=z;
	e[++s].next=first[y],first[y]=s,e[s].to=x,e[s].len=z;
}
inline void getp(int p,int fa)
{
	size[p]=1;int maxx=0;
	for(int u=first[p];u;u=e[u].next)
	{
	  int to=e[u].to;
	  if(vis[to]||to==fa) continue;
	  getp(to,p),size[p]+=size[to],maxx=max(maxx,size[to]);
	}
	maxx=max(maxx,sum-size[p]);
	if(maxx<m) m=maxx,rt=p;
}
inline void getd(int p,int fa)
{
	ss[d[p]]++;
	for(int u=first[p];u;u=e[u].next)
	{
	  int to=e[u].to;
	  if(to==fa||vis[to]) continue;
	  d[to]=(d[p]+e[u].len)%3,getd(to,p);
	}
}
inline int calc(int p,int w)
{
	d[p]=w,ss[2]=ss[1]=ss[0]=0,getd(p,0);
	return ss[1]*ss[2]*2+ss[0]*ss[0];
}
inline void dfs(int p)
{
	ans+=calc(p,0),vis[p]=1;
	for(int u=first[p];u;u=e[u].next)
	{
	  int to=e[u].to;
	  if(vis[to]) continue;
	  ans-=calc(to,e[u].len),m=1e9,sum=size[to],getp(to,rt),dfs(rt);
	}
}

inline void init()
{
	n=get_int();
	for(int i=1;i<n;i++)
	{
	  int x=get_int(),y=get_int(),z=get_int()%3;
	  build(x,y,z);
	}
}
inline void solve()
{
	m=1e9,sum=n,getp(1,0),dfs(rt);
	cout<<(ans/__gcd(ans,n*n))<<"/"<<(n*n/__gcd(ans,n*n));
}

int main()
{
	init();
	solve();
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值