E. Construct the Binary Tree

                                                          E. Construct the Binary Tree

You are given two integers nn and dd. You need to construct a rooted binary tree consisting of nn vertices with a root at the vertex 11 and the sum of depths of all vertices equals to dd.

A tree is a connected graph without cycles. A rooted tree has a special vertex called the root. A parent of a vertex vv is the last different from vv vertex on the path from the root to the vertex vv. The depth of the vertex vv is the length of the path from the root to the vertex vv. Children of vertex vv are all vertices for which vv is the parent. The binary tree is such a tree that no vertex has more than 22 children.

You have to answer tt independent test cases.

Input

The first line of the input contains one integer tt (1≤t≤10001≤t≤1000) — the number of test cases.

The only line of each test case contains two integers nn and dd (2≤n,d≤50002≤n,d≤5000) — the number of vertices in the tree and the required sum of depths of all vertices.

It is guaranteed that the sum of nn and the sum of dd both does not exceed 50005000 (∑n≤5000,∑d≤5000∑n≤5000,∑d≤5000).

Output

For each test case, print the answer.

If it is impossible to construct such a tree, print "NO" (without quotes) in the first line. Otherwise, print "{YES}" in the first line. Then print n−1n−1 integers p2,p3,…,pnp2,p3,…,pn in the second line, where pipi is the parent of the vertex ii. Note that the sequence of parents you print should describe some binary tree.

Example

input

Copy

3
5 7
10 19
10 18

output

Copy

YES
1 2 1 3 
YES
1 2 3 3 9 9 2 1 6 
NO

Note

Pictures corresponding to the first and the second test cases of the example:

 

题意:

构造一个二叉树,每个节点的长度为根节点到该节点的距离,满足n个节点使满足有所有节点总长度为d。

分析:

构造二叉树,将最后一个节点选择最优的位置放置。

代码:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int i,j,t,n,d,l,r,p,pre;
    int v[54321];
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&d);
        for(i=0;i<=n;i++)v[i]=1;
        p=n*(n-1)/2;
        p-=d;
        l=1;
        r=n-1;
        pre=1;
        while(l<r)
        {
            while(p<r-l||(v[l]==v[l-1]*2&&l<r))l++;
            if(l<r){
                v[l]++;
                p-=r-l;
                r--;
            }
        }
        pre=1;
        if(p!=0)printf("NO\n");
        else
        {
            printf("YES\n");
            for(i=1;i<=l;i++)
            {
                for(j=1;j<=v[i];j++)
                    printf("%d ",pre+(j-1)/2);
                pre+=v[i-1];
            }
            printf("\n");
        }
    }
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值