题意:给你一棵树,每条边有权值,求有没有一条链使得权值和为k

题解:和上一题类似,依旧是树分治,只是我们储存结果的时候是判断加起来为k的点对数,刚开始本来想用map存答案,结果就t了,后来用了vector,数组等各种,最后用数组,绝望的把memset改成for就过了。

学到一个新的点:如果我们调用了m[i](m是map),不管m[i]是不是0,当我们遍历map时就会出现这个数,但是second=0

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<cassert>
#include<iomanip>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define C 0.5772156649
#define pi acos(-1.0)
#define ll long long
#define mod 1000000007
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1 using namespace std;
using namespace __gnu_cxx; const double g=10.0,eps=1e-;
const int N=+,maxn=+,inf=0x3f3f3f; struct edge{
int to,Next,c;
}e[maxn];
int cnt,head[N];
int sz[N],zx[N],dis[N];
int k,son[N],sonsz;
bool vis[N];
void add(int u,int v,int c)
{
e[cnt].to=v;
e[cnt].c=c;
e[cnt].Next=head[u];
head[u]=cnt++;
}
void dfssz(int u,int f,int &sum)
{
sum++;
sz[u]=;
for(int i=head[u];~i;i=e[i].Next)
{
int x=e[i].to;
if(x==f||vis[x])continue;
dfssz(x,u,sum);
sz[u]+=sz[x];
}
}
void dfszx(int u,int f,int sum,int &ans,int &id)
{
zx[u]=sum-sz[u];
for(int i=head[u];~i;i=e[i].Next)
{
int x=e[i].to;
if(x==f||vis[x])continue;
dfszx(x,u,sum,ans,id);
zx[u]=max(zx[u],sz[x]);
}
if(ans>zx[u])
{
ans=zx[u];
id=u;
}
}
int findzx(int root)
{
int sum=,ans=inf,id=;
dfssz(root,-,sum);
dfszx(root,-,sum,ans,id);
return id;
}
void dfsdis(int u,int f)
{
son[sonsz++]=dis[u];
for(int i=head[u];~i;i=e[i].Next)
{
int x=e[i].to;
if(x==f||vis[x])continue;
dis[x]=dis[u]+e[i].c;
dfsdis(x,u);
}
}
int ok1()
{
sort(son,son+sonsz);
/* for(int i=0;i<ma.size();i++)cout<<ma[i]<<" ";
cout<<endl;
cout<<"---------"<<endl;*/
int ans=;
for(int i=;i<sonsz;i++)
{
int p1=upper_bound(son,son+sonsz,k-son[i])-son;
int p2=lower_bound(son,son+sonsz,k-son[i])-son;
ans+=p1-p2;
}
return ans/;
}
void dfsson(int u,int f)
{
son[sonsz++]=dis[u];
for(int i=head[u];~i;i=e[i].Next)
{
int x=e[i].to;
if(x==f||vis[x])continue;
dfsson(x,u);
}
}
int solve(int root)
{
int p=findzx(root);
dis[p]=;
sonsz=;
dfsdis(p,-);
int ans=ok1();
for(int i=head[p];~i;i=e[i].Next)
{
int x=e[i].to;
if(vis[x])continue;
sonsz=;
dfsson(x,p);
ans-=ok1();
}
vis[p]=;
for(int i=head[p];~i;i=e[i].Next)
{
int x=e[i].to;
if(vis[x])continue;
ans+=solve(x);
}
return ans;
}
void init()
{
cnt=;
memset(head,-,sizeof head);
}
int main()
{
/* ios::sync_with_stdio(false);
cin.tie(0);*/
int n;
while(scanf("%d",&n),n)
{
init();
for(int i=;i<=n;i++)
{
int a,b;
while(scanf("%d",&a))
{
if(!a)break;
scanf("%d",&b);
add(i,a,b);
add(a,i,b);
}
}
while()
{
for(int i=;i<=n;i++)vis[i]=;
scanf("%d",&k);
if(!k)break;
if(solve()>)puts("AYE");
else puts("NAY");
}
puts(".");
}
return ;
}
/******************* ********************/

poj2114树分治的更多相关文章

  1. poj2114 树分治(点分治)

    poj1741板子套一套,统计对数的方式改一下,可以在O(n)时间内统计对数 最后不要忘记输出最后的“.” /* 给定一棵边权树,是否存在一条路径使得其长度为恰好为x 把1741的板子改为求点对之间的 ...

  2. hdu-5977 Garden of Eden(树分治)

    题目链接: Garden of Eden Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/ ...

  3. 【BZOJ-1468】Tree 树分治

    1468: Tree Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 1025  Solved: 534[Submit][Status][Discuss] ...

  4. HDU 4812 D Tree 树分治+逆元处理

    D Tree Problem Description   There is a skyscraping tree standing on the playground of Nanjing Unive ...

  5. BZOJ 2152: 聪聪可可 树分治

    2152: 聪聪可可 Description 聪聪和可可是兄弟俩,他们俩经常为了一些琐事打起来,例如家中只剩下最后一根冰棍而两人都想吃.两个人都想玩儿电脑(可是他们家只有一台电脑)……遇到这种问题,一 ...

  6. POJ 1741 Tree 树分治

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

  7. UVALive 7148 LRIP【树分治+线段树】

    题意就是要求一棵树上的最长不下降序列,同时不下降序列的最小值与最大值不超过D. 做法是树分治+线段树,假设树根是x,y是其当前需要处理的子树,对于子树y,需要处理出两个数组MN,MX,MN[i]表示以 ...

  8. BZOJ 2566 xmastree(树分治+multiset)

    题目链接:http://www.lydsy.com:808/JudgeOnline/problem.php?id=2566 题意:一棵有边权的树.结点有颜色.每次修改一个点的颜色.求每次修改后所有同色 ...

  9. 树分治&树链剖分相关题目讨论

    预备知识 树分治,树链剖分   poj1741 •一棵有n个节点的树,节点之间的边有长度.方方方想知道,有多少个点对距离不超过m 题解 点分治模板题.详见我早上写的http://www.cnblogs ...

随机推荐

  1. vue.js 拦截器

    document.cookie = "mylogin=1";//1:登陆成功:保存登录状态 main.js router.beforeEach((to, from, next) = ...

  2. centos中screen的使用 创建 退出

    一.创建一个新窗口: 安装完成后,直接敲命令screen就可以启动它.但是这样启动的screen会话没有名字,实践上推荐为每个screen会话取一个名字,方便分辨: [root@elk-server ...

  3. 20170411 debug窗口执行文件

    [FUNCTION] Command=/H Title=Debugger Type=SystemCommand

  4. java ReentrantLock可重入锁的使用场景

    摘要 从使用场景的角度出发来介绍对ReentrantLock的使用,相对来说容易理解一些. 场景1:如果发现该操作已经在执行中则不再执行(有状态执行) a.用在定时任务时,如果任务执行时间可能超过下次 ...

  5. linux各个文件夹作用

    linux /bin 二进制可执行命令 /dev 设备特殊文件 /etc 系统管理和配置文件 /etc/rc.d 启动的配置文件和脚本 /home 用户主目录的基点,比如用户user的主目录就是/ho ...

  6. 【WEB HTTP】集成点:网关、隧道及中继

    网关:网关可以作为某种翻译器使用,它抽象出了一种能够到达资源的方法.网关是资源与应用程序之间的粘合剂. 在不同http版本之间进行转换的Web代理和网关一样,他们会执行复杂的逻辑,以便在各个端点之间进 ...

  7. Amazon2014在线笔试 第三题

    问题描述: 算法分析: s1:层数对齐:分别求两个数所在的层(l1,l2),把层数大的(假设l2>l1)先往上找父节点,从而对齐到l1层: s2:两个数同时往上找, 直到找到公共的父节点(一定能 ...

  8. More about Parameter Passing in Python(Mainly about list)

    我之前写了一篇关于Python参数传递(http://www.cnblogs.com/lxw0109/p/python_parameter_passing.html)的博客, 写完之后,我发现我在使用 ...

  9. windows下客户端开发hdf--环境搭建

    1.引入依赖 <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop- ...

  10. active admin

    activeadmin 1.0.0.pre4 所依赖的库 gem 'jquery-rails', '~> 4.0.4' 4.2版本会出现找不到jquery-ui 的datepicker错误 使用 ...