D. Cow and Snacks-(并查集dsu)

总结

分堆问题
我们把有口味有关联的客人放在一堆,然后每一堆考虑最大满足数
特别的是,每个客人喜欢两种不同的口味,要保证最大满足数,你会发现,一堆口味中,第一个吃两种口味,后面的人都只吃一种口味了,达到最优。

每一堆有关系口味,其实就是构成多个连通分量。每个连通分量,第一个人必须吃一条边,也就是两个结点(两种口味),剩下的人每次吃一种口味。

/*
                ____________    ______________       __
               / _________  /\ /_____   _____/\     / /\
              / /\       / /  \\    /  /\    \ \   / /  \
             / /  \_____/ /   / \__/  /  \____\/  / /   /
            / /   /    / /   /    /  /   /       / /   /
           / /   /    / /   /    /  /   /       / /   /
          / /   /    / /   /    /  /   /       / /   /
         / /___/____/ /   /    /  /   /       / /___/________
        /____________/   /    /__/   /       /______________/\
        \            \  /     \  \  /        \              \ \
         \____________\/       \__\/          \______________\/
           ___       ___               ___    __________
          /  /\     /  /\             /  /\  /_______  /\
         /  /__\___/  /  \           /  /  \ \      /  /  \
        /____    ____/   /          /  /   /  \____/  /   /
        \   /   /\   \  /          /  /   /       /  /   /
         \_/   /  \___\/ ___      /  /   /       /  /   /
          /   /   /     /  /\    /  /   /       /  /   /
         /   /   /     /  /__\__/  /   /       /  /___/____
        /___/   /     /___________/   /       /___________/\
        \   \  /      \           \  /        \           \ \
         \___\/        \___________\/          \___________\/

          A CODE OF CBOY

*/
#include<bits/stdc++.h>
//typedef long long ll;
//#define ull       unsigned long long
//#define int       long long
#define F           first
#define S           second
#define endl        "\n"//<<flush
#define lowbit(x)   (x&(-x))
#define ferma(a,b)  pow(a,b-2)
#define pb          push_back
#define mp          make_pair
#define all(x)      x.begin(),x.end()
#define memset(a,b) memset(a,b,sizeof(a));
#define IOS         ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
const double PI=acos(-1.0);
const int inf=0x3f3f3f3f;
const int MAXN=0x7fffffff;
const long long INF = 0x3f3f3f3f3f3f3f3fLL;
void file()
{
#ifdef ONLINE_JUDGE
#else
    freopen("cin.txt","r",stdin);
    //  freopen("cout.txt","w",stdout);
#endif
}
const int N=1e6+5;
int fat[N],Size[N];
int father(int x)
{
    if(fat[x]==x)
        return x;
    fat[x]=father(fat[x]);
    return fat[x];
}
signed main()
{
    IOS;
    //file();
    int n,k;
    cin>>n>>k;
    for(int i=1;i<=n;i++)
        fat[i]=i,Size[i]=1;
    for(int i=0;i<k;i++)
    {
        int x,y;
        cin>>x>>y;
        x=father(x);
        y=father(y);
        if(x!=y)
        {
            fat[x]=y;
            Size[y]+=Size[x];
        }
    }
    int ans=0;
    set<int>se;
    for(int i=1;i<=n;i++)
        se.insert(father(i));
    for(auto it:se)
    {
        ans+=Size[father(it)]-1;
    }
    cout<<k-ans<<endl;
    return 0;
}

发布了130 篇原创文章 · 获赞 5 · 访问量 5005

猜你喜欢

转载自blog.csdn.net/weixin_44224825/article/details/104101505