给一棵边带权树,问两点之间的距离小于等于K的点对有多少个。

模板题:就是不断找树的重心,然后分开了,分治,至少分成两半,就是上限为log

然后一起统计就ok了

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<algorithm>
#define inf 2000000007
using namespace std; int n,m;
double l,u;
double dis[];int ins[],num[];
int cnt,head[],Next[],rea[];double val[]; void add(int u,int v,double fee)
{
Next[++cnt]=head[u];
head[u]=cnt;
rea[cnt]=v;
val[cnt]=fee;
}
bool Spfa()
{
for (int i=;i<=n+m;i++)
dis[i]=inf,ins[i]=,num[i]=;
queue<int>q;
q.push();dis[]=,ins[]=num[]=;
while(!q.empty())
{
int u=q.front();q.pop();
for (int i=head[u];i!=-;i=Next[i])
{
int v=rea[i];double fee=val[u];
if (dis[v]>dis[u]+fee)
{
dis[v]=dis[u]+fee;
if (!ins[v])
{
ins[v]=;
q.push(v);
num[v]++;
if (num[v]>sqrt(n+m)) return false;
}
}
}
ins[u]=;
}
return true;
}
int main()
{
while(~scanf("%d%d%lf%lf",&n,&m,&l,&u))
{
cnt=;double x;
memset(head,-,sizeof(head));
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
{
scanf("%lf",&x);
add(j+n,i,log(u)-log(x));
add(i,j+n,log(x)-log(l));
}
if(Spfa()) puts("YES");
else puts("NO");
}
}

POJ 1741 Tree【Tree,点分治】的更多相关文章

  1. poj 1741 树的点分治(入门)

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 18205   Accepted: 5951 Description ...

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

    Tree   Description Give a tree with n vertices,each edge has a length(positive integer less than 100 ...

  3. POJ 1741:Tree(树上点分治)

    题目链接 题意 给一棵边带权树,问两点之间的距离小于等于K的点对有多少个. 思路 <分治算法在树的路径问题中的应用> 图片转载于http://www.cnblogs.com/Paul-Gu ...

  4. 【POJ 1741】Tree

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 11570   Accepted: 3626 Description ...

  5. 「POJ 1741」Tree

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

  6. 【POJ 1741】 Tree

    [题目链接] http://poj.org/problem?id=1741 [算法] 点分治 要求距离不超过k的点对个数,不妨将路径分成两类 : 1. 经过根节点 2. 不经过根节点 考虑第1类路径, ...

  7. POJ 1741 树的点分治

    题目大意: 树上找到有多少条路径的边权值和>=k 这里在树上进行点分治,需要找到重心保证自己的不会出现过于长的链来降低复杂度 #include <cstdio> #include & ...

  8. POJ 1741 树上的点分治

    题目大意: 找到树上点对间距离不大于K的点对数 这是一道简单的练习点分治的题,注意的是为了防止点分治时出现最后分治出来一颗子树为一条直线,所以用递归的方法求出最合适的root点 #include &l ...

  9. POJ 1741 树上 点的 分治

    题意就是求树上距离小于等于K的点对有多少个 n2的算法肯定不行,因为1W个点 这就需要分治.可以看09年漆子超的论文 本题用到的是关于点的分治. 一个重要的问题是,为了防止退化,所以每次都要找到树的重 ...

  10. poj 1741 Tree(树的点分治)

    poj 1741 Tree(树的点分治) 给出一个n个结点的树和一个整数k,问有多少个距离不超过k的点对. 首先对于一个树中的点对,要么经过根结点,要么不经过.所以我们可以把经过根节点的符合点对统计出 ...

随机推荐

  1. 用for循环实现的菱形图案

    package com.wh.lingxing; import java.util.Scanner; public class LingXing { public static void main(S ...

  2. DotNteBar 控件操作

    DotNteBar中ComboBoxEx.DroppedDown = true可以不点击该控件就显示其内容.将DropDownStyle属性设为DropDownList下拉框就不能进行编辑只能选择下拉 ...

  3. Angular 组件之间的传值

    第一种方法(传单个或者多个参数): 主页面方法: 先添加引用:private _routes: Router, Details(PBSCode) { this._routes.navigate(['p ...

  4. Node.js(二)常用的系统模块

    http模块 第一章已经介绍了 node.js 的模块都可以传一个回调函数  回调函数支持两个参数  error , data let fs = require('fs'); fs.readFile( ...

  5. Objective - c Foundation 框架详解2

    Objective - c  Foundation 框架详解2 Collection Agency Cocoa provides a number of collection classes such ...

  6. 在SQLServer 2005附加SQLServer 2008数据库异常处理

    远程服务器软件系统不算新,数据库是SQL Server 2005.本地开发基本是用新的软件系统.数据库采用SQL Server 2008. 这样在用远程服务器SQL 2005选择附加SQL 2008的 ...

  7. sql server 2008 r2 无法定位到数据库文件目录

    像这样,选择数据库文件时, 无法定位到文件夹目录,子目录下的都不显示.明明选择的这个文件夹里还有很多子文件夹,却显示不了. 解决方法: 在此文件夹上右击,属性-安全 添加红框中的用户就可以了.

  8. webstorm里直接调用命令行

    写代码写到一半要切换窗口出去敲命令行?webstorm的external tools可以帮你省下一点时间 举例说明,比如我要直接使用npm: ctrl+alt+s打开setting菜单,找到exter ...

  9. STL:set的使用

    关于set set是以特定的顺序存储相异元素的容器. set是关联式容器,C++ STL中标准关联容器set, multiset, map, multimap内部采用的就是一种非常高效的平衡检索二叉树 ...

  10. kafka可视化客户端工具(Kafka Tool)安装

    参考:https://www.cnblogs.com/frankdeng/p/9452982.html