完美网络
来源:互联网 发布:apt yum rpm 编辑:程序博客网 时间:2024/04/29 16:15
Time Limit: 1000MS Memory limit: 65536K
题目描述
输入
输出
示例输入
23 11 23 21 22 3
示例输出
21
提示
来源
示例程序
#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;
}
- 完美网络
- 完美网络
- 完美网络
- 完美网络
- 完美网络
- 完美网络
- 完美网络
- 完美网络
- 完美网络
- 完美网络
- 完美网络
- 完美网络
- 完美网络
- 完美网络
- 完美网络
- 完美网络
- 完美网络
- 完美网络
- OC_通知
- UITableView性能优化
- Origin Determinant
- linux--进程(7)
- PrintWriter证明了txt和doc的差距,txt不能识别控制符write方法在txt文件中运用时结果不能识别\n
- 完美网络
- (三十三)、Map接口
- Theano Tutorial文档翻译(六) - 条件
- supervisor 监控redis & mongodb
- 机器学习中常见的损失函数
- build-tools\24.0.1\aidl.exe'' finished with non-zero exit value 1
- 权限修饰符的作用和区别
- Spring-IOC-学习笔记(2)
- Theano Tutorial文档翻译(七) - 循环