Rikka with Graph

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 239    Accepted Submission(s): 157

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

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
    int T;
    for(scanf("%d",&T); T--;)
    {
        long long  n,m;
        scanf("%I64d%I64d",&n,&m);
        long long maxx=n*(n-)/;
        if(m>=maxx) printf("%I64d\n",maxx*);
        else if(m>=n-)  printf("%I64d\n",*(n-)*(n-)-*(m-n+));
        else printf("%I64d\n",*m*m+(n-m-)*(m+)*n*+(n-m-)*(n-m-)*n);
    }
}
 

hdu6090 菊花图的更多相关文章

  1. CF981C(菊花图)

    题目描述 RAMESS知道很多关于树的问题(无循环的无向连通图)! 他创建了一个新的有用的树的划分,但他不知道如何构造它,所以他请求你的帮助! 划分是从树上的边中分裂出一些简单的路径,使得每个两条路径 ...

  2. CF GYM 100781A(菊花图+直径)

    题目大意 给出若干颗树用最少的边把它们连成一个无向连通图,同时使图的直径最小.输出最小直径. 题解 我们定义树的半径为(树的直径+1)/2.符合题意的连接方式为.所有树的“中点”连在直径最长的树的中点 ...

  3. bzoj1023: [SHOI2008]cactus仙人掌图

    学习了一下圆方树. 圆方树是一种可以处理仙人掌的数据结构,具体见这里:http://immortalco.blog.uoj.ac/blog/1955 简单来讲它是这么做的:用tarjan找环,然后对每 ...

  4. 【构造】【贪心】hdu6090 Rikka with Graph

    给你n个点,让你连m条边,使得任意两两点对之间的最短路的和最小(两点若不可达,最短路记作n). 初始时ans=n*n*(n-1). 先尽量连成菊花图,每连一次让答案减小2*((n-2)*(i-1)+( ...

  5. [ZJOI 2018] 线图

    别想多了我怎么可能会正解呢2333,我只会30分暴力(好像现场拿30分已经不算少了2333,虽然我局的30分不是特别难想). 首先求k次转化的点数显然可以变成求k-1次转化之后的边数,所以我们可以先让 ...

  6. BZOJ 4289 最短路+优化建图

    题意:给出一个N个点M条边的无向图,经过一个点的代价是进入和离开这个点的两条边的边权的较大值,求从起点1到点N的最小代价.起点的代价是离开起点的边的边权,终点的代价是进入终点的边的边权. 解法:参考h ...

  7. 【SDOI2017】天才黑客(前后缀优化建图 & 最短路)

    Description 给定一张有向图,\(n\) 个点,\(m\) 条边.第 \(i\) 条边上有一个边权 \(c_i\),以及一个字符串 \(s_i\). 其中字符串 \(s_1, s_2, \c ...

  8. BZOJ 3784: 树上的路径

    Description 问一棵树上前 \(k\) 大路径的边权. Sol 边分治. 非常感谢数据没有菊花图. 为了写写边分治试试然后就开了这道题. 边分治非常好想,选一条重边,分成两部分,然后分别求最 ...

  9. [NOIP2014]自测

    这两天做完了2014年的noip提高. 因为以前看了SDSC2016时gty的课件,题目思路都知道了一点,做起来没多大困难. 100+100+75+100+100+70=545 里面水分好多,好多题都 ...

随机推荐

  1. 使用VSCode连接到IBM Cloud区块链网络

    文章目录 从IBM Cloud控制面板导出连接信息 在VSCode中创建gateway和wallet 在VSCode中提交transaction 上篇文章我们讲到怎么在IBM Cloud搭建区块链环境 ...

  2. UVALive 7509 Dome and Steles

    三分 #include<bits/stdc++.h> using namespace std; #define rep(i,a,b) for(int i=a;i<=b;++i) #d ...

  3. 企业云桌面-03-安装第1个企业 CA-013-CA01

    作者:学 无 止 境 QQ交流群:454544014 注意: <企业云桌面>系列博文是<企业云桌面规划.部署与运维实践指南>的基础部分,因为书中内容涉及非常多,非常全面,所以基 ...

  4. React技术栈——Redux

    Redux 1.Redux是什么?   Redux对于JavaScript应用而言是一个可预测状态的容器.换言之,它是一个应用数据流框架,而不是传统的像underscore.js或者AngularJs ...

  5. Spring源码学习——自定义标签

    2019独角兽企业重金招聘Python工程师标准>>> 1.自定义标签步骤 创建一个需要扩展的组件 定义xsd文件描述组件内容 创建一个文件,实现BeanDefinitionPars ...

  6. 解决Visual Studio (VS) 插件下载缓慢

    1.关闭IPV6协议 因为如果都支持IPV6协议,会自动使用IPV6下载扩展. 因为IPV6还没有建立完善,所以可能会比较慢. 百度经验:怎样关闭IPV6协议 2.给IPV6添加DNS 百度: 240 ...

  7. 图论--网络流--最大流--POJ 3281 Dining (超级源汇+限流建图+拆点建图)

    Description Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, an ...

  8. springboot后端校验

    这一篇讲解了如何定义特殊的校验 https://www.cnblogs.com/cjsblog/p/8946768.html https://blog.csdn.net/xgblog/article/ ...

  9. 学习vue第七节,filter过滤器如何的使用

    vue 过滤器如何的使用 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...

  10. R - Weak Pair HDU - 5877 离散化+权值线段树+dfs序 区间种类数

    R - Weak Pair HDU - 5877 离散化+权值线段树 这个题目的初步想法,首先用dfs序建一颗树,然后判断对于每一个节点进行遍历,判断他的子节点和他相乘是不是小于等于k, 这么暴力的算 ...