题意:给定一棵树中,让你计算它的直径,也就是两点间的最大距离。

析:就是一个树上DP,用两次BFS或都一次DFS就可以搞定。但两次的时间是一样的。

代码如下:

#include<bits/stdc++.h>

using namespace std;
const int maxn = 1e5 + 5;
vector<int> G[maxn];
int f[maxn], g[maxn], l[maxn]; int dfs(int root, int fa){
if(f[root] != -1) return f[root];
if(!G[root].size()) return f[root] = 0;
int ans = root, m = 0, mm = 0;
for(int i = 0; i < G[root].size(); ++i){
int u = G[root][i];
if(u == fa) continue;
if(dfs(u, root) + 1 > m){
m = f[u] + 1;
ans = u;
}
}
l[root] = ans;
for(int i = 0; i < G[root].size(); ++i){
int u = G[root][i];
if(f[u] + 1 > mm && u != l[root]) mm = f[u] + 1;
}
g[root] = mm;
return f[root] = m;
} int main(){
int n, m, u, v;
cin >> n >> m;
while(m--){
scanf("%d %d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
} memset(f, -1, sizeof(f));
int ans = 0;
for(int i = 1; i <= n; ++i)
if(f[i] == -1) dfs(i, -1);
for(int i = 1; i <= n; ++i) ans = max(ans, f[i]+g[i]);
cout << ans << endl;
return 0;
}

两次BFS:

#include<bits/stdc++.h>

using namespace std;
const int maxn = 1e5 + 5;
int d[maxn];
vector<int> G[maxn];
int vis[maxn], vvis[maxn]; int bfs(int root){
memset(vis, 0, sizeof(vis));
memset(d, 0, sizeof(d));
vis[root] = 1; vvis[root] = 1;
queue<int> q; q.push(root);
int ans = root, mmax = 0;
while(!q.empty()){
root = q.front(); q.pop();
for(int i = 0; i < G[root].size(); ++i){
int u = G[root][i];
if(vis[u]) continue;
q.push(u);
vis[u] = vvis[u] = 1;
d[u] = d[root] + 1;
if(mmax < d[u]){
mmax = d[u];
ans = u;
}
}
}
return ans;
} int solve(int root){
int u = bfs(root);
int v = bfs(u);
return d[v];
} int main(){
int n, m, u, v;
cin >> n >> m;
for(int i = 0; i < m; ++i){
scanf("%d %d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
} memset(vvis, 0, sizeof(vvis));
int ans = 0;
for(int i = 1; i <= n; ++i)
if(!vvis[i]) ans = max(ans, solve(i));
cout << ans << endl;
return 0;
}

CodeForces 690C2 Brain Network (medium)(树上DP)的更多相关文章

  1. codeforces 690C2 C2. Brain Network (medium)(bfs+树的直径)

    题目链接: C2. Brain Network (medium) time limit per test 2 seconds memory limit per test 256 megabytes i ...

  2. Brain Network (medium)(DFS)

    H - Brain Network (medium) Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d &am ...

  3. Brain Network (medium)

    Brain Network (medium) Further research on zombie thought processes yielded interesting results. As ...

  4. codeforces 690C3 Brain Network

    simple:并查集一下 #include <vector> #include <iostream> #include <queue> #include <c ...

  5. CodeForces 690C1 Brain Network (easy) (水题,判断树)

    题意:给定 n 条边,判断是不是树. 析:水题,判断是不是树,首先是有没有环,这个可以用并查集来判断,然后就是边数等于顶点数减1. 代码如下: #include <bits/stdc++.h&g ...

  6. 树的直径新求法、codeforces 690C3 Brain Network (hard)

    树的直径新求法 讲解题目 今天考了一道题目,下面的思路二是我在考场上原创,好像没人想到这种做法,最原始的题目,考场上的题目是这样的: 你现在有1 个节点,他的标号为1,每次加入一个节点,第i 次加入的 ...

  7. codeforces 690C3 C3. Brain Network (hard)(lca)

    题目链接: C3. Brain Network (hard) time limit per test 2 seconds memory limit per test 256 megabytes inp ...

  8. Codeforces Round #526 (Div. 2) D. The Fair Nut and the Best Path 树上dp

    D. The Fair Nut and the Best Path 题意:给出一张图 点有权值 边也要权值 从任意点出发到任意点结束 到每个点的时候都可以获得每个点的权值,而从边走的时候都要消耗改边的 ...

  9. codeforces 690C1 C1. Brain Network (easy)(水题)

    题目链接: C1. Brain Network (easy) time limit per test 2 seconds memory limit per test 256 megabytes inp ...

随机推荐

  1. [置顶] ubuntu版本很老,apt-get update更新失败时(W: Failed to fetch ...)------如何创建新的sources.list

    在说这个解决方案之前,我先说下,目前遇到的问题: 我使用 sudo apt-get update 之后,更新失败.具体原因如下: W: Failed to fetch http://cn.archiv ...

  2. SSMS安装英文版后无法修改为中文

    SSMS的UI语言和所安装的Visual Studio的语言是相关的,你这种情况应该是第一次安装的时候安装了英文版的visual studio isolated shell,在卸载的时候你没有卸载这个 ...

  3. Entity Framework 5.0系列之EF概览-三种编程方式

    概述 在开发面向数据的软件时我们常常为了解决业务问题实体.关系和逻辑构建模型而费尽心机,ORM的产生为我们提供了一种优雅的解决方案.ADO.NET Entity Framework是.NET开发中一种 ...

  4. SSH框架的简化(struts2、spring4、hibernate5)

    目的: 通过对ssh框架有了基础性的学习,本文主要是使用注解的方式来简化ssh框架的代码编写. 注意事项: 1.本文提纲:本文通过一个新闻管理系统的实例来简化ssh框架的代码编写,功能包括查询数据库中 ...

  5. python‘s first day for me

    计算机的基础 1,计算机由硬件及软件组成. 其中硬件主要包括了cpu,内存以及硬盘.软件则由操作系统以及一系列软件. 操作系统则可以操控硬件,使硬件完成一些需要的操作. python的历史 1989年 ...

  6. Unexpected API Error. Please report this at http://bugs.launchpad.net/nova/ and attach the Nova API log if possible. <class 'sqlalchemy.exc.OperationalError'> (HTTP 500) (Request-ID: req-6ac88345-ce5a

    Unexpected API Error. Please report this at http://bugs.launchpad.net/nova/ and attach the Nova API ...

  7. 腾讯安全反病毒实验室解读“Wannacry”勒索软件

    背景 针对昨日英国医院被攻击,随后肆虐中国高校的 WannaCry 勒索事件,腾讯安全反病毒实验室第一时间给出了深度权威的分析.此次勒索事件与以往相比最大的亮点在于,勒索病毒结合了蠕虫的方式进行传播, ...

  8. t讯src的一点小秘密

    1.腾讯网首页发表评论未做限制 风险url:http://coral.qq.com/2774166934 使用burp的intruder模块生成payload 未做任何限制导致可批量提交大量的评论…… ...

  9. C++Primer笔记-----day03

    ==============================================================day03================================= ...

  10. Javascript —— 有向图广度优先搜索

    用Javascript实现有向图的广度优先搜索 刚好遇到一个需求,对于一个有向图,指定一个节点 i 作为起点,输出从 i 出发,可以到达的所有节点,也就是图中以 i 作为起点的子连通片,思考了一下,可 ...