2014-10-26 解題區/解題區 - UVa UVa 12786 - Friendship Networks contents 1. Problem2. Sample Input3. Sample Output4. Solution Problem類似 UVa 10720 - Graph Construction,給一個無向圖的每一個節點 degree,請問是否能構成圖。 Sample Input1234 3 2 2 34 1 3 2 38 2 5 4 5 4 3 5 2 Sample Output123101 Solution使用公式加二分。 Erdős–Gallai theorem 12345678910111213141516171819202122232425262728293031#include <stdio.h>#include <algorithm>#include <functional>using namespace std;int n, i, k;long long sum[1005], d[1005];int main() { while(scanf("%d", &n) == 1 && n) { sum[0] = 0; for(i = 0; i < n; i++) scanf("%lld", &d[i]); sort(d, d+n, greater<int>()); for(i = 0; i < n; i++) sum[i+1] = sum[i] + d[i]; long long left = 0, right; int ret = 1; if(sum[n]&1) ret = 0; for(k = 0; k < n; k++) { left += d[k]; right = (long long)k * (k+1); int l = lower_bound(d , d + n, k+1, greater<int>()) - d; if(l < k+1) right += sum[n] - sum[k+1]; else right += sum[n] - sum[l] + (long long)(k+1)*(l - k - 1); if(left > right) ret = 0; } printf("%d\n", ret); } return 0;} Newer UVa 12809 - Binary Search Tree Older UVa 12776 - Query for Divisor-free Numbers