每日一题 第五十期 Codeforces Round 937 (Div. 4)

Analgorithmicproblemaskingfortheminimumpossibleheightofarootedtreewherespecificverticeshaveexactly2,1,or0children.Outputs-1ifnosuchtreeexists.
摘要由CSDN通过智能技术生成

F. 0, 1, 2, Tree!

time limit per test: 2 seconds

memory limit per test: 256 megabytes

input: standard input

output: standard output

Find the minimum height of a rooted tree † ^{\dagger} with a + b + c a+b+c a+b+c vertices that satisfies the following conditions:

  • a a a vertices have exactly 2 2 2 children,
  • b b b vertices have exactly 1 1 1 child, and
  • c c c vertices have exactly 0 0 0 children.

If no such tree exists, you should report it.

The tree above is rooted at the top vertex, and each vertex is labeled with the number of children it has. Here a = 2 a=2 a=2, b = 1 b=1 b=1, c = 3 c=3 c=3, and the height is 2 2 2.

† ^{\dagger} A rooted tree is a connected graph without cycles, with a special vertex called the root. In a rooted tree, among any two vertices connected by an edge, one vertex is a parent (the one closer to the root), and the other one is a child.

The distance between two vertices in a tree is the number of edges in the shortest path between them. The height of a rooted tree is the maximum distance from a vertex to the root.

Input

The first line contains an integer t t t ( 1 ≤ t ≤ 1 0 4 1 \leq t \leq 10^4 1t104) — the number of test cases.

The only line of each test case contains three integers a a a, b b b, and c c c ( 0 ≤ a , b , c ≤ 1 0 5 0 \leq a, b, c \leq 10^5 0a,b,c105; 1 ≤ a + b + c 1 \leq a + b + c 1a+b+c).

The sum of a + b + c a + b + c a+b+c over all test cases does not exceed 3 ⋅ 1 0 5 3 \cdot 10^5 3105.

Output

For each test case, if no such tree exists, output − 1 -1 1. Otherwise, output one integer — the minimum height of a tree satisfying the conditions in the statement.

Example
inputCopy
10
2 1 3
0 0 1
0 1 1
1 0 2
1 1 3
3 1 4
8 17 9
24 36 48
1 0 0
0 3 1
outputCopy
2
0
1
1
-1
3
6
-1
-1
3

AC代码:

#include<map>
#include<set>
#include<stack>
#include<cmath>
#include<queue>
#include<string>
#include<bitset>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<numeric>
//#define endl '\n'
using namespace std;

typedef long long ll;
typedef pair<int, int>PII;
const int N=3e5+10;
const int MOD=998244353;
const int INF=0X3F3F3F3F;
const int dx[]={-1,1,0,0,-1,-1,+1,+1};
const int dy[]={0,0,-1,1,-1,+1,-1,+1};
const int M = 1e8 + 10;

//树的构造
int t;
int main()
{
	cin >> t;
	while(t --){
		int a, b, c;
		cin >> a >> b >> c;
		int len = 0, num = 1;
		if(a + 1 != c) puts("-1");
		else {
			while(a){
				if(a >= num)
				{
					a -= num;
					num *= 2;
				}
				else {
					int nex = num + a;
					num -= a;
					a = 0;
					int l =min(num, b);
					b -= l;
					num = nex;
				}
				len ++;
			}
			while(b > 0){
				b -= num;
				len ++;
			}
			cout << len << endl;
		}
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值