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

析:就是一个树上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. FastJSON使用笔记

    虽然使用FastJSON来生成JSON数据非常简单 最常用的的方法就是JSON.toJSONString()静态方法,这个方法参数可以给一个String,也可以是一个int,也可以给一个Object类 ...

  2. ERROR无法从静态上下文中引用非静态变量

    ERROR无法从静态上下文中引用非静态变量 2012-06-16 20:58:52 分类: Java 什么是“static”? 学习过java.C++或C的人都应该认识这个关键字.用这个关键字修饰的变 ...

  3. url携带的参数获取并处理demo

    url demo: http://servername/webname/pagename.csp?paramName=paramValue&paramName2=paramName2& ...

  4. [转] geochart 地图控件官方示例

    <html> <head> <script type='text/javascript' src='https://www.google.com/jsapi'>&l ...

  5. thymeleaf switch在表格中的使用,遇到的空行问题

    switch在表格中的使用时 如果把<td>写在<div th:switch="${data.isShow}"> 里面导致外面出现很多空的<div&g ...

  6. 使用java获取自己的机器网卡

    package org.ibase4j.core.util; import java.io.BufferedReader;import java.io.IOException;import java. ...

  7. 关于ie6中绝对定位或浮动的div中既有向左float也有向右float时候如何让外层div自适应宽度的解决方案--

    一个详细的说明请见: http://www.cnblogs.com/yiyang/p/3265006.html 我的问题大约为,如下代码: <!DOCTYPE html PUBLIC " ...

  8. SQLite的基本用法

    SQLite是Android自带的轻量级数据库,接口封装的很好,不会SQL的也能很好的使用. 接下来讲一下怎么创建数据库.通过adb查看数据表和数据.增删查改. 一.创建数据库 Android封装了S ...

  9. TCP粘包拆包场景

    TCP编程底层都有粘包和拆包机制,因为我们在C/S这种传输模型下,以TCP协议传输的时候,在网络中的byte其实就像是河水,TCP就像一个搬运工,将这流水从一端转送到另一端,这时又分两种情况: 1)如 ...

  10. Rector模式

    讲到高性能IO绕不开Reactor模式,它是大多数IO相关组件如Netty.Redis在使用的IO模式,为什么需要这种模式,它是如何设计来解决高性能并发的呢? 最最原始的网络编程思路就是服务器用一个w ...