1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. using namespace std;
  5. const int MAX = 205;
  6. int dep, n, map[MAX][MAX];
  7. int dfs(int u, int v){
  8. int max1, max2, path_max;
  9. max1 = max2 = path_max = 0;
  10. for(int i = 1;i <= n;i ++){
  11. if(map[i][u] && i != v){
  12. int z = dfs(i, u);
  13. if(path_max < z) path_max = z;
  14. if(max1 < dep){
  15. max2 = max1;
  16. max1 = dep;
  17. }
  18. else if(max2 < dep) max2 = dep;
  19. }
  20. }
  21. if(path_max < max1 + max2) path_max = max2 + max1;
  22. dep = max1 + 1;
  23. return path_max;
  24. }
  25.  
  26. int main(int argc, char const *argv[]){
  27. int u, v;
  28. //freopen("in.c", "r", stdin);
  29. while(~scanf("%d", &n)){
  30. memset(map, 0, sizeof(map));
  31. for(int i = 0; i < n - 1;i ++){
  32. scanf("%d%d", &u, &v);
  33. map[u][v] = map[v][u] = 1;
  34. }
  35. int ans = -1;
  36. for(int i = 1;i <= n;i ++){
  37. for(int j = 1;j <= n;j ++){
  38. if(map[i][j]){
  39. int a = dfs(i, j);
  40. int b = dfs(j, i);
  41. ans = max(ans, a*b);
  42. }
  43. }
  44. }
  45. printf("%d\n", ans);
  46. }
  47. return 0;
  48. }

Codeforces 14D的更多相关文章

  1. Codeforces 14D Two Paths 树的直径

    题目链接:点击打开链接 题意:给定一棵树 找2条点不反复的路径,使得两路径的长度乘积最大 思路: 1.为了保证点不反复,在图中删去一条边,枚举这条删边 2.这样得到了2个树,在各自的树中找最长链.即树 ...

  2. CodeForces 14D 树的直径 Two Paths

    给出一棵树,找出两条不相交即没有公共点的路径,使得两个路径的长度的乘积最大. 思路:枚举树中的边,将该边去掉,分成两棵树,分别求出这两棵树的直径,乘起来维护一个最大值即可. #include < ...

  3. codeforces 14D(搜索+求树的直径模板)

    D. Two Paths time limit per test 2 seconds memory limit per test 64 megabytes input standard input o ...

  4. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  5. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  6. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  7. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  8. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  9. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

随机推荐

  1. apache 工作模式

    apache三种工作模式: prefork(2.4前默认)/worker/event(2.4默认)内容整理来自以下网站http://m.blog.csdn.net/article/details?id ...

  2. apache设置映射文件夹的配置方法

    在apache的配置文件中加入以下配置 Alias /uploadImage F:/upload <Directory F:/upload/UploadFiles>      Option ...

  3. SET QUOTED_IDENTIFIER ON

    语法  SET QUOTED_IDENTIFIER { ON | OFF }    注释  当 SET QUOTED_IDENTIFIER 为 ON 时,标识符可以由双引号分隔,而文字必须由单引号分隔 ...

  4. PythonCrawl自学日志(2)

    一.Scrapy环境的安装 1.配套组件的安装 由于开发环境是在VS2015Community中编码,默认下载的python3.5,系统是windows8.1,为此需要安装的组件有如下列表: 所有的组 ...

  5. Linux-ubuntu

    在VMware安装ubuntu时磁盘分配如下: /boot 100MB / 2G swap 512MB /home 5G /var 1G /usr 5G+. 设置root权限:sudo passwd ...

  6. C++ map映射的使用方法

    今天考试做了道题,用上了map,这是一道提高组联赛难度的题目,先发题目: ****************************** 1. A-B problem( dec.c/cpp/pas) . ...

  7. PL/SQL — 函数

    函数通常用于返回特定的数据.其实质是一个有名字的PL/SQL块,作为一个schema对象存储于数据库,可以被反复执行.函数通常被作为一个表达式来调用或存储过程的一个参数,具有返回值.   一.建立函数 ...

  8. mongodb Install the MongoDB service

    在用到mongodb时,首先要运行mongod.exe以启动mongo,这样就会出现命令框( command prompt),为了避免出现这种情况.要以服务的形式来启动mongo,这样就不会出现命令框 ...

  9. Ubuntu中PyCharm中字体设置

    在Ubuntu安装的PyCharm与Windows不同,除了Editor中的字体.配色需要设置外,文件操作栏(File--Edit--View--Navigate--Code--Refactor--- ...

  10. 显示 EXCEL 的页签列表

    如果你的EXCEL表有很多页签,反复点击左右箭头可能会很费时间. 不妨试试在 左箭头 或者 右箭头 上点击 右键,会有页签列表弹出.