hdu6090 菊花图
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
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)
.
, the number of the testcases.
For each testcase, the first line contains two numbers n,m(1≤n≤106,1≤m≤1012)
.
#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 菊花图的更多相关文章
- CF981C(菊花图)
题目描述 RAMESS知道很多关于树的问题(无循环的无向连通图)! 他创建了一个新的有用的树的划分,但他不知道如何构造它,所以他请求你的帮助! 划分是从树上的边中分裂出一些简单的路径,使得每个两条路径 ...
- CF GYM 100781A(菊花图+直径)
题目大意 给出若干颗树用最少的边把它们连成一个无向连通图,同时使图的直径最小.输出最小直径. 题解 我们定义树的半径为(树的直径+1)/2.符合题意的连接方式为.所有树的“中点”连在直径最长的树的中点 ...
- bzoj1023: [SHOI2008]cactus仙人掌图
学习了一下圆方树. 圆方树是一种可以处理仙人掌的数据结构,具体见这里:http://immortalco.blog.uoj.ac/blog/1955 简单来讲它是这么做的:用tarjan找环,然后对每 ...
- 【构造】【贪心】hdu6090 Rikka with Graph
给你n个点,让你连m条边,使得任意两两点对之间的最短路的和最小(两点若不可达,最短路记作n). 初始时ans=n*n*(n-1). 先尽量连成菊花图,每连一次让答案减小2*((n-2)*(i-1)+( ...
- [ZJOI 2018] 线图
别想多了我怎么可能会正解呢2333,我只会30分暴力(好像现场拿30分已经不算少了2333,虽然我局的30分不是特别难想). 首先求k次转化的点数显然可以变成求k-1次转化之后的边数,所以我们可以先让 ...
- BZOJ 4289 最短路+优化建图
题意:给出一个N个点M条边的无向图,经过一个点的代价是进入和离开这个点的两条边的边权的较大值,求从起点1到点N的最小代价.起点的代价是离开起点的边的边权,终点的代价是进入终点的边的边权. 解法:参考h ...
- 【SDOI2017】天才黑客(前后缀优化建图 & 最短路)
Description 给定一张有向图,\(n\) 个点,\(m\) 条边.第 \(i\) 条边上有一个边权 \(c_i\),以及一个字符串 \(s_i\). 其中字符串 \(s_1, s_2, \c ...
- BZOJ 3784: 树上的路径
Description 问一棵树上前 \(k\) 大路径的边权. Sol 边分治. 非常感谢数据没有菊花图. 为了写写边分治试试然后就开了这道题. 边分治非常好想,选一条重边,分成两部分,然后分别求最 ...
- [NOIP2014]自测
这两天做完了2014年的noip提高. 因为以前看了SDSC2016时gty的课件,题目思路都知道了一点,做起来没多大困难. 100+100+75+100+100+70=545 里面水分好多,好多题都 ...
随机推荐
- JavaScript实现插入排序
一.插入排序简介: 想象我们斗地主,摸排阶段,手里的牌都按照从小到大排序.如果每摸一张牌,我们就把他插入合适的位置,使得它比后面位置的牌小,比前面位置的牌大或者相等. 类似这样的一种排序方法就是插入排 ...
- Codeforce-CodeCraft-20 (Div. 2)-B. String Modification (找规律+模拟)
Vasya has a string s of length n. He decides to make the following modification to the string: Pick ...
- Codeforce 1155D Beautiful Array(DP)
D. Beautiful Array You are given an array aa consisting of nn integers. Beauty of array is the maxim ...
- 从0开始学自定义View -1
PS:好久没有写博客了,之前的东西有所忘记,百度一下竟然查到了自己的写过的博客,访问量还可以,一开始的写博客的初衷是把自己不会的记录下来,现在没想到也有博友会关注我,这就给了我动力,工作之余把零零碎碎 ...
- 挑战程序竞赛 反转开关 poj3276
这个我其实也没有看太懂它的证明过程. 1.若某一个位置被翻转了n次,则其实际上被翻转了n%2次. 2.分析易知翻转的顺序并不影响最终结果. 3.现在我们着眼于第1个位置,可知若要将第1个位置进行翻转只 ...
- c#一些常用知识点
UID自动生成随机数 UID.Text = Guid.NewGuid().ToString(); GridView中常用格式化公式 <asp:BoundField DataField=" ...
- Android Bluetooth How To--Based on Android L Bluedroid
Android Bluetooth How To(Based on Android L Bluedroid) 持续更新中… 1.How to enable btsnoop log? a) UI Set ...
- Viterbi-Algorithm(维特比)算法
CSDN博客:皮乾东 知乎:Htrying 微博:Htring的微博 微信公众号:自然语言处理爱好者(ID:NLP_lover) 文章来自:<数学之美> Viterbi-Algor ...
- C# 数据操作系列 - 4. 自己实现一个ORM
0. 前言 在之前的几篇内容中,我们了解了如何通过ADO.NET 访问数据库,如何修改.新增数据.如何通过DataSet和DataAdapter获取数据,我们将在这一篇试试自己实现一个简单的ORM框架 ...
- Coursera课程笔记----C++程序设计----Week3
类和对象(Week 3) 内联成员函数和重载成员函数 内联成员函数 inline + 成员函数 整个函数题出现在类定义内部 class B{ inline void func1(); //方式1 vo ...
