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 ...
随机推荐
- Codeforces 246E - Blood Cousins Return (树上启发式合并)
246E - Blood Cousins Return 题意 给出一棵家谱树,定义从 u 点向上走 k 步到达的节点为 u 的 k-ancestor,每个节点有名字,名字不唯一.多次查询,给出 u k ...
- HNOI2016 游记
题外 忽然想起去年的HNOI2015总结里好像引了一句诗: 此情可待成追忆,只是当时已惘然. Day0 唔,感觉不知道想些什么,只是觉得其实还没有做好准备,想学的东西学的仓促,想复习的东西,也只能看一 ...
- 四. Java继承和多态2. Java super关键字
super 关键字与 this 类似,this 用来表示当前类的实例,super 用来表示父类. super 可以用在子类中,通过点号(.)来获取父类的成员变量和方法.super 也可以用在子类的子类 ...
- SpringAop名词解释+基于xml的配置
1,AOP名词解释 2,AOP演示 (1)导包: (2)准备目标对象 package com.songyan.service; import org.aspectj.lang.ProceedingJo ...
- 每天一个liunx命令10之nohup和xargs
1上传jar包到服务器/edgewalk/springboot/下 2编写启动脚本start.sh #!/bin/sh APP_HOME=/edgewalk/springboot cd $APP_HO ...
- IOS设置UIView的边框为圆角
iOS 系统自带的 View 组件都是正方形的,看起来都太生硬,有时候我需要变成圆角形式,如下图: 具体的实现是使用QuartzCore库,下面我具体的描述一下实现过程: • 首先 ...
- Ubuntu16.04 -- 后台进程Nohup
nohup用于使程序在用户退出登陆.关闭终端之后仍能继续运行 用法: nohup your_command & #(符号&使程序在后台运行) exit #(退出nohup模式) 启动后 ...
- hdu 1162 Eddy's picture (Kruskal算法,prim算法,最小生成树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1162 [题目大意] 给你n个点的坐标,让你找到联通n个点的一种方法.保证联通的线路最短,典型的最小生成 ...
- 2017.4.26 慕课网--Java 高并发秒杀API配置文件(持续更新)
新建项目,new maven project. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=&q ...
- Android学习(一) 按钮的事件
用户登录 1.内部匿名类方式实现 layout <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/an ...