#题目:
The legendary Farmer John is throwing a huge party, and animals from all over the world are hanging out at his house. His guests are hungry, so he instructs his cow Bessie to bring out the snacks! Moo!
There are nsnacks flavors, numbered with integers 1,2,…,n. Bessie has n snacks, one snack of each flavor. Every guest has exactly two favorite flavors. The procedure for eating snacks will go as follows:
First, Bessie will line up the guests in some way.
Then in this order, guests will approach the snacks one by one.
Each guest in their turn will eat all remaining snacks of their favorite flavor. In case no favorite flavors are present when a guest goes up, they become very sad.
Help Bessie to minimize the number of sad guests by lining the guests in an optimal way.
Input
The first line contains integers n and k (2≤n≤105, 1≤k≤105 ), the number of snacks and the number of guests.
The i -th of the following k lines contains two integers xi and yi (1≤xi,yi≤n, xi≠yi), favorite snack flavors of the i -th guest.
Output
Output one integer, the smallest possible number of sad guests.
Examples
Input
5 4
1 2
4 3
1 4
3 4
Output
1
Input
6 5
2 3
2 1
3 4
6 5
4 5
Output
0
Note
In the first example, Bessie can order the guests like this: 3,1,2,4. Guest 3 goes first and eats snacks 1 and 4. Then the guest 1 goes and eats the snack 2 only, because the snack 1 has already been eaten. Similarly, the guest 2 goes up and eats the snack 3 only. All the snacks are gone, so the guest 4 will be sad.
In the second example, one optimal ordering is 2,1,3,5,4. All the guests will be satisfied.
#题意:
有n种零食和k个客人,每一个客人都有两种喜爱的零食。如果两种喜爱零食都吃不到那这位客人就会伤心了。要求你给客人排个吃零食的顺序,求出最少的伤心的客人数量。
#解题思路:
用到并查集,每读入一个客人的两种喜爱零食,就把这两点加到一个集合,如果说两点不在一个集合那就还有零食吃,若两点已经在同一个集合中了,那这位客人就要伤心了,于是ans++。
#代码:
#include<iostream>
#include<cstdlib>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<cstring>
using namespace std;
const int maxm = 1e5 + 5;
int fa[maxm];
void init()
{
for (int i = 0;i < maxm;i++)
fa[i] = i;
}
int fi(int x)
{
return fa[x] == x ? x : fa[x] = fi(fa[x]);
}
int main()
{
init();
int n, k;
cin >> n >> k;
int ans = 0;
while (k--)
{
int a, b;
cin >> a >> b;
a = fi(a);
b = fi(b);
if (a == b)ans++;
fa[a] = b;
}
cout << ans << endl;
return 0;
}