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

析:就是一个树上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. 解决IIS无响应假死状态

    方法一: 临时解决办法:在IIS中选择你的网站,右击->属性,选择主目录选项卡,最下面有个应用程序池选项,记住该处的名字,然后在IIS中找到应用程序池并展开,选择你刚才看到的那个名字,右击-&g ...

  2. linux tcp调优

    Linux TCP Performance Tuning News Linux Performance Tuning Recommended Books Recommended Links Linux ...

  3. congst与指针

    指向const的指针 //a pointer to const int;指针指向常量对象,相对本指针而言,不能指针指向的对象的常量,不能通过本指针修改常量对象指针,实际的对象不一定的常量 const指 ...

  4. Shachar Fleishma的论文,做点云重建的几篇论文都不错

    http://www.sci.utah.edu/~shachar/ 几篇论文都不错,但貌似05年之后就没有什么动作了.

  5. Windbg基本命令应用总结

    .cordll -ve -u -l //reload core dlls ------加载下载系统文件符号的URL---------- .sympath SRV*C:\Symbols*http://m ...

  6. 项目通过nginx强转为https访问后,代码中重定向的连接又变成了http协议,导致点击页面按钮,后台逻辑处理完后重定向报错了

    修改如下,需要在nginx对应的server下的location中增加配置,使重定向的地址协议取当前链接的协议,而不是nginx访问tomcat的协议,因为nginx访问tomcat是http的,并没 ...

  7. 【312】◀▶ arcpy 常用函数说明

    其他常用的 ArcPy 函数说明 序号 类名称   功能说明   语法 & 举例 01 RefreshActiveView   ====<<<< Description ...

  8. Spring <context:annotation-config />讲解

    在基于主机方式配置Spring的配置文件中,你可能会见到<context:annotation-config />这样一条配置,他的作用是向Spring容器注册AutowiredAnnot ...

  9. pl/sql中if语句的使用

  10. MySQL数据库篇之单表查询

    主要内容: 一.单表查询的语法 二.关键字的执行优先级 三.简单查询 四.where约束 五.分组查询 group by 六.having过滤 七.查询排序 order by 八.限制查询的记录数 l ...