点分治裸题23333这个题如果K很大的话就不能用依次处理每棵子树做了。(数组存不开2333)

这个就只能是总的依次减去每棵子树里的。

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<iostream>
 4 #include<cstdlib>
 5 #include<cstring>
 6 #include<cmath>
 7 #define N 100005
 8 #define M 1000005
 9 #define LL long long
10 #define inf 0x3f3f3f3f
11 using namespace std;
12 inline int ra()
13 {
14     int x=0,f=1; char ch=getchar();
15     while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();}
16     while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();}
17     return x*f;
18 }
19 int n,K,cnt,sum,ans,root;
20 int head[10005],deep[10005],d[10005],f[10005],son[10005];
21 bool vis[10005];
22 struct data{int to,next,v;}e[20005];
23 void insert(int x, int y, int w)
24 {
25     e[++cnt].next=head[x]; e[cnt].to=y; e[cnt].v=w; head[x]=cnt;
26 }
27 void getroot(int x, int fa)
28 {
29     son[x]=1; f[x]=0;
30     for (int i=head[x];i;i=e[i].next)
31     {
32         if (e[i].to==fa || vis[e[i].to]) continue;
33         getroot(e[i].to,x);
34         son[x]+=son[e[i].to];
35         f[x]=max(f[x],son[e[i].to]);
36     }
37     f[x]=max(f[x],sum-son[x]);
38     if (f[x]<f[root]) root=x;
39 }
40 void getdeep(int x, int fa)
41 {
42     deep[++deep[0]]=d[x];
43     for (int i=head[x];i;i=e[i].next)
44     {
45         if (e[i].to==fa || vis[e[i].to]) continue;
46         d[e[i].to]=d[x]+e[i].v;
47         getdeep(e[i].to,x);
48     }
49 }
50 int cal(int x, int now)
51 {
52     d[x]=now; deep[0]=0;
53     getdeep(x,0);
54     sort(deep+1,deep+deep[0]+1);
55     int t=0,l,r;
56     for (l=1,r=deep[0];l<r;)
57     {
58         if (deep[l]+deep[r]<=K) t+=r-l,l++;
59         else r--;
60     }
61     return t;
62 }
63 void work(int x)
64 {
65     ans+=cal(x,0); vis[x]=1;
66     for (int i=head[x];i;i=e[i].next)
67     {
68         if (vis[e[i].to]) continue;
69         ans-=cal(e[i].to,e[i].v);
70         sum=son[e[i].to];
71         root=0;
72         getroot(e[i].to,root);
73         work(root);
74     }
75 }
76 int main()
77 {
78     while (1)
79     {
80         ans=0,root=0; cnt=0;
81         memset(vis,0,sizeof(vis));
82         memset(head,0,sizeof(head));
83         n=ra(); K=ra(); if (n==0) break;
84         for (int i=1; i<n; i++)
85         {
86             int x=ra(),y=ra(),w=ra();
87             insert(x,y,w); insert(y,x,w);
88         }
89         sum=n; f[0]=inf;
90         getroot(1,0);
91         work(root);
92         cout<<ans<<endl;
93     }
94     return 0;
95 }

转载于:https://www.cnblogs.com/ccd2333/p/6419284.html

poj 1741 Tree相关推荐

  1. [POJ 1741] Tree

    Link: POJ 1741 传送门 Solution: 此题的难点在于点分治上的统计 如果依然采取每棵子树的结果与之前所有子树的结果合并的方式会比较麻烦 同时复杂度可能超过$O(n*log(n))$ ...

  2. POJ 1741 Tree(树的点分治)

    题目链接:http://poj.org/problem?id=1741 题意:给出一棵树,定义dis(u,v)为两个节点之间的距离.dis(u,v)<=K时成(u,v)是合法的.问有多少个合法的 ...

  3. POJ 1741 Tree(树分治)

    去网上搜题解大多数都是说论文,搜了论文看了看,写的确实挺好,直接复制过来. 不过代码中有些细节还是要注意的,参考这篇http://blog.sina.com.cn/s/blog_6d5aa19a010 ...

  4. POJ 1741 Tree(点分治)

    题意 一棵 \(n\) 个节点的树,求两点间距离不超过 \(k\) 的点对对数. 思路 点分治模板题,点分治一般用来解决树上的路径问题,核心在于找出重心,算经过重心的合法路径,然后以重心把树劈成若干个 ...

  5. POJ - 1741 Tree(点分治模板题)

    题目链接:点击查看 题目大意:给出一棵 n 个节点的树,现在定义 dis( x , y ) 为点 x 和点 y 之间的路径长度,现在问 dis ( x , y ) <= k 的点对有多少 题目分 ...

  6. POJ 1741 Tree 树分治

    题意: 给出一颗有\(n (n \leq 10^4)\)个节点的树,和一个\(k\).统计有多少个点对\(u, \, v(u \neq v)\)满足\(u\)到\(v\)的最短距离不超过\(k\). ...

  7. poj 1145 Tree Summing

    // poj 1145 Tree Summing /* 交了好多遍才过,注意几点: 1.输入可能有负数:2.叶子节点判断要准确,是两个子节点都没有的节点:3.空树答案为no */ #include & ...

  8. 【POJ - 1741】Tree(树分治,容斥,点分治,模板题)

    题干: Give a tree with n vertices,each edge has a length(positive integer less than 1001). Define dist ...

  9. POJ 1741 树分治

    题目链接[http://poj.org/problem?id=1741] 题意: 给出一颗树,然后寻找点对(u,v)&&dis[u][v] < k的对数. 题解: 这是一个很经典 ...

最新文章

  1. 如果要将对象用作Map中的key,需要注意什么
  2. 下载参考序列后字母转换小写转大写
  3. 用python处理excel数据的优势-python处理excel的优势是什么
  4. poj 3459(背包问题)
  5. python元胞自动机模拟交通_大师兄带你复现 -gt; 难度超高的二维CA元胞自动机模型...
  6. android黑色半透明dialog背景,Android开发中Dialog半透明背景消失
  7. Android之解决PC浏览器访问手机服务端取assets目录下的文件页面显示不出来问题
  8. struts2获取请求参数的三种方式及传递给JSP参数的方式
  9. 第十三期:消灭 Java 代码的“坏味道”
  10. 多数人读研或者更高学位,不过是学历军备竞赛中不得已的选择
  11. pyqt 界面关闭信号_PyQt从类(子窗口)发送信号返回到MainWindow(类)
  12. C# 字体大小 字体宽度 字体高度
  13. PAT Basic Level 1072 开学寄语 (20 分)
  14. 高仿小米商城项目,我爱了!
  15. Excel快速填充空白单元格内容为上一行的内容
  16. html js打印页边距,js 设立网页打印的页眉页脚和页边距
  17. unity gizmo绘制圆形帮助调试
  18. wxpython问卷调查界面_7步教你搭建智能问卷调查系统
  19. 数据要素的性质、定价及配置
  20. 麒麟kylin3安装字体

热门文章

  1. windows下查看当前进程,杀掉进程等
  2. 入职阿里啦!java面试技巧之不要给自己挖坑实战干货
  3. 【迁移学习(Transfer L)全面指南】迁移学习中的bottlenecks(瓶颈层)
  4. 【PAT (Advanced Level) Practice】1113 Integer Set Partition (25 分)
  5. 【Linux入门到精通系列讲解】.bashrc 文件作用
  6. 【深度学习入门到精通系列】R2 Unet解释
  7. python【力扣LeetCode算法题库】169 多数元素
  8. 前端cookie 放到请求头_ajax请求携带cookie和自定义请求头header(跨域和同域)
  9. 浅析日常网站建设中运营与优化的工作重点
  10. 学计算机买笔记本是i5 i7,i7不一定比i5好!懂电脑的人选择买i5,而不是i7,究竟怎么回事?...