D. Cow and Snacks 并查集

 

D. Cow and Snacks

题意:有n种小吃,m个人,每个人有两种喜欢的小吃,当一个人遇到两种自己都喜欢的小吃,可以都吃掉,问在最优的吃小吃顺序下,不能吃到自己喜欢的小吃的人数最少是多少?

 

题解:把n种小吃当作n个点,m个人当作m条边,每个连通图里面第一个吃的人,一定是可以吃两种自己喜欢的小吃。每次判断这条边是否在已有的联通图里面,对已经在连通图里面的边,是一定不能吃到小吃,若不在连通图里面,则一定可以吃到小吃,用cnt统计可以吃到小吃的人数,最后m-cnt就是答案

 

#include<iostream>
#include<string.h>
#include<string>
#include<algorithm>
using namespace std;
int p[1000005], r[1000005];
int n,t=0;
void init()//初始化集合,每个元素的老板都是自己
{
    for (int i = 1; i <= n; i++)
    {
        p[i] = i;
    }
}

int find(int x)//查找元素x的老板是谁
{
    if (x == p[x])
        return x;
    else
        return p[x] = find(p[x]);
}

void join(int x, int y)//合并两个集合
{
    int xRoot = find(x);
    int yRoot = find(y);

    if (xRoot == yRoot) //老板相同,不合并
        return;
    //cnt=cnt-1;
    if (r[xRoot] < r[yRoot]) //r[i]是元素i所在树的高度,矮树的根节点认高树的根节点做老板
        p[xRoot] = yRoot;
    else if (r[xRoot] > r[yRoot])
        p[yRoot] = xRoot;
    else
    {
        p[yRoot] = xRoot;//树高相同,做老板的树高度要加一
        r[xRoot]++;
    }
}

bool sameRoot(int x, int y)//查询两个元素的老板是否相同
{
    return find(x) == find(y);
}
//这里也可以用cnt求不同子集个数,初始化cnt=n,每加入一条边,cnt=cnt-1;
int main()
{
    ios::sync_with_stdio(false);
    int m,cnt=0;
    cin>>n>>m;
    init();
    for(int i=0;i<m;i++)
    {
        int x,y;
        cin>>x>>y;
        if(!sameRoot(x,y))
        {
            join(x,y);
            cnt++;
        }
    }
    cout<<m-cnt<<endl;
    return 0;
}

 

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.

 

上一篇:一起学习android使用一个回调函数onCreateDialog实现负载对话(23)


下一篇:D. Cow and Snacks codeforces # 585 div2 小思维题