题目链接:https://vjudge.net/contest/241341#problem/C

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
4 6 1 3 2 3 3 4 4 5 4 6 3 1 2 2 3 4 1 2 2 3 2 4 7 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

题目大意:输入t,代表t组样例,输入n,接下来有n-1条边,问你一个点被经过的最多次数是多少,如果刚好到该点不算经过,所以叶子节点都算经过0次

个人思路:这道题首先要推出来经过的次数由哪几部分组成:可以把该点看作根,那么就是求它的子树的问题了,该点的次数有两部分组成:

第一部分:该点有n个子树,每个子树的节点数乘以其它子树的总节点数

第二部分:一个子树上的节点之间也有路,所以也要相乘(我这里的算法是多算了一倍,所以要除以2)

看代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<stdio.h>
#include<string.h>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<set>
#include<queue>
#include<map>
typedef long long ll;
using namespace std;
const ll mod=1e9+7;
const int maxn=2e4+10;
const int maxk=100+10;
const int maxx=1e4+10;
const ll maxe=1000+10;
#define INF 0x3f3f3f3f3f3f
vector<int>p[maxn];
int Po[maxn];
int ans=0,n;
void dfs(int now,int pre)
{for(int i=0;i<p[now].size();i++){int v=p[now][i];if(v!=pre){dfs(v,now);Po[now]+=Po[v];//该点除了自己和pre有多少个节点
        }}int sum1=0,sum2=0;sum1=Po[now]*(n-1-Po[now]);//自己集合的节点数*不属于自己集合的节点数(不包括本身)for(int i=0;i<p[now].size();i++){int v=p[now][i];if(v!=pre){// sum2+=Po[v]*(p[now].size()-Po[v]);sum2+=Po[v]*(Po[now]-Po[v]);//自己集合的节点之间也会有路径
        }}sum2=sum2/2;//相当于乘了两遍,所以除以2if(ans<sum1+sum2)ans=sum1+sum2;Po[now]++;//加上自己
}
int main()
{ios::sync_with_stdio(false);int t;int sum=1;cin>>t;while(t--){for(int i=0;i<maxn;i++)p[i].clear();//每次都要清空ans=0;memset(Po,0,sizeof(Po));int a,b;cin>>n;for(int i=1;i<n;i++){cin>>a>>b;p[a].push_back(b);p[b].push_back(a);}dfs(1,0);//从第一个节点开始遍历(其实任意一个节点都可以)printf("Case #%d: %d\n",sum++,ans);}return 0;
}

转载于:https://www.cnblogs.com/caijiaming/p/9384707.html

UVALive - 6436相关推荐

  1. DP UVALive 6506 Padovan Sequence

    题目传送门 /*题意:两行数字,相邻列一上一下,或者隔一列两行都可以,从左到右选择数字使和最大DP:状态转移方程:dp[i][j] = max (dp[i][j], dp[1-i][j-1] + a[ ...

  2. The UVALIVE 7716 二维区间第k小

    The UVALIVE 7716 二维区间第k小 /** 题意:给一个n * n的矩阵,有q个查询每次查询r,c,s,k表示已(r,c)为右上角 大小为s的正方形中 第k小的元素n <= 250 ...

  3. UVALive 8513 lovers 2017 西安区域赛 B 贪心+multiset

    UVALive 8513 有2种人,每个人有自己的权值$A_i$ $B_i$ 当$A_i + B_i >=K$时 两个人可以配对 问最多多少人可以配对 解法 : 把$/{ A_i /}$ 排序 ...

  4. 训练指南 UVALive - 3713 (2-SAT)

    layout: post title: 训练指南 UVALive - 3713 (2-SAT) author: "luowentaoaa" catalog: true mathja ...

  5. 逆序数 UVALive 6508 Permutation Graphs

    题目传送门 1 /* 2 题意:给了两行的数字,相同的数字连线,问中间交点的个数 3 逆序数:第一行保存每个数字的位置,第二行保存该数字在第一行的位置,接下来就是对它求逆序数 4 用归并排序或线段树求 ...

  6. Infinite Fraction Path UVALive - 8207

    Infinite Fraction Path UVALive - 8207 题意: 给你n个数,每个数在0到9之间,每个数的下标一次是0~n-1,然后他所能走到的数为(i^2+1)%n,i为他本身的下 ...

  7. F - Heron and His Triangle UVALive - 8206

    F - Heron and His Triangle UVALive - 8206 题意: 给你应该n,然后求一个最小的t,问长度为t-1,t,t+1所组成的三角形的面积为整数,t>=n 题解: ...

  8. Tree UVALive - 8212

    Tree UVALive - 8212 题意: 有n个点,k个颜色,每个点都要被染色,相同颜色之间的边算是被该颜色覆盖,问有多少边被所有颜色覆盖 题解: 题目给的是无根树,我们可以将1默认为根然后求所 ...

  9. Rabbits UVALive - 8211

    Rabbits UVALive - 8211 题意: n个兔子的位置,兔子每次可以跳到两个兔子之间,问最多可以跳多少下? 题解: 求出所有相邻两数的间隔,然后减去最小间隔就是答案 代码: #inclu ...

最新文章

  1. 谢文睿:西瓜书 + 南瓜书 吃瓜系列 11. 贝叶斯分类器
  2. 3. Swift 数组|字典|集合
  3. 第九届全国大学生智能汽车竞赛获奖名单
  4. Windows远程桌面管理--功能强大的远程批量管理工具
  5. MHA管理所有数据库服务器
  6. python实现进度条的3种方式
  7. 记一次灵活的模型训练生成的pth转onnx文件失败
  8. ImportError: libpq.so.5: cannot open shared object file: No such file or directory
  9. smb 限制大文件上传_单个文件大小 上传百度云盘 微信发送 有大小限制 怎么破?...
  10. MFC 文件I/O和串行化
  11. get请求400错误 vue_vue用get请求,一个很奇怪的现象
  12. DataGrid 嵌套DataList
  13. java接口返回类,Java接口和返回类型 - java
  14. 曹鹏php mysql视频教程_曹鹏 PHP+MYSQL 视频教程(flash)
  15. 7大浏览器颜值代表,谁才是真正的浏览器颜值之王呢?
  16. 明明是旅游小程序却做起了内容电商?
  17. 在线支付接口详解、支付接口对接
  18. 用Python3抓取并分析猫眼电影TOP100
  19. 信息熵与老鼠试药、称球问题
  20. Windows 变慢原因分析及解决方法

热门文章

  1. android 滚动条 相关属性
  2. [推荐]查看Json输出的*最方便*的方法 (转)
  3. 解决Maven的jar包冲突问题
  4. Hbase CallQueueTooBigException 异常处理
  5. ‘ActiveX component can’t create object解决方法
  6. 数据科学之:消费者资产分析
  7. 原生xgboost与sklearn里的xgboost
  8. hadoop入门简介
  9. 机器学习算法总结之支持向量机(一)
  10. 吃货联盟点餐java面向对象_使用面向对象思想编写吃货联盟