原文链接https://codeforces.com/contest/1581/problem/B
B. Diameter of Graph
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
CQXYM wants to create a connected undirected graph with nn nodes and mm edges, and the diameter of the graph must be strictly less than k−1k−1. Also, CQXYM doesn't want a graph that contains self-loops or multiple edges (i.e. each edge connects two different vertices and between each pair of vertices there is at most one edge).
The diameter of a graph is the maximum distance between any two nodes.
The distance between two nodes is the minimum number of the edges on the path which endpoints are the two nodes.
CQXYM wonders whether it is possible to create such a graph.
Input
The input consists of multiple test cases.
The first line contains an integer t(1≤t≤105)t(1≤t≤105) — the number of test cases. The description of the test cases follows.
Only one line of each test case contains three integers n(1≤n≤109)n(1≤n≤109), mm, kk (0≤m,k≤109)(0≤m,k≤109).
Output
For each test case, print YES if it is possible to create the graph, or print NO if it is impossible. You can print each letter in any case (upper or lower).
Example
input
Copy
5 1 0 3 4 5 3 4 6 3 5 4 1 2 1 1
output
Copy
YES NO YES NO NO
Note
In the first test case, the graph's diameter equal to 0.
In the second test case, the graph's diameter can only be 2.
In the third test case, the graph's diameter can only be 1.
首先,自环的定义是一个节点出发一条边连接到自身。
其次,一个n个节点的图,所能连接的最少边为n-1(此时图退化成链)最多是(n-1)*n/2,此时称之为完全图,完全图特性为,图的直径是1,即任意一个节点都和剩下的n-1个节点直接相连;
所以,首先可以排除掉 m<n-1||m>(n-1)*n/2的情况,前者出现孤立点,后者出现自环和重边;如果是完全图,直径必须是1;如果是普通图,我们来看,随着k值降低,条件越来越苛刻,所以我们必须构造的直径应该越小越好,但最小的情况(完全图)无法构造
我们构造菊花图
菊花图需要n-1条边,仅次于完全图的优越性。
然后这是m=n-1的情况,m再大些,可以向上面加边,加到(n-1)*n/2才达到完全图的地步,但无法到达,只能不断逼近,这个过程,直径始终是2;
注意,题目是严格小于k-1,也就是直径要小于等于k-2;
分类讨论即可,n=1的情况,单独伶出来,开long long
# include <iostream>
# include<algorithm>
# include<cstring>
# include<map>
using namespace std;
typedef long long int ll;
int main()
{
int t;
cin>>t;
while(t--)
{
ll n,m,k;
cin>>n>>m>>k;
ll d=k-2;
if(m<n-1||m>(n-1)*n/2)
{
cout<<"NO"<<'\n';
continue;
}
if(n==1)
{
if(m!=0)
{
cout<<"NO"<<'\n';
continue;
}
else
{
if(d>=0)
{
cout<<"YES"<<'\n';
}
else
{
cout<<"NO"<<'\n';
}
}
}
else if(m==(n-1)*n/2)
{
if(d>=1)
{
cout<<"YES"<<'\n';
}
else
{
cout<<"NO"<<'\n';
}
}
else
{
if(d>=2)
{
cout<<"YES"<<'\n';
}
else
{
cout<<"NO"<<'\n';
}
}
}
return 0;
}