当前位置:网站首页>Pond (topology + priority queue)

Pond (topology + priority queue)

2022-06-11 13:51:00 Joanh_ Lan

The title is as follows :

Betty owns a lot of ponds, some of them are connected with other ponds by pipes, and there will not be more than one pipe between two ponds. Each pond has a value vv.

Now Betty wants to remove some ponds because she does not have enough money. But each time when she removes a pond, she can only remove the ponds which are connected with less than two ponds, or the pond will explode.

Note that Betty should keep removing ponds until no more ponds can be removed. After that, please help her calculate the sum of the value for each connected component consisting of a odd number of ponds

Input

The first line of input will contain a number T(1≤T≤30) which is the number of test cases.

For each test case, the first line contains two number separated by a blank. One is the number p(1≤p≤1e4) which represents the number of ponds she owns, and the other is the number m(1≤m≤105) which represents the number of pipes.

The next line contains p numbers v1​,...,vp​, where vi​(1≤vi​≤1e8) indicating the value of pond ii.

Each of the last mm lines contain two numbers aa and bb, which indicates that pond aa and pond bb are connected by a pipe.

Ideas :

Please delete the points that meet the requirements , Calculate each Odd point connected block The sum of the weights of

You can first calculate the degree of each point through topology meanwhile Drawing + Union checking set ( Connected block )

Use the priority queue to go through bfs

Priority queue ( Heap ) use pair encapsulation { The degree of this point , The label of the point }

Find the matching point and delete it ( notes : It is easy to delete here , As a result, this point will be calculated later ), Traverse the points that connect the points , Connection point Degree minus one

 ... ...

AC The code is as follows :

#include <bits/stdc++.h>
#define buff                     \
    ios::sync_with_stdio(false); \
    cin.tie(0);                  \
    cout.tie(0);
#define int long long
using namespace std;
const int N = 1e4 + 9;
int n, m, k;
int val[N];
int fa[N];
int d[N];
map<int, int> mp;
vector<pair<int, int>> s[N];
vector<int> g[N];
void init()
{
    k = 0;
    mp.clear();

    for (int i = 1; i <= n; i++)
    {
        d[i] = 0;
        fa[i] = i;
        s[i].clear();
        g[i].clear();
    }
}
int find(int x)
{
    return x == fa[x] ? fa[x] : fa[x] = find(fa[x]);
}
void solve()
{
    cin >> n >> m;
    init();
    for (int i = 1; i <= n; i++)
    {
        cin >> val[i];
    }
    int u, v;
    for (int i = 1; i <= m; i++)
    {
        cin >> u >> v;
        d[u]++, d[v]++;
        g[u].push_back(v), g[v].push_back(u);
        u = find(u), v = find(v);
        fa[v] = u;
    }
    for (int i = 1; i <= n; i++)
    {
        if (i == find(i))
        {
            mp[i] = ++k;
        }
    }
    for (int i = 1; i <= n; i++)
    {
        s[mp[find(i)]].push_back({d[i], i});
    }
    int ans = 0;
    // cout << k << " ***" << endl;
    // for (int i = 1; i <= k; i++)
    // {
    //     sort(s[i].begin(), s[i].end());
    // }

    for (int i = 1; i <= k; i++)
    {
        int sum = 0;
        int cnt = s[i].size();
        priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;
        for (auto it : s[i])
        {
            q.push(it);
        }
        while (!q.empty())
        {
            auto it = q.top();
            q.pop();
            if (d[it.second] <= 1)
            {
                if (d[it.second] < 0)
                    continue;
                cnt--;
                d[it.second] = -1;
                for (auto qq : g[it.second])
                {
                    d[qq]--;
                    if (d[qq] <= 1)
                        q.push({d[qq], qq});
                }
            }
            else
                sum += val[it.second];
        }
        if (cnt & 1)
            ans += sum;
    }
    cout << ans << '\n';
}
signed main()
{
    int T;
    cin >> T;
    while (T--)
        solve();
}

原网站

版权声明
本文为[Joanh_ Lan]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206111350104238.html