专题四1003
来源:互联网 发布:c语言在线编程网站 编辑:程序博客网 时间:2024/05/29 00:31
*题意:列出了每条道路直接连通的城镇。使全省任何两个城镇间都可达,问最少还需要建设多少条道路?
*解题思路:赤裸裸的并查集
*感想:课件上的原题
*AC源码
#include<iostream>
using namespace std;
int bin[1002];
int findx(int x)
{
int r=x;
while(bin[r] !=r)
r=bin[r];
return r;
}
void merge(int x,int y)
{
int fx,fy;
fx = findx(x);
fy = findx(y);
if(fx != fy)
bin[fx] = fy;
}
int main()
{
int n,m,i,x,y,count;
while(cin>>n&&n)
{
for(i=1;i<=n;i++)
bin[i] = i;
for(cin>>m;m>0;m--)
{
cin>>x>>y;
merge(x,y);
}
for(count=-1, i=1;i<=n;i++)
if(bin[i] == i)
count ++;
cout<<count<<endl;
}
}
Problem C
Time Limit : 4000/2000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 66 Accepted Submission(s) : 31
Problem Description
某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇。省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只要互相间接通过道路可达即可)。问最少还需要建设多少条道路? <br>
Input
测试输入包含若干测试用例。每个测试用例的第1行给出两个正整数,分别是城镇数目N ( < 1000 )和道路数目M;随后的M行对应M条道路,每行给出一对正整数,分别是该条道路直接连通的两个城镇的编号。为简单起见,城镇从1到N编号。 <br>注意:两个城市之间可以有多条道路相通,也就是说<br>3 3<br>1 2<br>1 2<br>2 1<br>这种输入也是合法的<br>当N为0时,输入结束,该用例不被处理。 <br>
Output
对每个测试用例,在1行里输出最少还需要建设的道路数目。 <br>
Sample Input
4 21 34 33 31 21 32 35 21 23 5999 00
Sample Output
102998<div style='font-family:Times New Roman;font-size:14px;background-color:F4FBFF;border:#B7CBFF 1px dashed;padding:6px'><div style='font-family:Arial;font-weight:bold;color:#7CA9ED;border-bottom:#B7CBFF 1px dashed'><i>Hint</i></div>Hint</div>Huge input, scanf is recommended.
Statistic |Submit | Back
0 0
- 专题四1003
- 专题四1003
- 1003-专题四
- 专题四-1003
- 专题四 · 1003
- 2016sdau课程练习专题四 1003
- 3G专题四
- LDAP专题(四)
- 专题四 · 1001
- 专题四 Problem A
- 专题四 Problem B
- 专题四 Problem C
- 专题四 Problem D
- 专题四 Problem E
- 专题四1001
- 专题四1005
- 专题四1004
- 专题四ACM1003
- sphinx检索mysql问题
- Android Studio的快捷键以及设置方式
- Eclipse中将一个android项目当做另外一个android项目的library
- [从头读历史] 第251节 图解易经(下)
- ZooKeeper完全分布式安装与配置
- 专题四1003
- 怎么用Beyond Compare精确查找文本内容
- 漂亮的日期选择对话框datepicker
- Latex排版全解
- ajax动态生成表格,增加时间格式化
- linux中断简介
- android 使用Scroller实现美团悬浮框,网易左右滑动菜单效果
- Horizon dashboard的基本组成模块
- 伪元素(1)