1 条题解
-
0
思维
1. 是否能构成树
题目中第三个条件:“与第 个房屋相连的房屋数量不能超过 ”。意味着第 个节点的度最多为 。
要使得图联通则最少需要 条边,则最少有 度。
结论: 则为
Yes,即 总和需要大于 .2. 最远距离
由于每条边长度都是 ,如果能连成一条直链,即为最长。
在直链下除了两个端点的度为 , 其他点的度都为 。只有度大于等于2的点才能作为中间节点链接两边;度为1只能作为端点,并且对于答案的贡献最多为2。
最长长度为:
#include<bits/stdc++.h> using namespace std; typedef long long LL; typedef pair<LL, LL> PII; #define x first #define y second const int N = 1e5 + 10, mod = 1e9 + 7; void solve() { int n; cin >> n; int one = 0, sum = 0; for(int i = 0; i < n; i ++) { int x; cin >> x; if(x == 1) one ++; sum += x; } if(sum < 2 * (n - 1)) cout << "No\n"; else { cout << "Yes\n"; cout << n - one + min(2, one) - 1 << endl; } } int main() { int t = 1; scanf("%d", &t); while(t --) solve(); }
- 1
信息
- ID
- 175
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 2
- 标签
- 递交数
- 36
- 已通过
- 7
- 上传者