Time Limit: 1000MS Memory limit: 65536K
题目描述
输入
输出
示例输入
2 3 1 1 2 3 2 1 2 2 3
示例输出
2 1
提示
来源
示例程序
#include<iostream>
#include<algorithm>
#include<queue>
#include<cstdio>
#include<cstring>
using namespace std;
int main()
{
int T,n,m,x,y;
cin>>T;
int str[100010];
while(T--)
{
memset(str,0,sizeof(str));
priority_queue<int,vector<int>,greater<int > >q;//建立队列
cin>>n>>m;
while(m--)
{
cin>>x>>y;
str[x]++;//代表点周围的边数
str[y]++;//上同
}
sort(str+1,str+n+1);//从小到大排序
for(int i=1; i<=n; i++)
{
if(str[i]<2)
q.push(str[i]);//说明需要加边,所以进入队列
else
break;//若有一个大于等于2,后面的肯定大于等于2,因为已经排序了
}
int a,b;
int c=0;//要加的边数
while(q.size()>=2)//只有队列中有两个以上元素才可以进行加边
{
a=q.top();//拿出第一个节点
q.pop();//删除第一个节点
b=q.top();//拿出第一个节点
q.pop();//删除第一个节点
a++;
b++;
c++;//连起a和b
if(a<2)
q.push(a);
if(b<2)
q.push(b);
}
if(q.empty()!=true)//若只有一个元素,首先肯定不是单一存在的,所以边数加1
{
c++;
}
printf("%d\n",c);
}
return 0;
}