畅通工程

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 38471    Accepted Submission(s): 20404

Problem Description
某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇。省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只要互相间接通过道路可达即可)。问最少还需要建设多少条道路?
 
Input
测试输入包含若干测试用例。每个测试用例的第1行给出两个正整数,分别是城镇数目N ( < 1000 )和道路数目M;随后的M行对应M条道路,每行给出一对正整数,分别是该条道路直接连通的两个城镇的编号。为简单起见,城镇从1到N编号。
注意:两个城市之间可以有多条道路相通,也就是说
3 3
1 2
1 2
2 1
这种输入也是合法的
当N为0时,输入结束,该用例不被处理。
 
Output
对每个测试用例,在1行里输出最少还需要建设的道路数目。
 
Sample Input
4 2
1 3
4 3
3 3
1 2
1 3
2 3
5 2
1 2
3 5
999 0
0
 
Sample Output
1
0
2
998

Hint

Hint

Huge input, scanf is recommended.

感概:现在看以前的代码我都不忍直视了,太烂了。
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>
using namespace std; const int INF=0x3f3f3f3f;
const double eps=1e-;
const double PI=acos(-1.0);
#define maxn 2000
int n, m, cnt;
int root[maxn];
void init_root()
{
for(int i = ; i <= n; i++)
root[i] = i;
}
int find_root(int x)
{
if(x == root[x])
return x;
else
return root[x] = find_root(root[x]);
}
void uni(int a, int b)
{
int x = find_root(a);
int y = find_root(b);
if(x != y)
{
root[y] = x;
cnt++;
}
}
int main()
{
while(~scanf("%d", &n))
{
if(n == )
break;
init_root();
scanf("%d", &m);
int a, b;
cnt = ;
for(int i = ; i < m; i++)
{
scanf("%d%d", &a, &b);
if(cnt == n-)
continue;
uni(a, b);
}
int ans = ;
for(int i = ; i <= n; i++)
if(root[i] == i)
ans++;
printf("%d\n", ans-);
}
return ;
}

HDU1232 畅通工程 (并查集模板题)的更多相关文章

  1. HDU-1232/NYOJ-608畅通工程,并查集模板题,,水过~~~

    畅通工程 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) http://acm. ...

  2. HDU - 1232 畅通工程-并查集模板

    某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只要互相间接通过道路可达即可). ...

  3. hdu1232畅通工程(并查集,简单题)

    传送门 最少好要修多少条路太能使全部城镇连通.只要用并查集算可以连通的城市的组数,修的路就是组数减1 #include<bits/stdc++.h> using namespace std ...

  4. hdu1232 畅通工程 并查集的 应用

    畅通工程 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  5. HDU1232 畅通工程 并查集

    畅通工程 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  6. PAT题解-1118. Birds in Forest (25)-(并查集模板题)

    如题... #include <iostream> #include <cstdio> #include <algorithm> #include <stri ...

  7. HDU1232 畅通project 并查集

    这道题跟HDU 1213 How Many Tables 并查集很接近,都是赤裸裸的并查集的题. 思路:如果还须要建n-1条路.每并一次就自减1. 參考代码: #include<stdio.h& ...

  8. HDU 1232 (畅通工程) 并查集经典模板题

    Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府"畅通工程"的目标是使全省任何两个城镇间都可以实现交通 ...

  9. ACM: 畅通工程-并查集-解题报告

    畅通工程 Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description 某省调查城镇交通状况 ...

随机推荐

  1. Hdu1401-Solitaire(双向bfs)

    Solitaire is a game played on a chessboard 8x8. The rows and columns of the chessboard are numbered ...

  2. 腾讯课堂-草图大师 Sketchup 初级到精通视频讲座

    腾讯课堂-草图大师 Sketchup 初级到精通视频讲座 草图大师 Sketchup 初级到精通视频讲座

  3. android 缓存Bitmap - 开发文档翻译

    由于本人英文能力实在有限,不足之初敬请谅解 本博客只要没有注明“转”,那么均为原创,转贴请注明本博客链接链接 Loading a single bitmap into your user interf ...

  4. 苹果拒绝App内部使用版本检测功能

    10.6 - Apple and our customers place a high value on simple, refined, creative, well thought through ...

  5. BOOST中如何实现线程安全代码

    1 需要include <boost/thread.hpp> 2 背景知识请参考<boost程序库完全开发指南>第12章 3 编绎:g++ -o mthread mthread ...

  6. document.write 存在几个问题?应该注意

    document.write (henceforth DW) does not work in XHTML XHTML 不支持 DW executed after the page has finis ...

  7. 未能加载文件或程序集“System.Web.Helpers, Version=2.0.0.0(转)

    在本地终于用上了ASP.NET MVC4自带的认证功能,但放到生产服务器上就出问题了:打开注册页面没问题,但一点下注册按钮就报错了: 未能加载文件或程序集“System.Web.Helpers, Ve ...

  8. WCF入门教程系列三

    一.WCF服务应用程序与WCF服务库 我们在平时开发的过程中常用的项目类型有“WCF 服务应用程序”和“WCF服务库”. WCF服务应用程序,是一个可以执行的程序,它有独立的进程,WCF服务类契约的定 ...

  9. poj 3959 Alignment of Code <vector>“字符串”

    Description You are working in a team that writes Incredibly Customizable Programming Codewriter (IC ...

  10. jQuery模糊选择

    属性字头选择器(Attribute Contains Prefix Selector) jQuery 属性字头选择器的使用格式是 jQuery(‘[attribute|=value]‘) ,例如 jQ ...