codeforces 14D 树的直径
来源:互联网 发布:程序员薪资 编辑:程序博客网 时间:2024/05/21 22:33
题目大意:给你一棵树,要进行两次访问,两次访问中不能重复访问任一节点和边,问两次访问的最大长度乘积,每边 长度为1
思路:因为n只有200,所以可以枚举从哪里将树分成两棵树即删除一条边,求两棵树的直径,维护乘积最大值即可。树的直径在以前的博文讲过,两遍dfs即可
#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <fstream>#include <algorithm>#include <cmath>#include <queue>#include <stack>#include <vector>#include <map>#include <set>#include <iomanip>using namespace std;//#pragma comment(linker, "/STACK:102400000,102400000")#define maxn 205#define MOD 1000000007#define mem(a , b) memset(a , b , sizeof(a))#define LL long long#define ULL long longconst long long INF=0x3fffffff;int a[maxn][maxn];int vis[maxn] , n , maxx , ed;struct node{ int u , v;}edge[maxn];void dfs(int u , int dis){ vis[u] = 1; if(maxx <= dis) { maxx = dis; ed = u; } for(int i = 1 ; i <= n ; i ++ ) { if(a[u][i] && !vis[i]) dfs(i , dis + 1); }}int main(){ while(scanf("%d" , &n) != EOF) { int pos = 2 , u , v ; mem(a , 0); mem(vis , 0); for(int i = 2 ;i <= n ; i ++) { scanf("%d %d" , &u , &v); edge[pos].u = u; edge[pos++].v = v; a[u][v] = a[v][u] = 1; } int res = 0 , len1 , len2; for(int i = 2 ; i <= n ; i ++) { u = edge[i].u; v = edge[i].v; a[u][v] = a[v][u] = 0; mem(vis , 0); ed = -1 , maxx = 0; dfs(u , 0); mem(vis , 0); maxx = 0; // if(ed != -1) dfs(ed , 0); len1 = maxx; mem(vis , 0); ed = -1 , maxx = 0; dfs(v , 0); mem(vis , 0); maxx = 0; // if(ed != -1) dfs(ed , 0); len2 = maxx; res = max(res , len1 * len2); a[u][v] = a[v][u] = 1; } printf("%d\n" , res); } return 0;}/*1513 1410 145 1010 69 1010 715 68 72 61 103 13 114 314 12*/
0 0
- codeforces 14D 树的直径
- Codeforces 14D Two Paths 树的直径
- Codeforces--14D--Two Paths(树的直径)
- Codeforces 14D Two Paths 【树的直径】
- 【CodeForces】14D - Two Paths(树的直径)
- Codeforces 337D Book of Evil (树的直径)
- Codeforces Round #328 (Div. 2) D. Super M (树的直径,虚树的直径)
- Codeforces Round #14 (Div. 2) D. Two Paths ( 树的直径
- [树的直径] Codeforces 804D Round #411 (Div. 1) D. Expected diameter of a tree
- Codeforces 14D 求树的直径(网上大多数是DFS于是自己写了个BFS)
- Codeforces 592D Super M 【求解包含若干个点的最小树 + 树的直径】
- Codeforces Round #328 D Super M(树的直径+树形DP)
- 最快遍历完图中重要节点 树的直径 Codeforces 328 (Div. 2) D. Super M
- Codeforces Round #328 (Div. 2) 592 D. Super M 树的直径
- Codeforces Round #411 (Div. 1) D. Expected diameter of a tree(树的直径)
- Codeforces 804D Expected diameter of a tree(树的直径)
- Codeforce 337D(树的直径变形)
- Codeforces 455C Civilization 树的直径+并查集
- 视频聊天程序
- ViewDragHelper实战 自己打造Drawerlayout
- AndroidStudio 分享项目到 git
- 搭建个人博客使用github+hexo
- Android游戏编程笔记(二)
- codeforces 14D 树的直径
- 【Yii框架 一 】PHP框架,Yii概述
- WordCount过程详解
- java面试题之----UDP聊天程序
- 某些情况下 push pop动画失效的处理
- FAQ 常见问题 -【Dr.Elephant系列文章-8】
- 对 Android 开发者有益的 40 条优化建议
- 算法学习过程
- PHP接口数据的封装XML和JSON