zoukankan      html  css  js  c++  java
  • [POI2012] TOU-Tour de Byteotia

    [POI2012] TOU-Tour de Byteotia - 并查集

    Description

    给定一个n个点m条边的无向图,问最少删掉多少条边能使得编号小于等于k的点都不在环上

    Solution

    删除一条边,只能破坏一个环

    所以对于一个环来说,删除它的哪一条边,是没有区别的

    因此,对于那些连接两个 >k 的点的边,我们可以把他们先连上

    然后去处理那些剩下的边,如果加上去不会成环,就加上去

    并查集维护即可

    #include <bits/stdc++.h>
    using namespace std;
    
    #define int long long
    const int N = 1e6 + 5;
    
    int fa[N];
    
    int find(int p)
    {
        return p == fa[p] ? p : fa[p] = find(fa[p]);
    }
    
    void merge(int p, int q)
    {
        p = find(p);
        q = find(q);
        if (p - q)
            fa[p] = q;
    }
    
    signed main()
    {
        ios::sync_with_stdio(false);
    
        int n, m, k;
        cin >> n >> m >> k;
    
        for (int i = 1; i <= n; i++)
            fa[i] = i;
    
        vector<pair<int, int>> edge;
        for (int i = 1; i <= m; i++)
        {
            int p, q;
            cin >> p >> q;
            if (p > k && q > k)
                merge(p, q);
            else
                edge.push_back({p, q});
        }
    
        int ans = 0;
    
        for (auto [p, q] : edge)
        {
            if (find(p) == find(q))
                ans++;
            else
                merge(p, q);
        }
    
        cout << ans << endl;
    }
    
  • 相关阅读:
    zookeeper部署
    Hadoop集群的构建和安装
    numpy ndarray求其最值的索引
    两数之和
    盛最多水的容器
    mysql 单表卡死
    Leetcode 258. Add Digits
    Matlab中添加路径与去除路径
    Leetcode 136 Single Number
    Leetcode 485. Max Consecutive Ones
  • 原文地址:https://www.cnblogs.com/mollnn/p/14445417.html
Copyright © 2011-2022 走看看