题解:CF1950F 0, 1, 2, Tree!

· · 题解

题目链接

思路

不能形成树的情况:

完整代码

#include <cstdio> 
#include <iostream> 
#include <algorithm>
using namespace std;


int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        int a, b, c;
        cin >> a >> b >> c;
        if (!c || a + 1 != c)
            cout << "-1" << endl;
        else
        {
            int cnt = 0, s = 1;
            while (s < a + 1)
            {
                cnt++;
                s *= 2;
            }
            if (b > s - a - 1)
                cnt++;
            if (b > s && b > a + 1)
                cnt += (b - s + a) / (a + 1);
            cout << cnt << endl;
        }
    }
    return 0;
}