具体可以见漆子超的论文

/**************************************************************
    Problem:
    User: BLADEVIL
    Language: Pascal
    Result: Accepted
    Time: ms
    Memory: kb
****************************************************************/
 
//By BLADEVIL
var
    n, m                            :longint;
    pre, other, len                 :array[..] of longint;
    last                            :array[..] of longint;
    l, top                          :longint;
    size, stack, yy, ll             :array[..] of longint;
    ff                              :array[..] of boolean;
    root                            :longint;
    b                               :array[..] of longint;
    old                             :longint;
    ans                             :longint;
     
procedure connect(x,y,z:longint);
begin
    inc(l);
    pre[l]:=last[x];
    last[x]:=l;
    other[l]:=y;
    len[l]:=z;
end;
 
 
procedure dfs_size(x,fa:longint);
var
    q, p                            :longint;
begin
    size[x]:=;
    inc(top);
    stack[top]:=x;
    q:=last[x];
    while q<> do
    begin
        p:=other[q];
        if (ff[q]) or (p=fa) then
        begin
            q:=pre[q];
            continue;
        end;
        dfs_size(p,x);
        inc(size[x],size[p]);
        q:=pre[q];
    end;
    yy[x]:=fa;
end;
 
procedure getroot(u:longint);
var
    ms, s, x, p, q                  :longint;
    i                               :longint;
begin
    top:=;
    dfs_size(u,);
    ms:=maxlongint;
    for i:= to top do
    begin
        x:=stack[i];
        s:=size[u]-size[x];
        q:=last[x];
        while q<> do
        begin
            p:=other[q];
            if (ff[q]) or (p=yy[x]) then
            begin
                q:=pre[q];
                continue;
            end;
            if size[p]>s then s:=size[p];
            q:=pre[q];
        end;
        if s<ms then
        begin
            ms:=s;
            root:=x;
        end;
    end;
end;
 
procedure dfs_value(x,fa,lz,dep:longint);
var
    q, p                            :longint;
begin
    if lz>m then exit;
    if dep>=ans then exit;
    if b[m-lz]>= then
        if b[m-lz]+dep<ans then ans:=b[m-lz]+dep;
    if (b[lz]<) or (dep<b[lz]) then
    begin
        inc(top);
        stack[top]:=lz;
        ll[top]:=dep;
    end;
    q:=last[x];
    while q<> do
    begin
        p:=other[q];
        if (p=fa) or (ff[q]) then
        begin
            q:=pre[q];
            continue;
        end;
        dfs_value(p,x,lz+len[q],dep+);
        q:=pre[q];
    end;
end;
 
procedure solve(u:longint);
var
    i, q, p                         :longint;
 
begin
    getroot(u);
    if top= then exit;
    top:=;
    q:=last[root];
    while q<> do
    begin
        p:=other[q];
        if ff[q] then
        begin
            q:=pre[q];
            continue;
        end;
        old:=top+;
        dfs_value(p,root,len[q],);
        for i:=old to top do
            if (b[stack[i]]<) or (b[stack[i]]>ll[i]) then b[stack[i]]:=ll[i];
        q:=pre[q];
    end;
     
    for i:= to top do b[stack[i]]:=-;
    q:=last[root];
    while q<> do
    begin
        p:=other[q];
        if ff[q] then
        begin
            q:=pre[q];
            continue;
        end;
        ff[q]:=true;
        ff[q xor ]:=true;
        solve(p);
        q:=pre[q];
    end;
         
end;
 
     
procedure main;
var
    i                               :longint;
    x, y, z                         :longint;
     
begin
    read(n,m);
    l:=;
    fillchar(b,sizeof(b),$ff);
    b[]:=;   
    for i:= to n- do
    begin
        read(x,y,z);
        inc(x); inc(y);
        connect(x,y,z);
        connect(y,x,z);
    end;
    ans:=maxlongint;
    solve();
    if ans> then writeln(-) else writeln(ans);
end;
 
begin
    main;
end.

bzoj 2599 数分治 点剖分的更多相关文章

  1. bzoj 2599(点分治)

    2599: [IOI2011]Race Time Limit: 70 Sec  Memory Limit: 128 MBSubmit: 3642  Solved: 1081[Submit][Statu ...

  2. BZOJ 2243 染色 | 树链剖分模板题进阶版

    BZOJ 2243 染色 | 树链剖分模板题进阶版 这道题呢~就是个带区间修改的树链剖分~ 如何区间修改?跟树链剖分的区间询问一个道理,再加上线段树的区间修改就好了. 这道题要注意的是,无论是线段树上 ...

  3. BZOJ 2599: [IOI2011]Race( 点分治 )

    数据范围是N:20w, K100w. 点分治, 我们只需考虑经过当前树根的方案. K最大只有100w, 直接开个数组CNT[x]表示与当前树根距离为x的最少边数, 然后就可以对根的子树依次dfs并更新 ...

  4. bzoj 2599 [IOI2011]Race (点分治)

    [题意] 问树中长为k的路径中包含边数最少的路径所包含的边数. [思路] 统计经过根的路径.假设当前枚举到根的第S个子树,若x属于S子树,则有: ans<-dep[x]+min{ dep[y] ...

  5. BZOJ 2599 [IOI2011]Race【Tree,点分治】

    给出N(1 <= N <= 200000)个结点的树,求长度等于K(1 <= K <= 1000000)的路径的最小边数. 点分治,这道题目和POJ 2114很接近,2114是 ...

  6. 【BZOJ 2599】【IOI 2011】Race 点分治

    裸的点分治,然而我因为循环赋值$s$时把$i <= k$写成$i <= n$了,WA了好长时间 #include<cstdio> #include<cstring> ...

  7. BZOJ 2599 Race(树分治)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2599 题意:给一棵树,每条边有权.求一条路径,权值和等于K,且边的数量最小. 题意:每次 ...

  8. bzoj 2599: [IOI2011]Race (点分治 本地过了就是过了.jpg)

    题面:(复制别人的...) Description 给一棵树,每条边有权.求一条路径,权值和等于K,且边的数量最小. Input 第一行 两个整数 n, k第二..n行 每行三个整数 表示一条无向边的 ...

  9. bzoj 2599: [IOI2011]Race【点分治】

    点分治,用一个mn[v]数组记录当前root下长为v的链的最小深度,每次新加一个儿子的时候都在原来儿子更新过的mn数组里更新ans(也就是查一下mn[m-dis[p]]+de[p]) 这里注意更新和初 ...

随机推荐

  1. [leetcode]_Balanced Binary Tree

    第四道树题,逐渐能写递归了.虽然最后AC的代码还是看了网络,但是距离成功攻克此类问题仅一步之遥. 题目:一棵树,判断是否为AVL.(AVL条件:树上任意一点的左右子树的高度差<=1) 思路:树依 ...

  2. VS2010在非IE浏览器下调试Silverlight程序

    以Chrome为例: 第一步:在程序中设置断点. 第二步:右键点击web应用程序的起始页(.html或.aspx文件),选择"浏览方式",选中Chrome或其它非IE浏览器,点&q ...

  3. Hive中的排序语法

    ORDER BY hive中的ORDER BY语句和关系数据库中的sql语法相似.他会对查询结果做全局排序,这意味着所有的数据会传送到一个Reduce任务上,这样会导致在大数量的情况下,花费大量时间. ...

  4. 【转】Spark 体系结构

    原文地址:http://jerryshao.me/architecture/2013/03/29/spark-overview/ 援引@JerryLead的系统架构图作为Spark整体结构的一个 bi ...

  5. 并行编程之CountdownEvent的用法

    教程:http://blog.gkarch.com/threading/part5.html#the-parallel-class http://www.cnblogs.com/huangxinche ...

  6. python datetime 时间日期处理小结

    python datetime 时间日期处理小结 转载请注明出处:http://hi.baidu.com/leejun_2005/blog/item/47f340f1a85b5cb3a50f5232. ...

  7. angularjs2 学习笔记(三) 服务

    在anglar2中服务是什么? 如果在项目中有一段代码是很多组件都要使用的,那么最好的方式就是把它做成服务. 服务是一种在组件中共享功能的机制,当我们使用标签将多个组件组合在一起时我们需要操作一些数据 ...

  8. MIFARE系列2《非接触卡标准》

    根据信号发送和接收方式的不同,ISO/IEC14443-3定义了TYPEA.TYPEB两种卡型.它们的不同主要在于载波的调制深度及二进制数的编码方式.从读写机具向卡传送信号时,二者是通过13.56Mh ...

  9. Swift弹窗

    在一个ViewController中使用以下代码: let alertController = UIAlertController(title: "Game Set", messa ...

  10. [IOS] Storyboard全解析-第一部分

    (Storyboard)是一个能够节省你很多设计手机App界面时间的新特性,下面,为了简明的说明Storyboard的效果,我贴上本教程所完成的Storyboard的截图: 现在,你就可以清楚的看到这 ...