题意:n种小吃,每种一个。k个人,每人有2个喜欢的小吃。当轮到某个人时这个人会把他喜欢的2种小吃都吃掉,如果一种都没有吃到他就会沮丧。要求可能的最少的沮丧的人数。
一开始没想到这个题要怎么搞,现在看来还是挺简单的。只需要建一个图,对于每个人,将他喜欢的两种食物连起来。然后就只要用并查集维护一下就行了。
代码:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN=2e5+5;
int n,k,ans,f[MAXN],a,b;
int getf(int x){
if (f[x]==x) return x;
return f[x]=getf(f[x]);
}
int main(){
cin>>n>>k;ans=0;
for (int i=1;i<=n;i++) f[i]=i;
for (int i=1;i<=k;i++){
cin>>a>>b;
a=getf(a);
b=getf(b);
if (a==b) ans++;
f[a]=b;
}
cout<<ans<<endl;
return 0;
}