题目链接

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. 修改mac的hosts文件

    第一步:请先打开 Mac 系统中的 Finder 应用,接下来请按快捷键组合 Shift+Command+G 三个组合按键,并输入 Hosts 文件的所在路径:/etc/hosts , 随后即可在 F ...

  2. 在线webservice

    腾讯QQ在线状态 WEB 服务Endpoint: http://www.webxml.com.cn/webservices/qqOnlineWebService.asmx Disco: http:// ...

  3. PHP关于传众多参数还是传上下文对象的性能测试

    在开发微信公众平台平台的过程中,有这么几个参数总是需要传来传去,$userOpenId,$message,$time. 在整个程序的运行过程中,为了函数方便的处理,将这三个变量一直放在参数列表里.关于 ...

  4. nodejs 调试

    什么语言入门的准备功能就是写helloworld, 调试. 用惯了chrome的话,推荐用chrome自带的调试器来调试.很方便. 在地址栏中输入 chrome://inspect 并按回车,会打开如 ...

  5. SpringMVC源码剖析(五)-消息转换器HttpMessageConverter

    原文链接:https://my.oschina.net/lichhao/blog/172562 #概述 在SpringMVC中,可以使用@RequestBody和@ResponseBody两个注解,分 ...

  6. 第139天:详解cookie、 sessionStorage 和localStorage

    1.cookie:存储在用户本地终端上的数据.有时也用cookies,指某些网站为了辨别用户身份,进行session跟踪而存储在本地终端上的数据,通常经过加密.一般应用最典型的案列就是判断注册用户是否 ...

  7. 【Java并发编程】之三:线程挂起、恢复与终止的正确方法

    挂起和恢复线程 ​ Thread 的API中包含两个被淘汰的方法,它们用于临时挂起和重启某个线程,这些方法已经被淘汰,因为它们是不安全的,不稳定的.如果在不合适的时候挂起线程(比如,锁定共享资源时), ...

  8. python 锁 信号量 事件 队列

    什么是python 进程锁? #同步效率低,但是保证了数据安全  重点 很多时候,我们需要在多个进程中同时写一个文件,如果不加锁机制,就会导致写文件错乱 这个时候,我们可以使用multiprocess ...

  9. PHP通过SMTP实现发送邮件_包括附件

    require("class.phpmailer.php"); //这个是一个smtp的php文档,网上可以下载得到 $mail = new PHPMailer(); //建立邮件 ...

  10. innodb--表空间

    MySQL把数据库中表结构的定义信息保存到数据库目录的.frm文件中. 在InnoDB中数据库中存储的数据及索引实际是存放在表空间里的(tablespace). 可以将每个基于InnoDB存储引擎的表 ...