bzoj2152 (点分治)

题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2152

思路:

要想两点之间距离为3的倍数,那么用t0表示该点距离重心的距离对3取模为0,依此得t1,t2,那么两点之间距离为3的倍数只有三种可能:t1-t2,t2-t1,t0-t0,将所有和重心的具体全部统计好,最后t1*t2*2+t0*t0就是

为3的倍数的点对数量

实现代码:

#include<bits/stdc++.h>
using namespace std;
#define inf 0x7fffffff
const int M = 1e5+;
int siz[M],f[M],head[M],vis[M],ans,cnt,t[M],sum,root,d[M];
struct node{
int to,next,w;
}e[M<<]; void init(){
ans = ;
cnt = ;
memset(vis,,sizeof(vis));
memset(head,,sizeof(head));
} void add(int u,int v,int w){
e[++cnt].to = v; e[cnt].w = w; e[cnt].next = head[u]; head[u] = cnt;
} void get_root(int u,int fa){
siz[u] = ; f[u] = ;
for(int i = head[u];i;i = e[i].next){
int v = e[i].to;
if(v != fa&&!vis[v]){
get_root(v,u);
siz[u] += siz[v];
f[u] = max(f[u],siz[v]);
}
}
f[u] = max(f[u],sum - siz[u]);
if(f[u] < f[root]) root = u;
return ;
} void get_dis(int u,int fa){
t[d[u]]++;
for(int i = head[u];i;i = e[i].next){
int v = e[i].to;
if(v != fa&& !vis[v]){
d[v] = (d[u] + e[i].w)%;
get_dis(v,u);
}
}
return ;
} int cal(int u,int c){
t[] = t[] = t[] = ;
d[u] = c;
get_dis(u,);
return t[]*t[]*+t[]*t[];
} void solve(int v){
ans += cal(v,); vis[v] = ;
for(int i = head[v];i;i = e[i].next){
int v = e[i].to;
if(!vis[v]){
ans -= cal(v,e[i].w);
sum = siz[v];
root = ;
get_root(v,);
solve(root);
}
}
} int main()
{
ios::sync_with_stdio();
cin.tie(); cout.tie();
int m,u,v,w,n;
cin>>n;
init();
for(int i = ;i <= n-;i ++){
cin>>u>>v>>w;
w%=;
add(u,v,w);
add(v,u,w);
}
f[] = inf;
sum = n;
root = ;
get_root(,);
solve(root);
int x = __gcd(ans,n*n);
cout<<ans/x<<"/"<<n*n/x<<endl;
return ;
}
上一篇:Boost 常用的库


下一篇:Linux/Unix shell 自动发送AWR report