传送门

$ \color{green} {solution : }$

我们可以暴力枚举断边,然后 $ O(n) $ 的跑一次换根 $ dp $,然后复杂度是 $ O(n * n) $ 的

#include <bits/stdc++.h>
using namespace std;

const int maxn = 100010;

template <typename T> inline void G(T &x) {
    x = 0; char o; bool f = false;
    for ( ; !isdigit(o = getchar()); ) {
        if( o == '-') {
            f = true;
        }
    }
    for ( ; isdigit(o); o = getchar()) {
        x = (x << 1) + (x << 3) + (o & 15);
    }
    if( f) {
        x = ~x + 1;
    }
}

template <typename T> inline bool check_Max(T &x, const T &y) {
    return x < y ? x = y, false : true;
}

template <typename T> inline bool check_Min(T &x, const T &y) {
    return x > y ? x = y, false : true;
}

struct Edge {
    int v, w; Edge *to;
    Edge ( int v, int w, Edge *to) : v(v), w(w), to(to) {}
    Edge () {}
}*head[maxn], pool[maxn<<1], *pis = pool;

int di[maxn], ndi[maxn];

inline void dfs1(int u, int pre, int &Mx) {
    di[u] = ndi[u] = 0;
    for ( Edge *now = head[u]; now; now = now->to) if( now->v ^ pre) {
        dfs1(now->v, u, Mx);
        check_Max(ndi[u], di[now->v] + now->w);
        if(ndi[u] > di[u]) {
            swap(ndi[u], di[u]);
        }
    }
    check_Max(Mx, ndi[u] + di[u]);
}

inline void dfs2(int u, int pre, int &Mx) {
    check_Min(Mx, di[u]);
    for ( Edge *now = head[u]; now; now = now->to) if( now->v ^ pre) {
        if( di[u] == di[now->v] + now->w) {
            check_Max(ndi[now->v], ndi[u] + now->w);
        }
        else {
            check_Max(ndi[now->v], di[u] + now->w);
        }
        if( ndi[now->v] > di[now->v]) {
            swap(di[now->v], ndi[now->v]);
        }
        dfs2(now->v, u, Mx);
    }
}

int n;

struct line {
    int l, r, w;
}data[maxn];

int main() {
    G(n);
    for ( register int i = 1; i < n; ++ i) {
        G(data[i].l); G(data[i].r); G(data[i].w);
        int u = data[i].l, v = data[i].r, w = data[i].w;
        head[u] = new (pis ++) Edge(v, w, head[u]); head[v] = new (pis ++) Edge(u, w, head[v]);
    }
    int ans = 0x7fffffff;
    for ( register int i = 1; i < n; ++ i) {
        int mx1 = 0, mx2 = 0, ret = 0;
        dfs1(data[i].l, data[i].r, mx1); dfs1(data[i].r, data[i].l, mx2);
        check_Max(ret, mx1); check_Max(ret, mx2);
        int mx3 = di[data[i].l], mx4 = di[data[i].r];
        dfs2(data[i].l, data[i].r, mx3); dfs2(data[i].r, data[i].l, mx4);
        check_Max(ret, mx3 + mx4 + data[i].w);
        check_Min(ans, ret);
    }
    printf("%d\n",ans);
    return 0;
}

[BZOJ 4890][TJOI2017]城市的更多相关文章

  1. BZOJ 4890: [Tjoi2017]城市 树形dp

    标签:树形dp,枚举,树的直径 一上来看到这个题就慌了,只想到了 $O(n^3)$ 的做法. 碰到这种题时要一步一步冷静地去分析,观察数据范围. 首先,$n\leqslant 5000$,所以可以先 ...

  2. bzoj4890[Tjoi2017]城市(树的半径)

    4890: [Tjoi2017]城市 Time Limit: 30 Sec  Memory Limit: 128 MBSubmit: 149  Solved: 91[Submit][Status][D ...

  3. 【BZOJ4890】[TJOI2017]城市(动态规划)

    [BZOJ4890][TJOI2017]城市(动态规划) 题面 BZOJ 洛谷 题解 数据范围都这样了,显然可以暴力枚举断开哪条边. 然后求出两侧直径,暴力在直径上面找到一个点,使得其距离直径两端点的 ...

  4. [洛谷P3761] [TJOI2017]城市

    洛谷题目链接:[TJOI2017]城市 题目描述 从加里敦大学城市规划专业毕业的小明来到了一个地区城市规划局工作.这个地区一共有ri座城市,<-1条高速公路,保证了任意两运城市之间都可以通过高速 ...

  5. Luogu 3761 [TJOI2017]城市

    BZOJ 4890. 在树上断开一条边之后会形成两个联通块,如果要使这一条边接回去之后保持一棵树的形态,那么必须在两个联通块之间各找一个点连接. 那么,对于每一条可能断开的边,它产生的答案是以下两者的 ...

  6. 换根DP+树的直径【洛谷P3761】 [TJOI2017]城市

    P3761 [TJOI2017]城市 题目描述 从加里敦大学城市规划专业毕业的小明来到了一个地区城市规划局工作.这个地区一共有ri座城市,<-1条高速公路,保证了任意两运城市之间都可以通过高速公 ...

  7. [TJOI2017]城市(树的直径)

    [TJOI2017]城市 题目描述 从加里敦大学城市规划专业毕业的小明来到了一个地区城市规划局工作.这个地区一共有ri座城市,<-1条高速公路,保证了任意两运城市之间都可以通过高速公路相互可达, ...

  8. BZOJ4890 & 洛谷3761:[TJOI2017]城市——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=4890 https://www.luogu.org/problemnew/show/P3761 从加 ...

  9. [bzoj 4887] [Tjoi2017]可乐

    传送门 Description 加里敦星球的人们特别喜欢喝可乐.因而,他们的敌对星球研发出了一个可乐机器人,并且 放在了加里敦星球的1号城市上.这个可乐机器人有三种行为:停在原地,去下一个相邻的 城市 ...

随机推荐

  1. hibernate方言

    hibernate中,方言是解决hql翻译成sql保证语法的正确,首先数据库都是支持sql的,不同的数据库会存在一些语法上面的差异.HQL则是基于对象的查询语言,当系统需要数据库的变换时,那么用hib ...

  2. PHP json_encode中文unicode转码问题

    用PHP的json_encode来处理中文的时候, 中文都会被编码, 变成不可读的, 类似”\u***”的格式,如果想汉字不进行转码,这里提供三种方法 1.升级PHP,在PHP5.4, 这个问题终于得 ...

  3. C++11新特性之auto

    auto的使用  c++11引入了auto类型说明符,auto让编译器通过初始值来推算变量的类型,所以auto定义的变量必须有初始值.  使用auto也能在一条语句中声明多个变量,因为一条声明语句只能 ...

  4. org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'deptDao_a' defined in class path resource [beansAndHibernate.xml]: Cannot resolve reference to bean 'sessionFact

    Error creating bean with name 'deptDao_a' defined in class path 因为更改了类的名字,所以其setter方法没有更改,需要 private ...

  5. .NET基础 (06)面向对象的实现

    面向对象的实现1 C#中类可以有多个父类.可以实现多个接口吗2 简述C#中重写.重载和隐藏的概念3 为什么在构造方法中调用虚方法会导致问题4 在C#中如何声明一个类不能被继承 面向对象的实现 1 C# ...

  6. 解决jeesite开发java.lang.String cannot be cast to com.thinkgem.jeesite.modules.sys.security.SystemAuthorizingRealm$Principal问题

    解决jeesite问题java.lang.String cannot be cast to SystemAuthorizingRealm问题 这些天在jeesite项目上进行二次开发,遇到许多莫名其妙 ...

  7. [QPlugins]学习大纲

    QPlugins是一个DELPHI实现的插件框架,官方网址是:http://www.qdac.cc/ . 把学习QPlugins的过程和心得做一个记录,以便应用到项目中,同时学习一些实现方法和思想. ...

  8. Python【变量】

    本文介绍 1.Python运算符 运算符分类 运算符分为:算数运算.比较运算.逻辑运算.赋值运算.成员运算.身份运算.位运算 一.算数运算:返回数字 假设变量a=10,b=20 运算符: + 相加a+ ...

  9. 如何为 smartraiden 贡献代码

    如何为 smartRaiden 贡献代码 1.Fork 项目 登录 github 账号,并访问https://github.com/SmartMeshFoundation/SmartRaiden,然后 ...

  10. 201621123012 《Java程序设计》第14次学习总结

    作业14-数据库 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结与数据库相关内容. 2. 使用数据库技术改造你的系统 2.1 简述如何使用数据库技术改造你的系统.要建立什么表?截 ...