题目如下:

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.

思路:

题目要求将符合要求的点删除,计算每个 奇数点连通块 的权值之和

可以先通过拓扑算出每个点的度 同时 建图 + 并查集(连通块)

用优先队列经行bfs

优先队列(小根堆) 用 pair封装 {该点的度数,该点的标号}

找到符合的点就将其删除(注:这里容易假删除,导致后面还会计算这个点),在把该点连接的点遍历一下,连接点 度减一

... ...

AC代码如下:

#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});}}elsesum += val[it.second];}if (cnt & 1)ans += sum;}cout << ans << '\n';
}
signed main()
{int T;cin >> T;while (T--)solve();
}

Ponds(拓扑 + 优先队列)相关推荐

  1. hdu 5438 Ponds 拓扑排序

    Ponds Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/contests/contest_showproblem ...

  2. P3243 [HNOI2015]菜肴制作(拓扑 + 优先队列)

    题目描述: 知名美食家小 A 被邀请至 ATM 大酒店,为其品评菜肴.ATM 酒店为小 A 准备了 n 道菜肴,酒店按照为菜肴预估的质量从高到低给予 1 到 n 的顺序编号,预估质量最高的菜肴编号为 ...

  3. HDU - 5438 Ponds 拓扑 dfs

    题意 在一个图中 给出了每个点得权值和连边 想要尽可能删除一些联通的点数小于2的点 删完后 求最后剩下联通块内点得数量是奇数的权值和 分析 本题由于在删除点得过程中需要考虑 当把当前点删除后 其联通的 ...

  4. HDU5438 Ponds 拓扑+并查集

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5438 题目大意:n个顶点m条边的无向图,每个顶点有一个权值,现在要删除所有的叶子节点,重复此操作直至形 ...

  5. hdu1285 拓扑排序+优先队列

    原题地址 这算是我个人AC的第一个拓扑排序题目吧. 题目解读 给出几组比赛的胜负情况.推断最后的排名.依据题意这就是一个明显的拓扑排序问题了. 注意 假设由于可能的排名有多种情况,这时要保证编号小的在 ...

  6. zcmu-2153(拓扑排序+优先队列)

    2153: D.ly的排队问题 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 35  Solved: 13 [Submit][Status][Web ...

  7. HDU 4857 拓扑排序 优先队列

    n个数,已经有大小关系,现给m个约束,规定a在b之前,剩下的数要尽可能往前移.输出序列 大小关系显然使用拓扑结构,关键在于n个数本身就有大小关系,那么考虑反向建图,优先选择值最大的入度为零的点,这样得 ...

  8. C++实现拓扑排序(vector模拟邻接表存储,优先队列实现)

    代码如下: #include <iostream> #include <queue> #include <vector> using namespace std; ...

  9. P3243-[HNOI2015]菜肴制作【拓扑排序,优先队列】

    正题 题目链接:https://www.luogu.com.cn/problem/P3243 题目大意 nnn个数,有mmm个要求形如xxx在yyy的前面,现在要求在i−1i-1i−1尽量靠前的情况下 ...

最新文章

  1. python批量重命名指定目录下所有文件的后缀名
  2. Javascript字符串的格式化
  3. 在C#中使用SerialPort类实现串口通信
  4. MATLAB从入门到精通:搭建一个 简单的Bp神经网络(附代码)
  5. MyCat学习:使用MySQL搭建主从复制(一主一从模式)
  6. Android切换到主线程
  7. PoiDocxDemo【Android将表单数据生成Word文档的方案之二(基于Poi4.0.0),目前只能java生成】...
  8. Linux设备管理(三)_总线设备的挂接
  9. kaldi 语音识别
  10. Visual Studio 2013中因Browser Link引起的Javascript错误
  11. 机器学习-西瓜书、南瓜书第四章
  12. 小米手机android程序闪退,小米手机软件闪退是什么原因
  13. AI语音红外遥控配网教程
  14. “蓝色巨人”转向,极氪会是吉利的一剂“良药”吗?
  15. C++基础入门(超详细)
  16. 输出1至100的数字之和;
  17. 迈禹牌净水器换滤芯的做法
  18. 【翻译】--docker是什么
  19. arduino灯带随音乐_【创客玩音乐】用灯带让音乐可视化
  20. mysql timestamp 默认_MySQL数据库TIMESTAMP怎么设置默认值 | 学步园

热门文章

  1. centos创建文件服务器,创建一个centos云服务器
  2. ActiveSync编程总结(一)
  3. 组合数奇偶性判定方式
  4. Linux对光标的操作与创建文件lseek、creat
  5. 如何查看某端口对应什么服务?
  6. C中使用汇编定义的字符串
  7. TransactionTemplate编程式事务
  8. 安装了微软正版验证补丁的解决方法
  9. 【摄影】计算机保存图片的方式
  10. Math类几个容易混淆的方法