ural 1272. Non-Yekaterinburg Subway

时间:2022-02-05 22:01:26

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 ;
}