2017杭电多校06Rikka with Graph
Rikka with Graph
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2377 Accepted Submission(s): 766
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).
4 5
Statistic | Submit | Clarifications | Back
题意:给出n个顶点,m条边,要求图的最小距离
思路:题中给出两点距离的值是经过的边数,所以尽可能的把所有的点连成菊花链;
分三种情况讨论:
1.当m<=n-1
分别计算连通点,孤立点,连通与孤立三种情况
result=(m+1-1)*(m+1-1)*2+(n-m-1)*(n-m-1)*n+(m+1)*(n-m-1)*2*n;
2.当m>n-1&&m<n*(n-1)/2
转换为边数为n*(n-1)/2的图的基础上删掉了n*(n-1)/2-m条边,每删掉一条边,距离数加上2
result=n*(n-1)+(n*(n-1)/2-m)*2;
3.当m>=n*(n-1)/2
显然此类情况图中所有的点都能直接相连,所以result=n*(n-1);
代码如下:
#include <iostream>
using namespace std;
typedef long long ll;
int main()
{
int t;
scanf("%d",&t);
ll n,m;
while(t--)
{
scanf("%lld%lld",&n,&m);
ll tmp=n*(n-1)/2;
ll result=0;
if(m>=tmp)
{
result=n*(n-1);
}
else if(m>n-1&&m<tmp)
{
result=(n*(n-1))+(tmp-m)*2;
}
else
{
ll p=m+1,q=n-m-1;
result=m*m*2+(n-m-1)*(n-m-2)*n+p*q*2*n;
}
cout<<result<<endl;
}
return 0;
}
2017杭电多校06Rikka with Graph的更多相关文章
- 2017杭电多校第六场1008 Kirinriki
传送门 Kirinriki Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) To ...
- 2017杭电多校第五场11Rikka with Competition
Rikka with Competition Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/O ...
- 2017杭电多校第五场Rikka with Subset
Rikka with Subset Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- 2017杭电多校第六场1011Classes
传送门 Classes Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Tota ...
- 2017杭电多校第六场03Inversion
传送门 Inversion Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) To ...
- 2017杭电多校第七场1011Kolakoski
Kolakoski Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others) Tota ...
- 2017杭电多校第七场1005Euler theorem
Euler theorem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others) ...
- 2017杭电ACM集训队单人排位赛 - 6
2017杭电ACM集训队单人排位赛 - 6 排名 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 59 1 X X 1 1 X X 0 1 ...
- 可持久化线段树的学习(区间第k大和查询历史版本的数据)(杭电多校赛第二场1011)
以前我们学习了线段树可以知道,线段树的每一个节点都储存的是一段区间,所以线段树可以做简单的区间查询,更改等简单的操作. 而后面再做有些题目,就可能会碰到一种回退的操作.这里的回退是指回到未做各种操作之 ...
随机推荐
- MySQL workbench8.0 CE基本用法(创建数据库、创建表、创建用户、设置用户权限、创建SQL语句脚本)
原文地址:https://blog.csdn.net/zgcr654321/article/details/82156277 安装完成MySQL后,打开MySQL workbench8.0. 可以看到 ...
- 使用GSON解析JSON文件
package com.pingyijinren.test; /** * Created by Administrator on 2016/5/19 0019. */ public class App ...
- Path Sum(参考别人,二叉树DFS)
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- NOIP 2009 潜伏者
P1071 潜伏者 题目描述 RR 国和 SS 国正陷入战火之中,双方都互派间谍,潜入对方内部,伺机行动.历尽艰险后,潜伏于 SS 国的 RR 国间谍小 CC 终于摸清了 SS 国军用密码的编码规则: ...
- Ubuntu 16.04安装GTX960闭源驱动
GTX960的闭源要Nvidia 346版才行,闭源驱动能很大提升显卡的性能,例如双显示输出等,缺点是不开源. 有以下方式来安装: 1.命令行: sudo add-apt-repository -y ...
- js算法:分治法-循环赛事日程表
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/ ...
- C++ auto 与 register、static keyword 浅析
[register/auto的比較分析] #include <iostream> using namespace std; int main(){ int i,sum=0; for(i=0 ...
- ≪统计学习精要(The Elements of Statistical Learning)≫课堂笔记(三)
照例文章第一段跑题,先附上个段子(转载的哦~): I hate CS people. They don't know linear algebra but want to teach projecti ...
- JspSmartUpload 实现上传
2.save 作用:将所有上传文件保存到指定文件夹下,并返回保存的文件个数. 原型:public int save(String destPathName) 和public int save(St ...
- Android仿微信朋友圈图片查看器
转载请注明出处:http://blog.csdn.net/allen315410/article/details/40264551 看博文之前,希望大家先打开自己的微信点到朋友圈中去,细致观察是不是发 ...