Codeforces 14D Two Paths 树的直径
题目链接:点击打开链接
题意:给定一棵树
找2条点不反复的路径,使得两路径的长度乘积最大
思路:
1、为了保证点不反复,在图中删去一条边,枚举这条删边
2、这样得到了2个树,在各自的树中找最长链。即树的直径,然后相乘就可以
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<set>
#include<vector>
#include<map>
#include<math.h>
#include<queue>
#include<string>
#include<stdlib.h>
#include<algorithm>
using namespace std;
#define N 220
struct Edge {
int from, to, nex;
bool hehe;
}edge[N<<1];
int head[N], edgenum;
void add(int u, int v){
Edge E={u,v,head[u],true};
edge[edgenum] = E;
head[u] = edgenum ++;
}
int n;
int dis[N];
void init(){ memset(head, -1, sizeof head); edgenum = 0; }
int bfs(int x){
memset(dis, 0, sizeof dis);
dis[x] = 1;
queue<int>q; q.push(x);
int hehe = x;
while(!q.empty()) {
int u = q.front(); q.pop();
for(int i = head[u]; ~i; i = edge[i].nex) {
if(!edge[i].hehe)continue;
int v = edge[i].to;
if(dis[v])continue;
dis[v] = dis[u]+1, q.push(v);
if(dis[hehe]<dis[v])hehe = v;
}
}
return hehe;
}
int main(){
int i, u, v;
while(~scanf("%d",&n)){
init();
for(i=1;i<n;i++){
scanf("%d %d",&u,&v);
add(u,v); add(v,u);
}
int ans = 0;
for(i = 0; i < edgenum; i+=2)
{
edge[i].hehe = edge[i^1].hehe = false;
u = edge[i].from; v = edge[i].to;
int L1 = bfs(u); int R1 = bfs(L1);
int mul1 = dis[R1] -1;
int L2 = bfs(v); int R2 = bfs(L2);
int mul2 = dis[R2] -1;
ans = max(ans, mul1 * mul2);
edge[i].hehe = edge[i^1].hehe = true;
}
printf("%d\n",ans);
}
return 0;
}
Codeforces 14D Two Paths 树的直径的更多相关文章
- Codeforces Beta Round #14 (Div. 2) D. Two Paths 树的直径
题目链接: http://codeforces.com/contest/14/problem/D D. Two Paths time limit per test2 secondsmemory lim ...
- Codeforces 592D - Super M - [树的直径][DFS]
Time limit 2000 ms Memory limit 262144 kB Source Codeforces Round #328 (Div. 2) Ari the monster is n ...
- Codeforces 455C Civilization:树的直径 + 并查集【合并树后直径最小】
题目链接:http://codeforces.com/problemset/problem/455/C 题意: 给你一个森林,n个点,m条边. 然后有t个操作.共有两种操作: (1)1 x: 输出节点 ...
- codeforces GYM 100781A【树的直径】
先求出每棵树的直径,排个序,要想图的直径最小的话需要每棵树的直径中点像直径最大的树的直径中点连边,这样直径有三种情况:是直径最大的树的直径:a[tot]:是直径最大的树和直径第二大的树的半径拼起来+1 ...
- codeforces 734E(DFS,树的直径(最长路))
题目链接:http://codeforces.com/contest/734/problem/E 题意:有一棵黑白树,每次操作可以使一个同色连通块变色,问最少几次操作能使树变成全黑或全白. 思路:先进 ...
- CodeForces 14D 树的直径 Two Paths
给出一棵树,找出两条不相交即没有公共点的路径,使得两个路径的长度的乘积最大. 思路:枚举树中的边,将该边去掉,分成两棵树,分别求出这两棵树的直径,乘起来维护一个最大值即可. #include < ...
- codeforces 14D(搜索+求树的直径模板)
D. Two Paths time limit per test 2 seconds memory limit per test 64 megabytes input standard input o ...
- TTTTTTTTTTTTT 树的直径 Codeforces Beta Round #14 (Div. 2) D. Two Paths
tiyi:给你n个节点和n-1条边(无环),求在这个图中找到 两条路径,两路径不相交,求能找的两条路径的长度的乘积最大值: #include <iostream> #include < ...
- Codeforces--14D--Two Paths(树的直径)
Two Paths Time Limit: 2000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u Submit ...
随机推荐
- codevs 2837 考前复习——01背包
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Description Aiden马上要考试了,可他还没怎么复习,于是他 ...
- shell spool
If you wish to use SQL*Plus Command-line , you'll simply issue the sqlplus command from your shell: ...
- Mybatis中的XML中需要用到的转义符号整理
使用这么久的Mybatis中需要转义的符号整理一下,小结一下: 1. < 小于符号 < 2. <= 小于等于 ...
- Delphi CRC算法, 不错
http://www.cnblogs.com/tangqs/archive/2011/12/08/2280255.html
- iOS用户响应者链的那些事儿
这篇文章想跟大家分享的主旨是iOS捕获用户事件的各种情况,以及内部封装的一些特殊事件. 我们先从UIButton谈起,UIButton大家使用的太多了,他特殊的地方就在于其内置的普通Default/高 ...
- WCF 与其它技术的比较
以下特性WCF都支持 特性 Web Service .Net Remoting Enterprise Services WSE(WS Enhancements) MSMQ 具有互操作性的Web服务 支 ...
- Android - 标准VideoView播放演示样例
标准VideoView播放演示样例 本文地址: http://blog.csdn.net/caroline_wendy 在Android SDK中的ApiDemos内, 提供标准播放视频的代码,使用V ...
- fabricjs line
let line1 = new fabric.Line([lineleft, lineheight, lineleft, 0], {//终止位置,线长,起始位置,top,这里是从项目中截下来的我用了变 ...
- 开关按钮(ToggleButton&Switch)
开关按钮,很实用的小东西. 下面上实例: -------------------------------我是邪恶的分割线--------------------------------------- ...
- Win7安装了Visual Studio 2008没有快捷方式怎么办
在C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE,就是这个devenv.exe了