当前位置:网站首页>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();
}边栏推荐
- Shader shader
- 六.开发记录之实验室服务器LXD部署
- What do you need to do to "surpass" the general database in the time sequence scenario?
- 无延时/无延迟视频直播实例效果案例
- 2022 Gansu Province safety officer B certificate test question simulation test question bank and online simulation test
- SAP Spartacus 中的 checkout(结帐) 设计
- Work summary: it took a long time to write SQL because of Cartesian product problem (Cartesian product summary attached)
- 在启牛开的证券账户安全吗?如何申请低佣金的股票账户?
- Operating instructions for communication between RS485 (Modbus RTU) industrial RFID reader ck-fr03-a01 and PLC Mitsubishi fx5u
- Terraform + Ansible实现基础设施及配置管理
猜你喜欢

Today in history: Apple II comes out; Microsoft acquires gecad; The scientific and technological pioneer who invented the word "software engineering" was born

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

折叠表达式

可变参表达式

About uni app configuration, app does not display the top title bar setting

Tp6 whoops based exception takeover (beautiful interface)

Network information system emergency response

如何写出高性能代码(四)优化数据访问

Please, don't use enumeration types in external interfaces any more!

XXVIII - 3D point cloud real-time and offline generation of 2D grid and 3D grid map
随机推荐
cadence SPB17.4 - group operation(add to group, view group list, delete group)
What do you need to do to "surpass" the general database in the time sequence scenario?
2022.2.28 variable length sequence table
关于#php#的问题:php写的原来的部署环境是在phpstudy里面进行部署的,php+MySQL+Apache但是由于每次都得保证电脑开着
/usr/bin/gzip: 1: ELF : not found /usr/bin/gzip: 3: : not found /usr/bin/gzip: 4: Syntax erro
YOLOv3学习笔记:YOLOv3的模型结构
Can't understand kotlin source code? Starting with the contracts function~
Application choreography nomad vs. kubernetes
Terrain + ansible implements infrastructure and configuration management
[pyhton crawler] regular expression
Interview to build an aircraft carrier, screw on the job, pay
RS485(Modbus RTU)工业RFID读写器CK-FR03-A01与PLC三菱FX5U的通讯操作说明
利用 VSCode 的代码模板提高 MobX 的编码效率
Please, don't use enumeration types in external interfaces any more!
ecplise无法连接sql server
自定义Terraform-Providers(Terraform Plugin Framework)-04
No delay / no delay live video instance effect cases
Musk says he doesn't like being a CEO, but rather wants to do technology and design; Wu Enda's "machine learning" course is about to close its registration | geek headlines
看不懂Kotlin源码?从Contracts 函数说起~
Tp6 whoops based exception takeover (beautiful interface)