完美网络
Time Limit: 1000MS Memory limit: 65536K
题目描述
完美网络是连通网络的基础上要求去掉网络上任意一条线路,网络仍然是连通网络。求一个连通网络要至少增加多少条边可以成为完美网络。
输入
第一行输入一个数T代表测试数据个数(T<=20)。每个测试数据第一行2个数n,m 分别代表网络基站数和基站间线路数。基站的序号为从1到n。接下来m行两个数代表x,y 代表基站x,y间有一条线路。
(
0 < n < m < 10000)
输出
对于每个样例输出最少增加多少线路可以成为完美网络。每行输出一个结果。
示例输入
2 3 1 1 2 3 2 1 2 2 3
示例输出
2 1
提示
<pre name="code" class="cpp">#include<bits/stdc++.h>
using namespace std;
int d[1000100];
int main()
{
int T,n,m,u,v;
cin>>T;
while(T--)
{
memset(d,0,sizeof(d));
cin>>n>>m;
for(int i=0;i<m;i++)
{
cin>>u>>v;
d[u]++;
d[v]++;
}
int count=0;
priority_queue<int,vector<int>, greater<int> >q;
for(int i=1;i<=n;i++)
{
if(d[i]<2)
{
q.push(d[i]);
}
}
while(q.size()>=2)
{
int t1=q.top();
q.pop();
int t2=q.top();
q.pop();
t1++;
t2++;
count++;
if(t1<2)
{
q.push(t1);
}
if(t2<2)
{
q.push(t2);
}
}
if(!q.empty())
{
count++;
}
cout<<count<<endl;
}
return 0;
}