Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 239 Accepted Submission(s): 150 Problem Description 度度熊最近似乎在研究图论。给定一个有 N 个点 (vertex) 以及 M 条边 (edge) 的无向简单图 (undirected simple graph),此图中保证没有任何圈 (cycle) 存在。
Input 输入的第一行有一个正整数 T ,代表接下来有几笔测试资料。
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 |
思路:并查集找联通块个数sum,以及最大入度,再比较sum+dmax+k-1与n;
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
using namespace std;
int f[200005];
int d[200005];
int mark[200005];
int _find(int x)
{
return x==f[x]?x:f[x]=_find(f[x]);
}
int main()
{
int t;
int n,m,k;
while(~scanf("%d",&t))
{
while(t--)
{
scanf("%d%d%d",&n,&m,&k);
for(int i=0;i<n;i++)
f[i]=i,d[i]=0;
//memset(d,0,sizeof(d));
//memset(mark,0,sizeof(mark));
int _max=0;
for(int i=0;i<m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
d[x]++;
d[y]++;
_max=max(_max,max(d[x],d[y]));
int xx=_find(x);
int yy=_find(y);
if(xx!=yy)
f[xx]=yy;
}
int sum=0;
for(int i=0;i<n;i++)
{
if(f[i]==i)sum++;
}
if(sum-1+_max+k>n-1)
printf("%d\n",n-1);
else
printf("%d\n",sum-1+_max+k);
}
}
return 0;
}