题目链接

Problem Description

As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them:

For an undirected graph G with n nodes and m edges, we can define the distance between (i,j) (dist(i,j)) as the length of the shortest path between i and j. The length of a path is equal to the number of the edges on it. Specially, if there are no path between i and j, we make dist(i,j) equal to n.

Then, we can define the weight of the graph G (wG) as ∑ni=1∑nj=1dist(i,j).

Now, Yuta has n nodes, and he wants to choose no more than m pairs of nodes (i,j)(i≠j) and then link edges between each pair. In this way, he can get an undirected graph G with n nodes and no more than m edges.

Yuta wants to know the minimal value of wG.

It is too difficult for Rikka. Can you help her?

In the sample, Yuta can choose (1,2),(1,4),(2,4),(2,3),(3,4).

Input

The first line contains a number t(1≤t≤10), the number of the testcases.

For each testcase, the first line contains two numbers n,m(1≤n≤106,1≤m≤1012).

Output

For each testcase, print a single line with a single number -- the answer.

Sample Input

1

4 5

Sample Output

14

题意:

n个点,你可以连接任意<=m条边,得到图G,WG = ∑ ∑ dist(i,j) 其中(ij都是从1到n)

dis(i,j)定义为从i到j经过的边的个数。如果从i无法到达j,则定义为n

问 怎么连边能让WG最小,最小的wg是多少

分析:

1.首先可以确定的一点肯定是边越多越好,这样才能保证尽可能多的两点直接相连,所以在不超过完全图的边的个数情况下,边的条数尽可能大,如果超过达到完全图所需要的边数即m>=n(n-1)/2,那么变成完全图,即答案是n(n-1) ,因为每一条边都要考虑两个方向

2.否则有两种情况(即没有达到完全图的情况)

(1).m>=n-1(相当于能构成一个连通图,任意两点之间可以直接或者间接的到达)

如果图恰好能组成深度为2的树(有一个点为中心(跟节点),直接连接着剩余的n-1个点),那么根节点和剩下n-1个点距离是1,剩余n-1个节点之间都能够通过根节点间接的到达,距离互相都是2,此时这棵树的WG=2*((n-1)+(2*(n-1)*(n-2))/2)

但是我们要考虑没有这么恰好的情况,组成一个连通图用n-1条边,那么剩下m-(n-1)条边当然是连在距离为2的点之间最好,那么此时这棵树的WG=2*( (n-1) + (2*(n-1)*(n-2))/2- (m-(n-1)) ) = 2*(n^2-n-m)

(2).m<n-1 (连通图都构不成,肯定存在不管直接还是间接都无法相连的边)

即无法组成连通图,那么使用m条边,m+1个点按照前一种情况组成一棵树,这棵树的WG1 = 2*(m)^2,余下的点之间都是不通的都是n。组成树的这x个点和其余n-x个点距离和是 WG2=2*n*(m+1)*(n-m-1),,未组成树的n-x个点和除自己之外每个点距离和是WG3=n*(n-m-1)*(n-m-2),故WG =WG1+WG2+WG3

代码:

#include <iostream>
#include <cstdio>
using namespace std;
typedef long long ll;
ll n, m;
int main()
{
int t;
scanf("%d",&t);
while (t--)
{
scanf("%lld%Illd",&n,&m);
ll ans = 0;
if(m>=(n*(n-1))/2) ans = n*(n-1);///n个点的胡话,最多连接(n*(n-1))/2条边就能保证任意的两个点直接相连
else if (m >= n-1)
{
ans = 2*(n*n - n - m);
}
else
{
ans= 2*m*m+(n-m-1)*(m+1)*n*2+(n-m-1)*(n-m-2)*n;
}
printf("%lld\n",ans);
}
return 0;
}

2017ACM暑期多校联合训练 - Team 5 1006 HDU 5205 Rikka with Graph (找规律)的更多相关文章

  1. 2017ACM暑期多校联合训练 - Team 2 1006 HDU 6050 Funny Function (找规律 矩阵快速幂)

    题目链接 Problem Description Function Fx,ysatisfies: For given integers N and M,calculate Fm,1 modulo 1e ...

  2. 2017ACM暑期多校联合训练 - Team 7 1010 HDU 6129 Just do it (找规律)

    题目链接 Problem Description There is a nonnegative integer sequence a1...n of length n. HazelFan wants ...

  3. 2017ACM暑期多校联合训练 - Team 8 1006 HDU 6138 Fleet of the Eternal Throne (字符串处理 AC自动机)

    题目链接 Problem Description The Eternal Fleet was built many centuries ago before the time of Valkorion ...

  4. 2017ACM暑期多校联合训练 - Team 5 1001 HDU 6085 Rikka with Candies (模拟)

    题目链接 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, s ...

  5. 2017ACM暑期多校联合训练 - Team 2 1011 HDU 6055 Regular polygon (数学规律)

    题目链接 **Problem Description On a two-dimensional plane, give you n integer points. Your task is to fi ...

  6. 2017ACM暑期多校联合训练 - Team 1 1006 HDU 6038 Function (排列组合)

    题目链接 Problem Description You are given a permutation a from 0 to n−1 and a permutation b from 0 to m ...

  7. 2017ACM暑期多校联合训练 - Team 4 1004 HDU 6070 Dirt Ratio (线段树)

    题目链接 Problem Description In ACM/ICPC contest, the ''Dirt Ratio'' of a team is calculated in the foll ...

  8. 2017ACM暑期多校联合训练 - Team 9 1005 HDU 6165 FFF at Valentine (dfs)

    题目链接 Problem Description At Valentine's eve, Shylock and Lucar were enjoying their time as any other ...

  9. 2017ACM暑期多校联合训练 - Team 9 1010 HDU 6170 Two strings (dp)

    题目链接 Problem Description Giving two strings and you should judge if they are matched. The first stri ...

随机推荐

  1. IntelliJ IDEA 创建 hello world Java web Maven项目从头到尾都有图有真相2017版本

    学Java的大部分吧都是要整Java web开发项目的,那么最好用的编辑器估计就是这个 IntelliJ IDEA,然后现在maven管理项目是很流行的.然后我就示范一下,如何使用这个IntelliJ ...

  2. DAVY的神龙帕夫——读者的心灵故事|十二橄榄枝的传说

    再次听Puff的时候我想起了Davy. 文理分班后我坐到了他后面.Davy天生一头黄毛,黑头发”not even one”.上课时他若不是肆无忌惮地舒开四肢呼呼大睡,便是如受惊一般伸长他的细脖子,直挺 ...

  3. QoS专题-第3期-QoS实现之报文简单分类与标记

    QoS实现之报文简单分类与标记 上一期专题我们讲到,MQC中的流分类可以实现报文的分类,流行为可以对报文进行重标记,从而实现对流量的精细化差分服务.而优先级映射则可以根据802.1p优先级.DSCP优 ...

  4. Astronauts UVALive - 3713(2-SAT)

    大白书例题 #include <iostream> #include <cstdio> #include <sstream> #include <cstrin ...

  5. 【刷题】BZOJ 3926 [Zjoi2015]诸神眷顾的幻想乡

    Description 幽香是全幻想乡里最受人欢迎的萌妹子,这天,是幽香的2600岁生日,无数幽香的粉丝到了幽香家门前的太阳花田上来为幽香庆祝生日. 粉丝们非常热情,自发组织表演了一系列节目给幽香看. ...

  6. excel换行

    在excel的单元格中换行 1. windows alt + enter 2. mac command + alt + enter

  7. 51nod 1623 完美消除(数位DP)

    首先考虑一下给一个数如何求它需要多少次操作. 显然用一个单调栈就可以完成:塞入栈中,将比它大的所有数都弹出,如果栈中没有当前数,答案+1. 因为数的范围只有0~9,所以我们可以用一个二进制数来模拟这个 ...

  8. Git 自动补全

    如果你用的是 Bash shell,可以试试看 Git 提供的自动补全脚本. http://git-scm.com/download 下载 Git 的源代码,进入contrib/completion  ...

  9. Centos 7.3 下 的QT 输入中文

    我的QT 目录在 /opt/Qt/Tools/QtCreator,进入到 /bin 目录,然后运行 ./qtcreator.sh  就可以运行 在qtcreator.sh 中加入 export QT_ ...

  10. C++中添加配置文件读写方法

    比如有一个工程,一些变量有可能需要不时的修改,这时候可以通过从配置文件中读取该数值,需要修改时只需要修改配位文件即可. 比如有一个这样的变量m_nTest; 我么可以写两个函数ReadConfig() ...