CodeForces 690C2 Brain Network (medium)(树上DP)
题意:给定一棵树中,让你计算它的直径,也就是两点间的最大距离。
析:就是一个树上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)的更多相关文章
- codeforces 690C2 C2. Brain Network (medium)(bfs+树的直径)
题目链接: C2. Brain Network (medium) time limit per test 2 seconds memory limit per test 256 megabytes i ...
- Brain Network (medium)(DFS)
H - Brain Network (medium) Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d &am ...
- Brain Network (medium)
Brain Network (medium) Further research on zombie thought processes yielded interesting results. As ...
- codeforces 690C3 Brain Network
simple:并查集一下 #include <vector> #include <iostream> #include <queue> #include <c ...
- CodeForces 690C1 Brain Network (easy) (水题,判断树)
题意:给定 n 条边,判断是不是树. 析:水题,判断是不是树,首先是有没有环,这个可以用并查集来判断,然后就是边数等于顶点数减1. 代码如下: #include <bits/stdc++.h&g ...
- 树的直径新求法、codeforces 690C3 Brain Network (hard)
树的直径新求法 讲解题目 今天考了一道题目,下面的思路二是我在考场上原创,好像没人想到这种做法,最原始的题目,考场上的题目是这样的: 你现在有1 个节点,他的标号为1,每次加入一个节点,第i 次加入的 ...
- codeforces 690C3 C3. Brain Network (hard)(lca)
题目链接: C3. Brain Network (hard) time limit per test 2 seconds memory limit per test 256 megabytes inp ...
- Codeforces Round #526 (Div. 2) D. The Fair Nut and the Best Path 树上dp
D. The Fair Nut and the Best Path 题意:给出一张图 点有权值 边也要权值 从任意点出发到任意点结束 到每个点的时候都可以获得每个点的权值,而从边走的时候都要消耗改边的 ...
- codeforces 690C1 C1. Brain Network (easy)(水题)
题目链接: C1. Brain Network (easy) time limit per test 2 seconds memory limit per test 256 megabytes inp ...
随机推荐
- PHP中exit,exit(0),exit(1),exit('0'),exit('1'),die,return的区别
die('1') die()和exit()都是中止脚本执行函数:其实exit和die这两个名字指向的是同一个函数,die()是exit()函数的别名.该函数只接受一个参数,可以是一个程序返回的数值或 ...
- Joker的运维开发之路
python 1--数据类型,流程控制 2--数据类型详细操作,文件操作,字符编码 https://mp.weixin.qq.com/s/i3lcIP82HdsSr9LzPgkqww 点开更精彩 目前 ...
- HttpClient基础用法
一.HttpClient HttpClient是Apache HttpComponents 下的子项目,用来提供高效的.最新的.功能丰富的支持HTTP协议的客户端编程工具包(httpclient-4. ...
- python实现cifar10数据集的可视化
在学习tensorflow的mnist和cifar实例的时候,官方文档给出的讲解都是一张张图片,直观清晰,当我们看到程序下载下来的数据的时候,宝宝都惊呆了,都是二进制文件,这些二进制文件还不小,用文本 ...
- Visual Studio 进行Excel相关开发,Microsoft.Office.Interop.Excel.dll库
1. Interop.Excel.dll 的查找 本文中将 Microsoft.Office.Interop.Excel.dll库简称为Interop.Excel.dll库 其实在使用Visual S ...
- Spring定时器Quartz的使用
在JavaEE系统中,我们会经常用到定时任务,比如每天凌晨生成前天报表,每一小时生成汇总数据等等,定时更新某某操作……. 我们可以使用java.util.Timer结合java.util.TimerT ...
- IOSerialize(序列化)
在讲序列化和反序列化之前,先来阐述文件夹/文件 检查.新增.复制.移动.删除, Directory和DirectotyInfo这两个特性主要是对文件夹进行操作 首先检测文件夹是否存在 if (!Dir ...
- STL : 反向迭代器(Reverse Iterator)
1. 定义反向迭代器(Reverse Iterator)是一种反向遍历容器的迭代器.也就是,从最后一个元素到第一个元素遍历容器.反向迭代器将自增(和自减)的含义反过来了:对于反向迭代器,++运算将访问 ...
- Tomcat集群---Cluster节点配置
<!-- Cluster(集群,族) 节点,如果你要配置tomcat集群,则需要使用此节点. className 表示tomcat集群时,之间相互传递信息使用那个类来实现信息之间的传递. cha ...
- Reactjs+BootStrap开发自制编程语言Monkey的编译器:创建简易的页面IDE
http://localhost:3000/