• Cow and Snacks


    Cow and Snacks

    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <string>
    #include <set>
    #include <queue>
    #include <map>
    #include <sstream>
    #include <cstdio>
    #include <cstring>
    #include <numeric>
    #include <cmath>
    #include <iomanip>
    #include <deque>
    #include <bitset>
    #include <cassert>
    //#include <unordered_set>
    #include <unordered_map>
    #define ll              long long
    #define pii             pair<int, int>
    #define rep(i,a,b)      for(int  i=a;i<=b;i++)
    #define dec(i,a,b)      for(int  i=a;i>=b;i--)
    #define forn(i, n)      for(int i = 0; i < int(n); i++)
    using namespace std;
    int dir[4][2] = { { 1,0 },{ 0,1 } ,{ 0,-1 },{ -1,0 } };
    const long long INF = 0x7f7f7f7f7f7f7f7f;
    const int inf = 0x3f3f3f3f;
    const double pi = acos(-1.0);
    const double eps = 1e-6;
    const int mod = 998244353;
    
    inline int read()
    {
        int x = 0; bool f = true; char c = getchar();
        while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
        while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
        return f ? x : -x;
    }
    inline ll gcd(ll m, ll n)
    {
        return n == 0 ? m : gcd(n, m % n);
    }
    void exgcd(ll A, ll B, ll& x, ll& y)
    {
        if (B) exgcd(B, A % B, y, x), y -= A / B * x; else x = 1, y = 0;
    }
    inline ll qpow(ll x, ll n) {
        int r = 1;
        while (n > 0) {
            if (n & 1) r = 1ll * r * x %mod;
            n >>= 1; x = 1ll * x * x %mod;
        }
        return r;
    }
    inline int inv(int x) {
        return qpow(x, mod - 2);
    }
    ll lcm(ll a, ll b)
    {
        return a * b / gcd(a, b);
    }
    /**********************************************************/
    const int N = 2e5 + 5;
    
    int n, k;
    bool vis[N];
    
    int f[200010];
    int find(int x) {
        return f[x] == x ? x : f[x] = find(f[x]);
    }
    bool add(int x, int y) {
        int fx = find(x), fy = find(y);
        if (fx != fy) {
            f[fx] = fy;
            return true;
        }
        return false;
    }
    
    int main()
    {
        cin >> n >> k;
        vector<vector<int>> g(n);
        rep(i, 1, n)
            f[i] = i;
        rep(i, 1, k)
        {
            int u, v;
            cin >> u >> v;
            add(u, v);
        }
        unordered_map<int,int> mp;
        rep(i, 1, n)
        {
            mp[find(i)]++;
        }
        int res = 0;
        for (auto x : mp)
        {
            res += x.second - 1;
        }
        cout << k - res << endl;
        return 0;
    }
     
  • 相关阅读:
    【解读】Https协议
    【解读】Http协议
    tomcat中AJP协议和HTTP协议的区别
    TOMCAT原理详解及请求过程
    Redis持久性——RDB和AOF
    redis配置文件解读
    HttpClient优化
    crontab与系统时间不一致
    天兔(Lepus)监控操作系统(OS)安装配置
    MySQL 优化之 index_merge (索引合并)
  • 原文地址:https://www.cnblogs.com/dealer/p/13578856.html
Copyright © 2020-2023  润新知