【2018 “百度之星”程序设计大赛

Problem Description

度度熊最近似乎在研究图论。给定一个有 N 个点 (vertex) 以及 M 条边 (edge) 的无向简单图 (undirected simple graph),此图中保证没有任何圈 (cycle) 存在。

现在你可以对此图依序进行以下的操作:

1. 移除至多 K 条边。
2. 在保持此图是没有圈的无向简单图的条件下,自由的添加边至此图中。

请问最后此图中度数 (degree) 最大的点的度数可以多大呢?
 

Input

输入的第一行有一个正整数 T,代表接下来有几笔测试资料。

对于每笔测试资料:
第一行有三个整数 NMK
接下来的 M 行每行有两个整数 a 及 b,代表点 a 及 b 之间有一条边。
点的编号由 0 开始至 N1

0KM2×105
1N2×105
0a,b<N
* 给定的图保证是没有圈的简单图
1T23
* 至多 2 笔测试资料中的 N>1000
 

Output

对于每一笔测试资料,请依序各自在一行内输出一个整数,代表按照规定操作后可能出现的最大度数。
 

Sample Input

2
3 1 1
1 2
8 6 0
1 2
3 1
5 6
4 1
6 4
7 0

Sample Output

2 4
 
 
 

 题解

假如不用删边,那么ans = maxdegree + 树的个数 - 1,假如能删k条边,那么ans += k,但是ans最多只能到n - 1,所以答案就是min(n - 1, maxdegree + 树的个数 - 1 + k)。这里树的个数 = 点数 - 边数。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 200005;
int in[N];
int main()
{
    int n, t, m, k;
    cin>>t;
    while(t--)
    {
        memset(in, 0, sizeof in);
        scanf("%d%d%d", &n, &m, &k);
        for(int i = 1; i <= m; i++)
        {
            int a, b;
            scanf("%d%d", &a, &b);
            in[a] ++;
            in[b] ++;
        }
        int tree_c = n - m;
        int j = 0;
        for(int i = 1; i < n; i++)
        {
            if(in[j] < in[i])
                j = i;
        }
        int ans = min(n - 1, in[j] + tree_c - 1 + k);
        printf("%d
", ans);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/lesroad/p/9468039.html