当前位置:网站首页>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 pondsInput
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();
}边栏推荐
- Ecplise cannot connect to SQL Server
- Explanation of waitgroup usage in go language learning
- create_ Error in engine MySQL connector encryption method
- Is byte really the end of the universe?
- Terraformer importing cloud resources
- Collapse expression
- Nomad application layout scheme 07 of hashicopy (submit job)
- Variable parameter expression
- Introduction to long connection
- The application of machine learning in database cardinality estimation
猜你喜欢

Unsealing easy QF PDA helps enterprises improve ERP management

The tree (AVL, 2-3-, red black, Huffman)

高比例风电电力系统储能运行及配置分析(Matlab实现)

Teatalk · online speech record | complete! It is important to select the right data migration strategy for safe cloud deployment

为什么每运行一部都显示一次数据库已存在,都要删除数据库,然后才能成功,每运行一部都要删除一次数据库,重新运行整体才成功.

On the life extension of distributed locks -- redis based distributed locks

Bs-xx-007 registered residence management system based on JSP

cadence SPB17.4 - allegro - allegro_free_viewer

Three level classification display

Just after the college entrance examination, I was confused and didn't know what to do? Tell me what I think
随机推荐
关于#php#的问题:php写的原来的部署环境是在phpstudy里面进行部署的,php+MySQL+Apache但是由于每次都得保证电脑开着
[pyhton crawler] regular expression
[issue 268] accidentally submit the test code to the production environment. I will teach you six ways to solve it in seconds!
C language learning record 6
代码对比工具,我就用这6个
Two small things, feel the gap with the great God
How about NFT market? Why is NFT so popular? How to build NFT platform
[201] PHP exception handling - try catch finally exception handling in PHP
Question bank and answers for 2022 tool fitter (intermediate) operation certificate examination
d的each与map不一致
Kubernetes binary installation (v1.20.15) (VII) plug a work node
Hashicopy之nomad应用编排方案07(提交Job)
全球手机市场衰退,连苹果也对iPhone14不抱过高期待
RS485(Modbus RTU)工业RFID读写器CK-FR03-A01与PLC三菱FX5U的通讯操作说明
Is byte really the end of the universe?
Easyexcel configuration and Application
cadence SPB17.4 - group operation(add to group, view group list, delete group)
The global mobile phone market is declining, and even apple does not expect too much of the iphone14
Debian下设定 TCP/IP 网络
Stochastic dynamic economic dispatching model of wind power (realized by matlab code)