1272. Non-Yekaterinburg Subway

Time limit: 1.0 second
Memory limit: 64 MB
A little town started to construct a subway. The peculiarity of the town is that it is located on small islands, some of them are connected with tunnels or bridges. The mayor is sure that the subway is to be under the ground, that’s why the project must use the less bridges the better. The only request for the subway is that the townsmen could get by metro (may be with changes) from every island to every island. Fortunately, we know that there is enough tunnels and bridges for it. It was decided to construct as less passages from island to island as possible to save money.
Your task given a town plan to determine the minimal possible number of bridges that is necessary to use in the subway construction.

Input

The first line contains three integers separated with a space: N (the number of islands,1 ≤ N ≤ 10000), K (the number of tunnels, 0 ≤ K ≤ 12000) and M (the number of bridges,0 ≤ M ≤ 12000). Then there are K lines; each line consists of two integers — the numbers of islands, connected with the corresponding tunnel. The last M lines define bridges in the same format.

Output

the minimal number of bridges necessary for the subway construction.

Sample

input output
6 3 4
1 2
2 3
4 5
1 3
3 4
4 6
5 6
2
Problem Author: Magaz Asanov (prepared Igor Goldberg)
Problem Source: Ural State University championship, October 25, 2003
Difficulty: 174
题意:给出一个图,问使它们相通要加多少条边。
分析:并查集裸题
 /**
Create By yzx - stupidboy
*/
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <ctime>
#include <iomanip>
using namespace std;
typedef long long LL;
typedef double DB;
#define For(i, s, t) for(int i = (s); i <= (t); i++)
#define Ford(i, s, t) for(int i = (s); i >= (t); i--)
#define Rep(i, t) for(int i = (0); i < (t); i++)
#define Repn(i, t) for(int i = ((t)-1); i >= (0); i--)
#define rep(i, x, t) for(int i = (x); i < (t); i++)
#define MIT (2147483647)
#define INF (1000000001)
#define MLL (1000000000000000001LL)
#define sz(x) ((int) (x).size())
#define clr(x, y) memset(x, y, sizeof(x))
#define puf push_front
#define pub push_back
#define pof pop_front
#define pob pop_back
#define ft first
#define sd second
#define mk make_pair
inline void SetIO(string Name)
{
string Input = Name+".in",
Output = Name+".out";
freopen(Input.c_str(), "r", stdin),
freopen(Output.c_str(), "w", stdout);
} inline int Getint()
{
int Ret = ;
char Ch = ' ';
bool Flag = ;
while(!(Ch >= '' && Ch <= ''))
{
if(Ch == '-') Flag ^= ;
Ch = getchar();
}
while(Ch >= '' && Ch <= '')
{
Ret = Ret * + Ch - '';
Ch = getchar();
}
return Flag ? -Ret : Ret;
} const int N = , M = ;
int n, m, k;
int Fa[N];
inline void Input()
{
scanf("%d%d%d", &n, &m, &k);
} inline int Find(int x)
{
static int Arr[N], Length;
Length = ;
while(x != Fa[x])
{
Arr[++Length] = x;
x = Fa[x];
}
For(i, , Length) Fa[Arr[i]] = x;
return x;
} inline void Solve()
{
For(i, , n) Fa[i] = i;
For(i, , m)
{
int u, v;
scanf("%d%d", &u, &v);
u = Find(u), v = Find(v);
if(u != v) Fa[u] = v;
} int Ans = ;
For(i, , n)
if(Find(i) == i) Ans++;
printf("%d\n", Ans - );
} int main()
{
#ifndef ONLINE_JUDGE
SetIO("H");
#endif
Input();
Solve();
return ;
}

ural 1272. Non-Yekaterinburg Subway的更多相关文章

  1. URAL(timus) 1272 Non-Yekaterinburg Subway(最小生成树)

    Non-Yekaterinburg Subway Time limit: 1.0 secondMemory limit: 64 MB A little town started to construc ...

  2. ural 1273. Tie

    1273. Tie Time limit: 1.0 secondMemory limit: 64 MB The subway constructors are not angels. The work ...

  3. ural 1217. Unlucky Tickets

    1217. Unlucky Tickets Time limit: 1.0 secondMemory limit: 64 MB Strange people live in Moscow! Each ...

  4. Ural 1086 - Cryptography

    While preparing this problem set the jury has run into the following problem: it was necessary to se ...

  5. Ural 1319 - Hotel

       You programmers are lucky! You don't have to deal with these terrible people – designers… This st ...

  6. Ural 1313 - Some Words about Sport

    Ural doctors worry about the health of their youth very much. Special investigations showed that a l ...

  7. Ural State University Internal Contest October'2000 Junior Session

    POJ 上的一套水题,哈哈~~~,最后一题很恶心,不想写了~~~ Rope Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7 ...

  8. URAL 2092 Bolero 贪心

    C - Bolero Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit S ...

  9. 第一次作业——subway

    作业源程序代码:https://github.com/R-81/subway 作业程序使用说明:通过输入命令参数求解路线(仅支持-b,-c),根据参数得出路线后,程序不会结束,此时可输入地铁路线名(例 ...

随机推荐

  1. js获取中国日期-农历

    /* var bsYear; var bsDate; var bsWeek; var arrLen=8; //数组长度 var sValue=0; //当年的秒数 var dayiy=0; //当年第 ...

  2. [Effective JavaScript 笔记]第49条:数组迭代要优先使用for循环而不是for...in循环

    示例 下面代码中mean的输出值是多少? var scores=[98,74,85,77,93,100,89]; var total=0; for(var score in scores){ tota ...

  3. unity StreamingAssets路径

    原地址:http://blog.csdn.net/nateyang/article/details/8493791 我们在读写例如XML和TXT文件的时候,在电脑上和手机上路径不一致,造成了很多麻烦, ...

  4. KMP算法精髓

    这个算法的做法就是在部分匹配的时候,常规想法是向后移动一位,但是KMP想法是向后移动n位(n=m-L). 注释:这里m表示已经匹配了的字符的个数,L表示已经匹配了的那些字符组成的这个字符串的前缀和后缀 ...

  5. sqlmap参数大全

    -u #注入点-f #指纹判别数据库类型-b #获取数据库版本信息-p #指定可测试的参数(?page=1&id=2 -p “page,id”)-D “”#指定数据库名-T “”#指定表名-C ...

  6. django-cms 代码研究(六)plugin的深入分析

    示例代码: https://github.com/divio/djangocms-picture 以上一个图片的插件,安装后可在页面中添加图片,效果如下图: 以此为切入点,分析plugin的逻辑: 分 ...

  7. luarocks install with lua5.1 and luajit to install lapis

    # in luarocks source directory...git clone https://github.com/archoncap/luarockscd luarocks ./config ...

  8. Calico在Kubernetes中的搭建

    一,需求 Kubernetes官方推荐的是Flannel,但是Flannel是一个overlay的网络,对性能会有一定的影响.Calico恰好能解决一下overlay网络的不足. Calico在Kub ...

  9. 【Python】python代码如何调试?

    Python 程序如何高效地调试? 现在我在debug python程序就只是简单在有可能错误的地方print出来看一下,不知道python有没像c++的一些IDE一样有单步调试这类的工具?或者说各位 ...

  10. Android Studio项目整合PullToRefresh的问题记录

    PullToRefresh下拉刷新在App中应用非常频繁,然而PullToRefresh是在ADT下开发完成的.如果要将其整合到Android Studio目录下的话颇费周折.前面的文章“Androi ...