D. Two Paths---cf14D(树的直径)
题目链接:http://codeforces.com/problemset/problem/14/D
题意:有n个city ; n-1条路:求断开一条路之后分成的两部分所构成的树的直径的积最大是多少;
n的取值范围不大,所以可以采用暴力枚举的方法’;
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <stdlib.h>
#include <queue>
#include <map> using namespace std; typedef long long LL; #define met(a, b) memset(a, b, sizeof(a))
#define INF 0x3f3f3f3f
#define N 210 int G[N][N], n, vis[N], dist[N], Max, Index; void bfs(int s)
{
met(vis, );
vis[s] = ;
dist[s] = ; queue<int> Q; Q.push(s); while( Q.size() )
{
int p = Q.front();
Q.pop(); for(int i=; i<=n; i++)
{
if( !G[p][i] )continue; if( !vis[i] )
{
vis[i] = ; dist[i] = dist[p] + ; Q.push(i); if(dist[i] > Max)
{
Max = dist[i]; Index = i;
}
}
}
}
} int main()
{
int a[N], b[N]; while(scanf("%d", &n)!=EOF)
{
met(G, ); for(int i=; i<n; i++)
{
scanf("%d %d", &a[i], &b[i]); G[a[i]][b[i]] = G[b[i]][a[i]] = ;
} int Ans = ; for(int i=; i<n; i++)
{
G[a[i]][b[i]] = G[b[i]][a[i]] = ; met(dist, );
Max = Index = ;
bfs(a[i]);
bfs(Index); int ans = Max; met(dist, );
Max = Index = ;
bfs(b[i]);
bfs(Index); Ans = max(Ans, ans * Max); G[a[i]][b[i]] = G[b[i]][a[i]] = ;
}
printf("%d\n", Ans);
}
return ;
}
D. Two Paths---cf14D(树的直径)的更多相关文章
- Codeforces--14D--Two Paths(树的直径)
Two Paths Time Limit: 2000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u Submit ...
- cf14D Two Paths(树的直径)
题意: N个点构成一棵树.树枝的长度都是1. 在当中找两条不相交[没有公共点]的路,使得二者长度之积最大. (2 ≤ n ≤ 200) 思路: 一开始思路好麻烦,好麻烦,好麻烦,,,,,,,而且WA, ...
- cf14d 树的直径,枚举删边
#include<bits/stdc++.h> using namespace std; #define maxn 300 ]; int n,head[maxn],tot,a,b,dis[ ...
- 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 14D Two Paths 树的直径
题目链接:点击打开链接 题意:给定一棵树 找2条点不反复的路径,使得两路径的长度乘积最大 思路: 1.为了保证点不反复,在图中删去一条边,枚举这条删边 2.这样得到了2个树,在各自的树中找最长链.即树 ...
- CodeForces 14D 树的直径 Two Paths
给出一棵树,找出两条不相交即没有公共点的路径,使得两个路径的长度的乘积最大. 思路:枚举树中的边,将该边去掉,分成两棵树,分别求出这两棵树的直径,乘起来维护一个最大值即可. #include < ...
- TTTTTTTTTTTTT 树的直径 Codeforces Beta Round #14 (Div. 2) D. Two Paths
tiyi:给你n个节点和n-1条边(无环),求在这个图中找到 两条路径,两路径不相交,求能找的两条路径的长度的乘积最大值: #include <iostream> #include < ...
- Gym - 100676H Capital City(边强连通分量 + 树的直径)
H. Capital City[ Color: Black ]Bahosain has become the president of Byteland, he is doing his best t ...
- Park Visit(树的直径)
传送门 Park Visit Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- codeforces 14D(搜索+求树的直径模板)
D. Two Paths time limit per test 2 seconds memory limit per test 64 megabytes input standard input o ...
随机推荐
- 一些lua代码
1.把k--v表转化为数组表,只支持2级 2.取中值 3.字符串按每行最多n像素分割,并返回每行最大宽度,可以用"\n"手动换行
- PHP——修改数据库2-加提示框,加登录页面
登录页面:0127lianxi.php <body> <h1>登陆</h1> <form action="0127lianxi.php" ...
- js 复制 功能
<html> <head> <title>Zero Clipboard Test</title> <meta charset="utf- ...
- JQuery EasyUI DataGrid动态合并(标题)单元) 一
JS: /** * EasyUI DataGrid根据字段动态合并单元格 * @param fldList 要合并table的id * @param fldList 要合并的列,用逗号分隔(例如:&q ...
- Python之并行
http://www.open-open.com/news/view/1c0179b/
- 【BZOJ】1500: [NOI2005]维修数列(splay+变态题)
http://www.lydsy.com/JudgeOnline/problem.php?id=1500 模板不打熟你确定考场上调试得出来? 首先有非常多的坑点...我遇到的第一个就是,如何pushu ...
- centos配置ssh免密码登录后,仍提示输入密码
配置SSH无密码登录需要3步: 1.生成公钥和私钥 2.导入公钥到认证文件,更改权限 3.测试 1.生成公钥和私钥 ssh-keygen -t dsa -P '' -f ~/.ssh/id_dsa 默 ...
- ie中自动识别单屏与双屏(js)
<!DOCTYPE html> <html> <head> <meta name="viewport" content="wid ...
- 360破解大赛crackme分析--之3DES解密附加数据
具体的分析这里有.本人仅仅是对这里面有趣的算法进行了一些学习 分析链接 这次是逆向的使用3DES解密的过程中的内容: 使用微软的crypt库 使用3DES解密程序中的附加数据 代码: VOID enc ...
- MySQL之explain 的type列 & Extra列
explain 可以分析 select 语句的执行,即 MySQL 的“执行计划. 一.type 列 MySQL 在表里找到所需行的方式.包括(由左至右,由最差到最好): | All | inde ...