B. Diameter of Graph-Codeforces Round #745 (Div. 2)

原文链接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;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qinsanma and Code

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值