SDUT-完美网络
来源:互联网 发布:http5网络协议 编辑:程序博客网 时间:2024/05/22 14:47
完美网络
Time Limit: 1000MS Memory Limit: 65536KB
Submit Statistic Discuss
Problem Description
完美网络是连通网络的基础上要求去掉网络上任意一条线路,网络仍然是连通网络。求一个连通网络要至少增加多少条边可以成为完美网络。
Input
第一行输入一个数T代表测试数据个数(T<=20)。每个测试数据第一行2个数n,m 分别代表网络基站数和基站间线路数。基站的序号为从1到n。接下来m行两个数代表x,y 代表基站x,y间有一条线路。
(0 < n < m < 10000)
Output
对于每个样例输出最少增加多少线路可以成为完美网络。每行输出一个结果。
Example Input
23 11 23 21 22 3
Example Output
21
Hint
Author
中国海洋大学第三届“朗讯杯”编程比赛高级组试题
#include <bits/stdc++.h>using namespace std;int f[10010];int main(){ int T,n,m,i,x,y,z; scanf("%d",&T); while(T--) { priority_queue<int,vector<int>,greater<int> >q; memset(f,0,sizeof(f)); z=0; scanf("%d%d",&n,&m); for(i=0; i<m; i++) { scanf("%d%d",&x,&y); f[x]++; f[y]++; } sort(f+1,f+n+1); for(i=1; i<=n; i++) { if(f[i]<2) { q.push(f[i]);//存入孤立的点 } else //后面的点的度数不可能大于等于2 break; } while(q.size()>=2)// { x=q.top(); q.pop(); y=q.top(); q.pop(); x++;//每次取两个有最小度数的点,这两点的度数加一可理解为将两个点连接起来 y++; z++;//增加一条边 if(x<2)//当度数大于等于2可认为已不是孤立点 { q.push(x); } if(y<2) { q.push(y); } } if(q.empty()==false)//解决两种情况:处理后最后队列中只剩余一个度数小于二的点而此时无法加边;原队列度数小于2的点只有一个时 { z++; } printf("%d\n",z); } return 0;}
参考:http://blog.csdn.net/change_ac/article/details/47657453
sum1将所有度数不为二的点连成环,但会使度数为一的点出现多余边,需减去sum2/2
#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>using namespace std;int degree[10010];int main(){ int t; int n,m; int i,x,y; scanf("%d",&t); while(t--) { memset(degree,0,sizeof(degree)); scanf("%d%d",&n,&m); for(i=0;i<m;i++) { scanf("%d%d",&x,&y); degree[x]++; degree[y]++; } int sum1=0,sum2=0; for(i=1;i<=n;i++) { if(degree[i]<=1) sum1++; if(degree[i]==1) sum2++; } sum=sum1-sum2/2; if (sum2==0) sum++; printf("%d\n",sum); } return 0;}
阅读全文
0 0
- 完美网络 SDUT 2506
- SDUT-2506 完美网络
- SDUT- 2506-完美网络
- sdut 2506 完美网络
- SDUT完美网络
- SDUT-完美网络
- SDUT 2056-完美网络
- SDUT 2560 完美网络(优先队列)
- sdut 2506 完美网络(优先队列)
- SDUT OJ 2506 数据结构实验——完美网络
- sdut 完美网络 无向图的连通分量
- 完美素数 SDUT 3274
- SDUT 3300 完美字符串
- 完美的素数 (sdut oj)
- SDUT-2553 完美的素数
- SDUT 2553 完美的素数
- 完美网络
- 完美网络
- PyQt5笔记
- HDU 6073 Matching In Multiplication
- 2017.8.1拼多多内推内推笔试题(1)—最大乘积的求取(分治求K大(小))
- response.encodeURL的用法 把 Session ID 添加到 URL 信息中
- redis performance
- SDUT-完美网络
- vue怎么放网页图标
- <fieldset> 标签
- 个人 常用 iscroll 配置
- HashMap和HashSet
- mybatis学习二动态SQL:
- JDBC learning notes
- HDU_6078 Wavel Sequence
- excel文件读取类