2018 “百度之星”程序设计大赛 - 初赛(B)1001degree

2018 “百度之星”程序设计大赛 - 初赛(B) 

degree

 Accepts: 1581

 Submissions: 3494

 Time Limit: 2000/1000 MS (Java/Others)

 Memory Limit: 131072/131072 K (Java/Others)

Problem Description

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

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

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

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

Input

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

对于每笔测试资料: 第一行有三个整数 NN, MM, KK。 接下来的 MM 行每行有两个整数 aa 及 bb,代表点 aa 及 bb 之间有一条边。 点的编号由 00 开始至 N - 1N−1。

  • 0 \le K \le M \le 2 \times 10^50≤K≤M≤2×10​5​​
  • 1 \le N \le 2 \times 10^51≤N≤2×10​5​​
  • 0 \le a, b < N0≤a,b<N
  • 给定的图保证是没有圈的简单图
  • 1 \le T \le 231≤T≤23
  • 至多 22 笔测试资料中的 N > 1000N>1000

Output

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

Sample Input

Copy

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

思路:把尽可能多的间接与入度最大的点相连的点变成与它直接直接相连。

看到无环图,就想到了生成树(虽然并不是,这大概是个生成森林??不知道有没有这个词,反正就是有很多连通分支,每个连通分支都是一个生成树),然后它可以增加无限条边最多删k条边保持依旧是无环图,要得到入度最大的点的最大入度,

要入度最大的话,显然第一步可以加边在其他连通分支与入度最大的点x之间加一条边,此时就变成了一颗生成树,所有的点都与入度最大的点直接或间接相连,现在不删边加任何一条边都会出现环,所以接下来就看能把多少间接相连的变成直接相连的啦(能删一条边就能把一个点与x直接相连)。看代码

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <vector>
#include <malloc.h>
#define Twhile() int T;scanf("%d",&T);while(T--)
#define clc(a,b,n) for(int i=0;i<=n;i++)a[i]=b
#define clc2(a,b,n,m) for(int i=0;i<=n;i++)for(int j=0;j<=m;j++)a[i][j]=b
#define fora(i,a,b) for(int i=a;i<b;i++)
#define fors(i,a,b) for(int i=a;i>b;i--)
#define fora2(i,a,b) for(int i=a;i<=b;i++)
#define fors2(i,a,b) for(int i=a;i>=b;i--)
#define PI acos(-1.0)
#define eps 1e-6
#define INF 0x3f3f3f3f
#define BASE 131

typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
using namespace std;
const int maxn=200000+11;
int N,M,K;
int in[maxn];
int main()
{
    Twhile()
    {
        scanf("%d%d%d",&N,&M,&K);
        int cou=0;
        memset(in,0,sizeof(in));
        fora2(i,1,M)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            x++;y++;
            in[x]++;
            in[y]++;
            cou=max(cou,max(in[x],in[y]));
        }
 
        if(M-cou<=K)printf("%d\n",N-1);
        else
            printf("%d\n",cou+K+(N-1-M));

    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/liyang__abc/article/details/81611817