D. Cow and Snacks

代码:

// Created by CAD on 2019/9/18.
#include <bits/stdc++.h>
using namespace std;
const int maxn=1e5+5;
int fa[maxn];
inline int find(int x)
{
    return x==fa[x]?fa[x]:fa[x]=find(fa[x]);
}
void merge(int x,int y)
{
    int fx=find(x),fy=find(y);
    if(fx!=fy) fa[fx]=fy;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n,m;    cin>>n>>m;
    for(int i=1;i<=n;++i) fa[i]=i;
    int u,v,ans=0;
    for(int i=1;i<=m;++i)
    {
        cin>>u>>v;
        if(find(u)==find(v)) ans++;
        merge(u,v);
    }
    cout<<ans<<endl;
    return 0;
}
01-25 20:37