HDU1232 畅通工程 (并查集模板题)
畅通工程
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 38471 Accepted Submission(s): 20404
注意:两个城市之间可以有多条道路相通,也就是说
3 3
1 2
1 2
2 1
这种输入也是合法的
当N为0时,输入结束,该用例不被处理。
1 3
4 3
3 3
1 2
1 3
2 3
5 2
1 2
3 5
999 0
0
0
2
998
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 畅通工程 (并查集模板题)的更多相关文章
- HDU-1232/NYOJ-608畅通工程,并查集模板题,,水过~~~
畅通工程 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) http://acm. ...
- HDU - 1232 畅通工程-并查集模板
某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只要互相间接通过道路可达即可). ...
- hdu1232畅通工程(并查集,简单题)
传送门 最少好要修多少条路太能使全部城镇连通.只要用并查集算可以连通的城市的组数,修的路就是组数减1 #include<bits/stdc++.h> using namespace std ...
- hdu1232 畅通工程 并查集的 应用
畅通工程 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- HDU1232 畅通工程 并查集
畅通工程 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- PAT题解-1118. Birds in Forest (25)-(并查集模板题)
如题... #include <iostream> #include <cstdio> #include <algorithm> #include <stri ...
- HDU1232 畅通project 并查集
这道题跟HDU 1213 How Many Tables 并查集很接近,都是赤裸裸的并查集的题. 思路:如果还须要建n-1条路.每并一次就自减1. 參考代码: #include<stdio.h& ...
- HDU 1232 (畅通工程) 并查集经典模板题
Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府"畅通工程"的目标是使全省任何两个城镇间都可以实现交通 ...
- ACM: 畅通工程-并查集-解题报告
畅通工程 Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description 某省调查城镇交通状况 ...
随机推荐
- 独立写作(A or B)
开头:On contemporary society(一般的背景)/ With the advent of the technologically advanced society (the info ...
- Message Authentication Code
- 最小费用最大流模板 poj 2159 模板水题
Going Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15944 Accepted: 8167 Descr ...
- if条件语句练习(相亲)
public class a { public static void main(String[] arg){ //F是房子,Q是钱,N是能力,等于0说明没有,等于1说明有: 有一项具备则可以嫁,都不 ...
- Linux网络编程--多播
一.多播介绍 什么是多播? 单播用于两个主机之间的端对端通信,广播用于一个主机对整个局域网上所有主机上的数据通信.单播和广播是两个极端,要么对一个主机进行通信,要么对整个局域网上的主机进行通信.实际情 ...
- js 写table 函数
//创建 table函数 function table(row,col,b,w) { document.write('<table border='+b+'>'); for(var i=0 ...
- Jquery_Ajax文件上传
如何实现jQuery的Ajax文件上传,PHP如实文件上传.AJAX上传文件,PHP上传文件. [PHP文件上传] 在开始之前,我觉得是有必要把通WEB上传文件的原理简单说一下的.实际上,在这里不管是 ...
- Block 代替for循环
NSDictionary *aDictionary = [[NSDictionary alloc]initWithObjectsAndKeys:", nil]; [aDictionary e ...
- C++入门学习——标准模板库之vector
vector(向量容器),是 C++ 中十分实用一个容器.vector 之所以被觉得是一个容器,是由于它可以像容器一样存放各种类型的对象,简单地说,vector 是一个可以存放随意类型(类型可以是in ...
- [core java学习笔记][第五章继承]
5.1 类.超类和子类 定义 class Manager extends Employee { 添加方法和域 } 权限 派生类不能使用基类的私有变量 派生类调用基类的方法 利用super这个关键词 s ...