这题和poj 1741是一模一样的

但是1741能AC的代码,在这里却是TLE,暂时没看出哪里出现了问题。。

AC代码:

#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <cctype>
#include <vector>
#include <iterator>
#include <set>
#include <map>
#include <sstream>
using namespace std; #define mem(a,b) memset(a,b,sizeof(a))
#define pf printf
#define sf scanf
#define spf sprintf
#define pb push_back
#define debug printf("!\n")
#define MAXN 40000+5
#define MAX(a,b) a>b?a:b
#define blank pf("\n")
#define LL long long
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
#define pqueue priority_queue
#define INF 0x3f3f3f3f #define ls (rt<<1)
#define rs (rt<<1|1) int n,m,k; int ans,root,tot,ptr = ,K,son[MAXN],head[MAXN],f[MAXN],dist[MAXN],d[MAXN],sum,vis[MAXN]; struct node{int y,next,v;}tree[MAXN<<]; void add(int u,int v,int w) {tree[ptr].y=v;tree[ptr].v=w;tree[ptr].next=head[u];head[u]=ptr++;} void getroot(int x,int fa)
{
son[x] = ;
f[x] = ;
for(int i=head[x];i!=-;i=tree[i].next)
{
int y = tree[i].y;
if(y == fa || vis[y]) continue;
getroot(y,x);
son[x] += son[y];
f[x] = max(f[x] , son[y]);
}
f[x] = max(f[x] , sum - son[x]);
if(f[x] < f[root]) root = x;
} void getdis(int x,int fa)
{
if(d[x] <= K) dist[tot++]=d[x];
for(int i=head[x];i!=-;i=tree[i].next)
{
int y = tree[i].y;
if(y == fa || vis[y]) continue;
d[y] = d[x] + tree[i].v;
getdis(y,x);
}
} int cal(int x,int now)
{
d[x]=now;
tot = ;
getdis(x,);
sort(dist,dist+tot);
int all = ,left=,right = tot-;
while(left<right)
{
if(dist[left]+dist[right] <= K) {all+=right-left;left++;}
else right--;
}
return all;
}
void solve(int x)
{
ans+=cal(x,);
vis[x] = ;
for(int i=head[x];i!=-;i=tree[i].next)
{
int y = tree[i].y;
if(vis[y]) continue;
ans-=cal(y,tree[i].v);
sum = son[y];
root = ;
getroot(y,root);
solve(root);
}
}
void init()
{
mem(head,-);
ptr = ;
ans = root = ;
mem(vis,);
sum=n;
f[]=INF;
} int main()
{
while(~sf("%d%d",&n,&m))
{
init();
for(int i=;i<n;i++)
{
int x,y,z;char ch[];
sf("%d%d%d%s",&x,&y,&z,ch);
add(x,y,z);
add(y,x,z);
}
sf("%d",&K);
getroot(,);
solve(root);
pf("%d\n",ans);
} }

1741可AC,这题TLE

#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <cctype>
#include <vector>
#include <iterator>
#include <set>
#include <map>
#include <sstream>
using namespace std; #define mem(a,b) memset(a,b,sizeof(a))
#define pf printf
#define sf scanf
#define spf sprintf
#define pb push_back
#define debug printf("!\n")
#define MAXN 40000+5
#define MAX(a,b) a>b?a:b
#define blank pf("\n")
#define LL long long
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
#define pqueue priority_queue
#define INF 0x3f3f3f3f #define ls (rt<<1)
#define rs (rt<<1|1) int n,m,k; int ptr = ,head[MAXN],vis[MAXN],f[MAXN],d[MAXN]; int ans,tot,rt,sum,son[MAXN],dis[MAXN],mu[MAXN]; struct node
{
int y,val,next;
}tree[MAXN<<]; void init()
{
mem(tree,);
mem(head,-);
mem(vis,);
mem(dis,);
ans = ;
ptr = ;
sum = n;
f[] = INF;
} void add(int fa,int son,int val)
{
tree[ptr].y = son;
tree[ptr].val = val;
tree[ptr].next = head[fa];
head[fa] = ptr++;
} void getroot(int root,int fa)
{
son[root] = ;
f[root] = ;
for(int i=head[root];i!=-;i=tree[i].next)
{
int y = tree[i].y;
if(vis[y] || y == fa) continue;
getroot(y,root);
son[root] += son[y];
f[root] = max(son[y],f[root]);
}
f[root] = max(f[root],sum-son[root]);
if(f[root]<f[rt]) rt = root;
} void getdis(int root,int fa)
{
if(d[root]<=k) dis[tot++] = d[root];
for(int i=head[root];i!=-;i=tree[i].next)
{
int y = tree[i].y;
if(vis[y] || y == fa) continue;
d[y] = d[root] + tree[i].val;
getdis(y,root);
}
} int getcnt(int root,int now)
{
d[root] = now;
tot = ;
getdis(root,);
sort(dis,dis+tot);
int left =,right = tot-,ans=;
while(left<right)
{
if(dis[left]+dis[right]<=k)
{
ans+= right-left;
left++;
}
else right--;
}
return ans;
} void solve(int root)
{
//pf("rt%d\n",rt);
ans+=getcnt(root,);
vis[root] = ;
for(int i=head[root];i!=-;i=tree[i].next)
{
int y = tree[i].y;
if(vis[y]) continue;
ans-=getcnt(y,tree[i].val);
sum = son[y];
rt = ;
getroot(y,rt);
solve(rt);
}
} int main()
{
int i,j,t,kase=;
while(~sf("%d%d",&n,&m),n+m)
{
init();
int x,y,z;
char ch[];
for(i=;i<n;i++)
{
sf("%d%d%d%s",&x,&y,&z,ch);
add(x,y,z);
add(y,x,z);
}
sf("%d",&k);
getroot(,);
solve(rt);
pf("%d\n",ans);
}
return ;
}

以及:http://blog.csdn.net/woshi250hua/article/details/7723400

我看起来是一样的,就是不知道TLE的原因。。

poj 1987 节点距离小于等于K(树DP)的更多相关文章

  1. POJ1741--Tree (树的点分治) 求树上距离小于等于k的点对数

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 12276   Accepted: 3886 Description ...

  2. poj 1741 两点距离小于K(树DP)

    http://blog.csdn.net/woshi250hua/article/details/7723400 求两点间距离小于等于k的方案数 理一下思路: 求通过点A与另一点连接符合条件的个数 = ...

  3. [51NOD1405] 树的距离之和(树DP)

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1405 (1)我们给树规定一个根.假设所有节点编号是0-(n-1 ...

  4. hdu 2196 叶子节点最长距离(树DP)

    http://www.cnblogs.com/kuangbin/archive/2012/08/28/2659915.html 求每个节点到叶子节点的最长距离 需要保存每个节点到叶子节点距离的最大值和 ...

  5. poj1741 树上距离小于等于k的对数 点分治 入门题

    #include <iostream> #include <stdio.h> #include <string.h> #include <algorithm& ...

  6. 863. All Nodes Distance K in Binary Tree 到制定节点距离为k的节点

    [抄题]: We are given a binary tree (with root node root), a target node, and an integer value K. Retur ...

  7. POJ 1987 BZOJ 3365 Distance Statistics 树的分治(点分治)

    题目大意:(同poj1741,刷一赠一系列) CODE: #include <cstdio> #include <cstring> #include <iostream& ...

  8. 【点分治】【路径小于等于k的条数】【路径恰好等于k是否存在】

    POJ1741:Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 29574   Accepted: 9915 Des ...

  9. Leetcode——863.二叉树中所有距离为 K 的结点

    给定一个二叉树(具有根结点 root), 一个目标结点 target ,和一个整数值 K . 返回到目标结点 target 距离为 K 的所有结点的值的列表. 答案可以以任何顺序返回. 示例 1: 输 ...

随机推荐

  1. LAMP实战之构建博客网站

    1.首先检查LAMP环境 [root@cairui htdocs]# ps -ef | grep httpd php Mar03 ? :: /opt/apache2.2.34/bin/httpd -k ...

  2. 最短路【bzoj1726】: [Usaco2006 Nov]Roadblocks第二短路

    1726: [Usaco2006 Nov]Roadblocks第二短路 Description 贝茜把家搬到了一个小农场,但她常常回到FJ的农场去拜访她的朋友.贝茜很喜欢路边的风景,不想那么快地结束她 ...

  3. git学习--远程分支删除

    查看远程分支 git branch -r  使用下面两条命令来删除远程分支 git branch -r -d origin/branch-name git push origin :branch-na ...

  4. P2913 [USACO08OCT]车轮旋转Wheel Rotation

    传送门 初始状态是 0,如果有 1 的连接,0 就变 1,如果还有 1 的连接,1 就变 0,如果是 0 的连接就不变 所以就是把答案异或上所有连接,不用考虑顺序,反正最终是一样的 #include& ...

  5. Codeforces Round #335 (Div. 2) C

                                                                   C. Sorting Railway Cars time limit pe ...

  6. UESTC 1437

    LCA模板题 随便找的倍增模板... #include<bits/stdc++.h> using namespace std; const int maxn = 1e5+11; int t ...

  7. lintcode - 房屋染色

    class Solution { public: /* * @param costs: n x 3 cost matrix * @return: An integer, the minimum cos ...

  8. WCF的三种模式

    WCF通信的3种模式 1.正常模式:客户端调取接口->等待服务响应->接受响应->执行客户端后面代码(wcf服务有入参,有返回值) 2.数据报模式:客户端调取接口->不等待响应 ...

  9. 缓存方案:本地guavaCache, 远程redis?

    线程内部缓存:a. 局部变量HashMap, 方法间传递  b. 使用ThreadLocal 本地缓存:单jvm内共享 可以使用(Concurrent)HashMap自己实现,也可以使用GuavaCa ...

  10. 16-----BBS论坛

    BBS论坛(十六) 16.登录功能完成 (1)front/forms.py class SigninForm(BaseForm): telephone = StringField(validators ...