Codeforces 14D Two Paths 【树的直径】
来源:互联网 发布:ubuntu 主题 编辑:程序博客网 时间:2024/05/22 08:06
题目链接:Codeforces 14D Two Paths
题意:给你一棵树,让你找到两条不存在公共点的路径,记长度分别为
思路:为了保证不存在公共点,我们划分两棵树,划分树的时候直接枚举边就可以了。对每一棵树求一次树的直径,维护最大值就好了。
#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <cmath>#include <algorithm>#include <vector>#include <queue>#include <map>#include <stack>#define PI acos(-1.0)#define CLR(a, b) memset(a, (b), sizeof(a))#define fi first#define se second#define ll o<<1#define rr o<<1|1using namespace std;typedef long long LL;typedef pair<int, int> pii;const int MAXN = 2*1e5+100;const int INF = 0x3f3f3f3f;const int MOD = 1e9 + 7;void getmax(int &a, int b) {a = max(a, b); }void getmin(int &a, int b) {a = min(a, b); }void add(LL &x, LL y) { x += y; x %= MOD; }struct Edge{ int from, to, next;};Edge edge[200*210];int head[210], edgenum;void init() {edgenum = 0; CLR(head, -1);}void addEdge(int u, int v) { Edge E = {u, v, head[u]}; edge[edgenum] = E; head[u] = edgenum++;}int node, ans;int dist[210];bool vis[210];void BFS(int sx){ queue<int> Q; CLR(dist, 0); CLR(vis, false); vis[sx] = true; ans = 0; node = sx; Q.push(sx); while(!Q.empty()) { int u = Q.front(); Q.pop(); for(int i = head[u]; i != -1; i = edge[i].next) { Edge E = edge[i]; if(!vis[E.to] && dist[E.to] < dist[u] + 1) { vis[E.to] = true; dist[E.to] = dist[u] + 1; if(dist[E.to] > ans) { ans = dist[E.to]; node = E.to; } Q.push(E.to); } } }}int Solve(int x){ BFS(x); BFS(node); return ans;}int u[210], v[210];int main(){ int n; cin >> n; for(int i = 1; i < n; i++) { cin >> u[i] >> v[i]; } int ans = 0; for(int i = 1; i < n; i++) { init(); for(int j = 1; j < n; j++) { if(i == j) continue; addEdge(u[j], v[j]); addEdge(v[j], u[j]); } int x = Solve(u[i]); int y = Solve(v[i]); //cout << x << " " << y << endl; ans = max(ans, x * y); } cout << ans << endl; return 0;}
0 0
- Codeforces 14D Two Paths 树的直径
- Codeforces--14D--Two Paths(树的直径)
- Codeforces 14D Two Paths 【树的直径】
- 【CodeForces】14D - Two Paths(树的直径)
- Codeforces Round #14 (Div. 2) D. Two Paths ( 树的直径
- Codeforces 14D Two Paths
- CodeForces 14D Two Paths
- codeforces 14D 树的直径
- Codeforces Beta Round #14 D - Two Paths
- CodeForces 14 E - Camels && D - Two Paths
- codeforces 14D Two Paths dfs
- Codeforces Beta Round #14 (Div. 2) D. Two Paths
- Codeforces Round #14 D. Two Paths(求树上两条不相交的路径的乘积最大值)
- Codeforces 337D Book of Evil (树的直径)
- Codeforces Round #328 (Div. 2) D. Super M (树的直径,虚树的直径)
- 【DFS——Codeforces Beta Round #14 (Div. 2)】D. Two Paths
- Codeforces Beta Round #14 (Div. 2) D. Two Paths 树形dp
- Codeforces 673D Bear and Two Paths (贪心构造)
- Spring使用util Schema
- POJ 3126 Prime Path 筛法+最短路
- Bellmanford 最短路(1)
- iOS UIAppearance使用详解
- python 实现用ISBN从豆瓣获取图书信息
- Codeforces 14D Two Paths 【树的直径】
- 《Linux内核设计与实现》与《Linux内核源代码情景分析》读书笔记
- 支持向量机分类入门实用指南
- spring4整合quartz2定时器启动报:batch acquisition of 0 triggers
- onlien_judge_1368
- 斯坦福大学深度学习笔记:逻辑回归
- Linux安装软件总结
- CNN的新进展(2)
- OPENCV图像处理基础(一)读取一张图片