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. ajax 技术

    ajax 技术 $.ajax({ url:"", type:'GET', success:function(data){ console.log(data); }, error:f ...

  2. Redis数据迁移的三个方法

    为什么80%的码农都做不了架构师?>>>   1. rdb数据备份恢复方法 redis 127.0.0.1:6379> SAVE OK 或者 redis-cli -h 127. ...

  3. unix域源码解析

    首先我们先要创建一个用于通信的结构unix_proto_data ,并初始化某些字段 static int unix_proto_create(struct socket *sock, int pro ...

  4. zabbix3.x.x升级教程

    1:停掉正在运行的zabbix服务,确保没有新数据写入数据库. /etc/init.d/zabbix_server stop 2:备份原zabbix的数据库数据,以及相关文件. mysqldump - ...

  5. P3588 【[POI2015]PUS】(线段树优化建边)

    P3588 [[POI2015]PUS] 终于有个能让我一遍过的题了,写篇题解纪念一下 给定长度为n的序列和其中部分已知的数,还有m个大小关系:区间\([l,r]\)中,有k个给定的数比剩下的\(r- ...

  6. python(类继承)

    一.继承 1.单继承 一个对象使用另一个对象的属性和方法,被继承的类也称父类 (1)父类与子类的方法不一样 class Four(): def sub(self,x,y): return x + y ...

  7. 洛谷P5018 对称二叉树

    不多扯题目 直接题解= = 1.递归 由题目可以得知,子树既可以是根节点和叶节点组成,也可以是一个节点,题意中的对称二叉子树是必须由一个根节点一直到树的最底部所组成的树. 这样一来就简单了,我们很容易 ...

  8. Java笔记(day13)

    多线程: 进程:正在进行中的程序(直译) 线程:执行路径,就是进程中负责程序执行的控制单元(执行路径): 一个进程中可以多个路径,称为多线程 一个进程至少一个线程 每一个线程都有自己运行的内容,这个内 ...

  9. JUC之CAS

    CAS(全称为CompareAndSwap,也有说是CompareAndSet,都差不多)是一条CPU并发原语,它的功能是判断内存某个位置的值是否为预期值,如果是则更改为新的值,判断预期值和更改新值的 ...

  10. 环境篇:Atlas2.0.0兼容CDH6.2.0部署

    环境篇:Atlas2.0.0兼容CDH6.2.0部署 Atlas 是什么? Atlas是一组可扩展和可扩展的核心基础治理服务,使企业能够有效地满足Hadoop中的合规性要求,并允许与整个企业数据生态系 ...