Diameter of Graph

CQXYM wants to create a connected undirected graph with n nodes and m edges, and the diameter of the graph must be strictly less than k−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) — 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), m, k (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为点数,m为边数。有关系:n-1 <= m <= n*(n-1)/2. 设相连的两点之间距离为1

当m=n-1时:此时图形为树,树的一个点到一个点的最小距离2,(就是都连在父结点上)此时任意两点之间距离大于等于2

当m=n*(n-1)/2 时:n*(n-1)/2说明为每两个点之间都有连接,此时的图就是满状态,如下图 

 此时任意两点之间距离就是1.

当n-1 < m <n*(n-1)/2 时:任意两点之间的距离就是两个边界情况之中,也就是1到2,从1点多多不到2.

注意:当n很小时 m满足 n-1 时此时有可能也满足满状态,满状态优先。

#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;

typedef long long ll;

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        ll n,m,k;
        cin>>n>>m>>k;
        if(n==1)
        {
            if(!m&&k>=2)
            {
                cout<<"YES"<<endl;
            }
            else
            {
                cout<<"NO"<<endl;
            }
        }
        else if(m<n-1)
        {
            cout<<"NO"<<endl;
        }
        else if(m>= n-1&&m<n*(n-1)/2)
        {
            if(k<=3)
            {
                cout<<"NO"<<endl;
            }
            else
            {
                cout<<"YES"<<endl;
            }
        }
        else if(m==n*(n-1) / 2)
        {
            if(k<=2)
            {
                cout<<"NO"<<endl;
            }
            else
            {
                cout<<"YES"<<endl;
            }
        }
        else
        {
            cout<<"NO"<<endl;
        }
        
    }
    return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值