练习四 1003
来源:互联网 发布:孙俪的等花开淘宝 编辑:程序博客网 时间:2024/04/30 14:39
概述:某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇。省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只要互相间接通过道路可达即可)。问最少还需要建设多少条道路?
思路:并查集问题,只要找出根节点的数目即可,很简单。
感想:第一节课听老师讲了,第二节课立刻去A了,感觉比较简单。
#include <iostream>#include <fstream>#include <algorithm>using namespace std;int bian[10000];int findx(int x){ int ans=x; while(bian[ans]!=ans) ans=bian[ans]; return ans;}void mergex(int a,int b){ int fx=findx(a); int fy=findx(b); if(fx!=fy) bian[fx]=fy;}int main(){ //ifstream cin("aaa.txt"); int n,m,temp1,temp2; while(cin>>n&&n) { cin>>m; for(int i=1;i<=n;++i) bian[i]=i; for(int i=0;i<m;++i) { cin>>temp1>>temp2; mergex(temp1,temp2); //bian[max(temp1,temp2)]=bian[min(temp1,temp2)]; } int countn=-1; for(int i=1;i<=n;++i) if(bian[i]==i) ++countn; cout<<countn<<endl; } return 0;}
0 0
- 练习四 1003
- 练习四 1003
- SDAU练习四 1003
- SDAU练习四 1003
- 练习四
- 2016sdau课程练习专题四 1003
- 数据库练习四
- 查询练习(四)
- Scala练习(四)
- 听课练习(四)
- 笔试练习四
- 笔试练习四
- 练习四1001
- 练习四 1001
- 练习四 1004
- 练习四 1002
- 练习四 1005
- 练习四 1001
- 剑指offer——旋转数组的最小值
- strcspn
- MyEclipse的输入提示配置
- 手机开发实战193——手机软件测试3
- HDU 3473 划分树
- 练习四 1003
- 【数据解析】Android Json数据的解析
- 乐事在线笔试题
- Android中的Service
- Unity Shader Example 6 (区域变色)
- python2.7安装Matplotlib相关问题
- Cocos2d-x v3.0物理系统 利用PhysicsEditor创建多边形
- 如何设置MFC静态文本背景为透明色?
- fresco系列一 异步框架的设计与实现