zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    剑指offer-第二章排序之年龄排序
    剑指offer—第二章算法之快速排序
    java小程序(课堂作业02)
    关于java的一些小知识(课程作业01)
    Java从命令行接受多个数字并求和
    《大道至简》第一章读后感及伪代码
    大道至简读后感
    GCPC 2013_A Boggle DFS+字典树 CSU 1457
    Aizu 2155 Magic Slayer 背包DP
    UVALive 4255 Guess
  • 原文地址:https://www.cnblogs.com/-citywall123/p/11587394.html
Copyright © 2011-2022 走看看