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 ...
随机推荐
- CentOS下j2ee环境搭建
转自:http://www.cnblogs.com/xiaoluo501395377/archive/2013/04/01/2994485.html 因为是做j2ee后台开发的,所以在Linux上搭建 ...
- 上传图片demo
页面: js: 后台: @RequiresPermissions("pointwall:upload:edit") @RequestMapping(value = "sa ...
- Android:不同drawable文件夹的区别
4.0后,新建android工程,会自动生成drawable,drawalbe-ldpi,drawable-mdpi,drawable-hdpi,drawable-xhdpi,drawable-xxh ...
- Entity Framework API介绍 -- DbSet<>().Find()
过去我们常常使用Where或First(FirstOrDefault)方法来查找对应的实体,比如: var query = context.CertInfoMakeDetails.ToList().W ...
- CentOS下如何从vi编辑器插入模式退出到命令模式
刚打了下关于vi编辑器的命令,发现一直退出不了.后来自己敲着敲着它就退出了,写博客记录下. 比如现在w文件夹下面有一个ww文件 我进入这个文本,输入命令 vi ww,未回车,情况如下 按了回车,就进入 ...
- Math对象及相关方法
Math.abs() 取绝对值 Math.ceil()向上取整 (出现小数点就向上+1) Math.floor()向下取整 Math.round()四舍五入 Math.max(val1,val2,va ...
- cinder backup ceph的配置和使用
Backup 是将 volume 备份到别的地方(备份设备),将来可以通过 restore 操作恢复. 初看 backup 功能好像与 snapshot 很相似,都可以保存 volume 的当前状态, ...
- python以下划线开头的变量名含义
Python核心风格:避免用下划线作为变量名的开始. 因为下划线对解释器有特殊的意义,而且是内建标识符所使用的符号,我们建议程序员避免用下划线作为变量名的开始.一般来讲,变量名_xxx被看作是“私有的 ...
- php Reflection
<?php function title($title, $name) { return sprintf("%s. %s\r\n", $title, $name); } $f ...
- SlidingMenu Demo
参考:http://www.krislq.com/2013/03/android_case_slidingmenu_fragment/ 我下载了它的例子,然后自己重写了一下,运行时总报错,原来是sup ...