图论dfs(vector储存)

D. Cow and Snacks

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 nn snacks flavors, numbered with integers 1,2,…,n1,2,…,n. Bessie has nn 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 nn and kk (2≤n≤1052≤n≤105, 1≤k≤1051≤k≤105), the number of snacks and the number of guests.

The ii-th of the following kk lines contains two integers xixi and yiyi (1≤xi,yi≤n1≤xi,yi≤n, xi≠yixi≠yi), favorite snack flavors of the ii-th guest.

Output

Output one integer, the smallest possible number of sad guests.

Examples

input

Copy

5 4
1 2
4 3
1 4
3 4

output

Copy

1

input

Copy

6 5
2 3
2 1
3 4
6 5
4 5

output

Copy

0

Note

In the first example, Bessie can order the guests like this: 3,1,2,43,1,2,4. Guest 33 goes first and eats snacks 11 and 44. Then the guest 11 goes and eats the snack 22 only, because the snack 11 has already been eaten. Similarly, the guest 22 goes up and eats the snack 33 only. All the snacks are gone, so the guest 44 will be sad.

In the second example, one optimal ordering is 2,1,3,5,42,1,3,5,4. All the guests will be satisfied.

无脑dfs

#include<bits/stdc++.h> 
using namespace std;
int n,k;
int ans=0;
int vis[100005],l[100005],r[100005];
vector<int > vet[100005];//用vector储存图
void dfs(int v)
{
	vis[v]=1;
	for(int i=0;i<vet[v].size();i++)
	{
		if(!vis[vet[v][i]])
		{
			ans++;
			dfs(vet[v][i]);
		}
	}
	return ;
}
int main()
{
	scanf("%d%d",&n,&k);
	for(int i=0;i<k;i++)
	{
		int a,b;
		scanf("%d%d",&a,&b);
		l[i]=a;
		r[i]=b;
		
		vet[a].push_back(b);
		vet[b].push_back(a);
	}
	for(int i=0;i<k;i++)
	{
		
		if(!vis[l[i]])
		{
			dfs(l[i]);
		}
		if(!vis[r[i]])
		{
			dfs(r[i]);
		}
	}
	cout<<k-ans<<endl;
}
发布了44 篇原创文章 · 获赞 6 · 访问量 1195

猜你喜欢

转载自blog.csdn.net/qq_43868883/article/details/100852774