Tree Land Kingdom is a prosperous and lively kingdom. It has N cities which are connected to each
other by roads such that there is exactly one path to go from one city to any other city. Each road in
the kingdom connects exactly two different cities.
Every day a lot of merchants travel from one city to other cities in the kingdom making this kingdom
famous for its commerce. The king of this kingdom wonders, which city is the busiest one in his entire
kingdom. The busyness of a city is defined as the number of merchants who visits this city on each
day. A merchant is considered as visiting c city if and only if city c lies on the path when the merchant
travels from city a to city b.
Unfortunately, we need a lot of resources and time to answer the king’s question. Therefore, the
ministers come up with an idea to approximate the answer so they can provide the king with an “early”
answer while they are working on the actual answer. To approximate the answer, the ministers modify
the definition of a city’s busyness a bit. The busyness of a city a is now defined as the number of
different pair of cities a − b such that c lies in a simple path from a to b (note that c is neither a nor
c). A path is considered simple if and only if it does not visit any city more than once.
Consider the example as shown in Figure 1 below.
In this example, the busyness of city A, B, E and F are 0 because there is no pair of cities which
path visits those nodes. The busyness of city C is 7 (the pairs are: A-B, A-D, A-E, A-F, B-D, B-E, B-F)
and the busyness of city D is also 7 (the pairs are: A-E, A-F, B-E, B-F, C-E, C-F, E-F). Therefore, the
highest busyness in this example is 7, which occurs in city C and D.
Given the kingdom’s road structure, your task is to determine the highest busyness among all cities
in the kingdom.
Input
The first line of input contains an integer T (T ≤ 50) denoting the number of cases. Each case begins
with an integer N (3 ≤ N ≤ 20,000) denoting the number of cities in the kingdom. The cities are
numbered from 1 to N. The following N − 1 lines each contains two integers a and b (1 ≤ a, b ≤ N)
denoting a road which connects city a and city b.
Output
For each case, output ‘Case #X: Y ’, where X is the case number starts from 1 and Y is the highest
busyness among all cities in the kingdom for that case.
Notes:
• Explanation for 1st sample case
This sample case corresponds to Figure 1 in the problem statement.
• Explanation for 2nd sample case
The busiest city is city 2 with busyness of 1 (the pair is: 1-3).
• Explanation for 3rd sample case
The busiest city is city 2 with busyness of 3 (the pairs are: 1-3, 1-4, 3-4)

Sample Input

1 3
2 3
3 4
4 5
4 6

1 2
2 3

1 2
2 3
2 4

2 5
1 2
7 4
3 7
2 3
7 6
Sample Output
Case #1: 7
Case #2: 1
Case #3: 3
Case #4: 9

题意:给出n个顶点,n-1条边,对于每一个顶点来说每有一条路径经过,繁荣度+1,求最大繁荣度。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <set>
using namespace std;
typedef long long ll;
typedef unsigned long long Ull;
#define MM(a,b) memset(a,b,sizeof(a));
const double eps = 1e-10;
const int inf =0x7f7f7f7f;
const double pi=acos(-1);
const int maxn=20000+10;
int n,ans=0,sum[maxn];
vector<vector<int> > G(maxn);
void add_edge(int u,int v)
{
G[u].push_back(v);
G[v].push_back(u);
} void dfs(int father,int u)
{
for(int i=0;i<G[u].size();i++)
{
int v=G[u][i];
if(v==father) continue;
dfs(u,v);
sum[u]+=sum[v];
}
int m=sum[u]*(n-1-sum[u]),tmp=0;
for(int i=0;i<G[u].size();i++)
{
int x=G[u][i];
if(x==father) continue;
tmp+=sum[x]*(sum[u]-sum[x]);
}
m+=tmp/2;
ans=max(ans,m);
sum[u]++;
} int main()
{
int cas,kk=0;
scanf("%d",&cas);
while(cas--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++) G[i].clear();
for(int i=1;i<=n-1;i++)
{
int u,v;
scanf("%d %d",&u,&v);
add_edge(u,v);
}
MM(sum,0);ans=0;
dfs(0,1);
printf("Case #%d: %d\n",++kk,ans);
}
return 0;
}

  分析:很棒的一道题目,刚开始还是觉得有难度的,因为想的是对于每个点在O(1)时间内统计出他左边的点的个数和右边点的个数,然后还要考虑一个点可能不止连接着两个点.......

正确解答:原先的想法中统计出点分支的边对应的子树上点的个数思路还是有些正确的,难点就在与不会怎么统计,实际上,没有充分利用好这是一棵树的性质,是一颗树的话就意味着可以以1为根节点建一棵树,

然后sum[u]代表u的子树上的节点个数,那么这个点的富有度就是sum[u]*(n-1-sum[u])+子树上的

点,dfs建树就好,复杂度O(N);

  

LA 6434 The Busiest City dfs的更多相关文章

  1. codeforces B. Strongly Connected City(dfs水过)

    题意:有横向和纵向的街道,每个街道只有一个方向,垂直的街道相交会产生一个节点,这样每个节点都有两个方向, 问是否每一个节点都可以由其他的节点到达.... 思路:规律没有想到,直接爆搜!每一个节点dfs ...

  2. LA 6476 Outpost Navigation (DFS+剪枝)

    题目链接 Solution DFS+剪枝 对于一个走过点k,如果有必要再走一次,那么一定是走过k后在k点的最大弹药数增加了.否则一定没有必要再走. 记录经过每个点的最大弹药数,对dfs进行剪枝. #i ...

  3. UVALive - 6436

    题目链接:https://vjudge.net/contest/241341#problem/C Tree Land Kingdom is a prosperous and lively kingdo ...

  4. hibernate 左链接查询

    select pro from Provide as pro left join pro.labels as la left join pro.city as c where 1=1

  5. [POJ] 1948 Triangular Pastures (DP)

    题目地址:http://poj.org/problem?id=1948 题目大意: 给N条边,把这些边组成一个三角形,问面积最大是多少?必须把所有边都用上. 解题思路: 根据题意周长c已知,求组合三边 ...

  6. 快速批量导入庞大数据到SQL SERVER数据库(ADO.NET)

    原文地址:http://www.cnblogs.com/chenxizhang/archive/2008/11/11/1331060.html 如果你需要在程序中批量插入成千上万行的数据,你会怎么编写 ...

  7. [搜索] hdu 4016 Magic Bitwise And Operation

    主题链接: http://acm.hdu.edu.cn/showproblem.php?pid=4016 Magic Bitwise And Operation Time Limit: 6000/30 ...

  8. [TJOI2012]桥(最短路+线段树)

    有n个岛屿, m座桥,每座桥连通两座岛屿,桥上会有一些敌人,玩家只有消灭了桥上的敌人才能通过,与此同时桥上的敌人会对玩家造成一定伤害.而且会有一个大Boss镇守一座桥,以玩家目前的能力,是不可能通过的 ...

  9. 用json获取拉钩网的信息

    class LaoGo(object): def __init__(self): self.url="http://www.lagou.com/lbs/getAllCitySearchLab ...

随机推荐

  1. winform中如何使用确认对话框

    在系统中,常需要这样的功能,让用户确认一些信息:如下图: [退出系统]按钮关键代码如下: private void btnExit_Click(object sender, EventArgs e) ...

  2. [多平台]pymo – 手机上的 GalGame 引擎

    [多平台]pymo – 手机上的 GalGame 引擎 介绍下这个能在手机上玩移植 GalGame 的游戏引擎,不知道有多少人听过呢?相信如果有喜欢在手机上玩 GalGame 的同学肯定听过类似的东西 ...

  3. vue页面中图片不显示解决

    在做新版组态界面的时候,用vue框架实现,在配置页面图片的时候发现有一张图片明明页面输入的路径是对的可是图片就是不显示出来 现象: network页面资源也不报错,而且状态码竟然还是200,点prev ...

  4. R安装包的问题

    package 'tswge' is not available (for R version 3.5.1) install.packages('tswge', repos="http:// ...

  5. JVM类加载机制和内存模型

    一.JVM类加载机制 首先需要了解一下类加载器(ClassLoader): ClassLoader是Java的一个核心组件,它主要作用是从系统外部获得Class二进制数据流,然后将数据流装载到系统,交 ...

  6. Hacklab WebIDE在线调试ESP32笔记

    目录 1.什么是Hacklab WebIDE 1.1 优势 1.2 趋势 2. 使用方法 2.1 功能介绍 2.2 编译第一个程序 2.3 搭建esp32的开发环境 2.4 建立开发板与云平台的连接 ...

  7. 附件上传vue组件封装(一)

    //父页面部分 <attachment @newFileList="newFileList" :operationType="operationType" ...

  8. mac 下的操作

    nodejs在Mac下的卸载 在 node 官网上下载的安装包,用安装包安装的node.应该可以用一下命令行卸载: 在终端输入以下命令: sudo rm -rf /usr/local/{bin/{no ...

  9. VMwarevSphere Client 链接 vCenter Server中的主机,开启虚拟机提示:在主机当前连接状况下不允许执行该操作

    VMwarevSphere Client 链接 vCenter Server中的主机,开启虚拟机提示:在主机当前连接状况下不允许执行该操作很多原因都可以导致该问题出现,例如 vCenter Serve ...

  10. Phoenix设置联合主键

    1例如 create table test12(email varchar not null,id integer not null,name varchar not null,age integer ...