题目链接: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+;
const int maxn=2e4+;
const int maxk=+;
const int maxx=1e4+;
const ll maxe=+;
#define INF 0x3f3f3f3f3f3f
vector<int>p[maxn];
int Po[maxn];
int ans=,n;
void dfs(int now,int pre)
{
for(int i=;i<p[now].size();i++)
{
int v=p[now][i];
if(v!=pre)
{
dfs(v,now);
Po[now]+=Po[v];//该点除了自己和pre有多少个节点
}
}
int sum1=,sum2=;
sum1=Po[now]*(n--Po[now]);//自己集合的节点数*不属于自己集合的节点数(不包括本身)
for(int i=;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
if(ans<sum1+sum2)
ans=sum1+sum2;
Po[now]++;//加上自己
}
int main()
{
ios::sync_with_stdio(false);
int t;
int sum=;
cin>>t;
while(t--)
{
for(int i=;i<maxn;i++)
p[i].clear();//每次都要清空
ans=;
memset(Po,,sizeof(Po));
int a,b;
cin>>n;
for(int i=;i<n;i++)
{
cin>>a>>b;
p[a].push_back(b);
p[b].push_back(a);
}
dfs(,);//从第一个节点开始遍历(其实任意一个节点都可以)
printf("Case #%d: %d\n",sum++,ans);
}
return ;
}

UVALive - 6436的更多相关文章

  1. UVALive - 6436、HYSBZ - 2435 (dfs)

    这两道题都是用简单dfs解的,主要是熟悉回溯过程就能做,据说用bfs也能做 道路修建(HYSBZ - 2435) 在 W 星球上有n 个国家.为了各自国家的经济发展,他们决定在各个国家 之间建设双向道 ...

  2. UVALive - 6436(DFS)

    题目链接:https://vjudge.net/contest/241341#problem/C 题目大意:给你从1到n总共n个数字,同时给你n-1个连接,同时保证任意两个点之间都可以连接.现在假设任 ...

  3. UVALive - 6436 —(DFS+思维)

    题意:n个点连成的生成树(n个点,n-1条边,点与点之间都连通),如果某个点在两点之间的路径上,那这个点的繁荣度就+1,问你在所有点中,最大繁荣度是多少?就比如上面的图中的C点,在A-B,A-D,A- ...

  4. UESTC 2016 Summer Training #6 Div.2

    我好菜啊.. UVALive 6434 给出 n 个数,分成m组,每组的价值为最大值减去最小值,每组至少有1个,如果这一组只有一个数的话,价值为0 问 最小的价值是多少 dp[i][j] 表示将 前 ...

  5. UVALive - 4108 SKYLINE[线段树]

    UVALive - 4108 SKYLINE Time Limit: 3000MS     64bit IO Format: %lld & %llu Submit Status uDebug ...

  6. UVALive - 3942 Remember the Word[树状数组]

    UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...

  7. UVALive - 3942 Remember the Word[Trie DP]

    UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...

  8. 思维 UVALive 3708 Graveyard

    题目传送门 /* 题意:本来有n个雕塑,等间距的分布在圆周上,现在多了m个雕塑,问一共要移动多少距离: 思维题:认为一个雕塑不动,视为坐标0,其他点向最近的点移动,四舍五入判断,比例最后乘会10000 ...

  9. UVALive 6145 Version Controlled IDE(可持久化treap、rope)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

随机推荐

  1. 洛谷 P4220 & UOJ #347 通道 —— 随机化

    题目:https://www.luogu.org/problemnew/show/P4220 http://uoj.ac/problem/347 先写了一下 n^2 和三棵树一样的情况,n^2 还写了 ...

  2. centos7 中文乱码解决方法

    centos7 中文乱码解决方法 标签(空格分隔): centos7 1.查看安装中文包: 查看系统是否安装中文语言包 (列出所有可用的公共语言环境的名称,包含有zh_CN) # locale -a ...

  3. cmake opencv,dlib 编译静态库 1

    无论windows,linux 所有的库 ,dlib,opencv 通过cmake-gui 设置好静态库, 动态库,和其他各个选项 Tips: cmake 优先级用cmake-gui,因为命令太多,容 ...

  4. Wix 使用总结(续)--关于Feature和Component的状态判断安装过程(转)

    安装过程中,有时候需要根据用户的设置来进行不同的安装,其中一个方面就是根据用户选择安装的Feature或者Component,来判断下一步的操作.    Wix中提供了相关的判断方法和内置的状态值.  ...

  5. SpringMvc之参数绑定注解详解之一

    引言: 前段时间项目中用到了REST风格来开发程序,但是当用POST.PUT模式提交数据时,发现服务器端接受不到提交的数据(服务器端参数绑定没有加 任何注解),查看了提交方式为application/ ...

  6. Mysql 增量备份和全量备份

    全量备份: # vim /root/DBFullyBak.sh //添加以下内容 #!/bin/bash # Program # use mysqldump to Fully backup mysql ...

  7. JavaScript的内部对象

    JavaScript的内部对象 按创建方式不同分为:使用变量声明的隐性对象,使用new创建的显性对象 隐性对象 在赋值和声明后就是一个隐性对象,隐性对象不支持prototype属性,也无法随意扩展对象 ...

  8. Statement, PreparedStatement和CallableStatement的区别

    Statement用于执行不带参数的简单SQL语句,并返回它所生成的结果,每次执行SQL豫剧时,数据库都要编译该SQL语句. Satatement stmt = conn.getStatement() ...

  9. 1.7 xss之同源策略与跨域访问

    同源策略: 同源策略 在web应用的安全模型中是一个重要概念.在这个策略下,web浏览器允许第一个页面的脚本访问第二个页面里的数据,但是也只有在两个页面有相同的源时.源是由URI,主机名,端口号组合而 ...

  10. python sys.sdout.write 和print 区别

    sys.sdout.write 标准输入相当于“%value%”,输出内容没有空格,而print输出带有空格,举个例子 用sys.sdout.write: import sys for i in ra ...