Ponds

Time Limit: 1 Sec

Memory Limit: 256 MB

题目连接

http://acm.hdu.edu.cn/contests/contest_showproblem.php?pid=1001&cid=621

Description

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 v.

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≤104) 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≤108) indicating the value of pond i.

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

Output

For each test case, output the sum of the value of all connected components consisting of odd number of ponds after removing all the ponds connected with less than two pipes.

Sample Input

1
7 7
1 2 3 4 5 6 7
1 4
1 5
4 5
2 3
2 6
3 6
2 7

Sample Output

21

HINT

题意

给你一个图,然后要求把度数小于2的点全部删去,然后问你奇数集合的点权和是多少

注意,你删去了一个点之后,可能会使得一些点又变成了度数小于2的

题解:

用类似拓扑排序的思想去做就ok啦

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>using namespace std;const int N=200100;
long long a[N],ans;
int n,m,T,cnt,ok[N],vis[N],pre[N],nxt[N],to[N],tot[N],col;
vector<int> s[N];
queue<int> q;void dfs(int x,int fa)
{s[col].push_back(x);ok[x]=0;for(int p=pre[x];p!=-1;p=nxt[p]){if((!vis[p])||(!ok[to[p]])) continue;if(p==(fa^1)) continue;dfs(to[p],p);}
}
void makeedge(int x,int y)
{to[cnt]=y;nxt[cnt]=pre[x];pre[x]=cnt++;to[cnt]=x;nxt[cnt]=pre[y];pre[y]=cnt++;
}int main()
{scanf("%d",&T);while(T--){memset(tot,0,sizeof(tot));memset(pre,-1,sizeof(pre));memset(ok,1,sizeof(ok));memset(vis,1,sizeof(vis));ans=0LL;cnt=0;scanf("%d%d",&n,&m);for(int i=1;i<=n;i++){scanf("%I64d",&a[i]);}for(int i=0;i<m;i++){int x,y;scanf("%d%d",&x,&y);tot[x]++;tot[y]++;makeedge(x,y);}while(!q.empty()) q.pop();for(int i=1;i<=n;i++){if(tot[i]<2){q.push(i);}}while(!q.empty()){int x=q.front();q.pop();ok[x]=0;for(int p=pre[x];p!=-1;p=nxt[p]){vis[p]=0;tot[x]--;tot[to[p]]--;if(ok[to[p]]&&tot[to[p]]<2){q.push(to[p]);}}}col=0;for(int i=1;i<=n;i++){col++;s[col].clear();if(ok[i]){dfs(i,cnt+10);if(s[col].size()%2==1){for(int j=0;j<s[col].size();j++){ans+=a[s[col][j]];}}}}printf("%I64d\n",ans);}return 0;
}

转载于:https://www.cnblogs.com/qscqesze/p/4805256.html

hdu 5438 Ponds 拓扑排序相关推荐

  1. HDU - 5438 Ponds 拓扑 dfs

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

  2. HDU 2647 Reward (拓扑排序)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 题意是给你n点m条有向边,叶子点(出度为0)上的值为888,父亲点为888+1,依次计算... ...

  3. HDU 2647 Reward 拓扑排序

    http://acm.hdu.edu.cn/showproblem.php?pid=2647 题意: 输入N和M代表N个人和M组数据,M组数据中的A和B代表A的工资要比B的工资高,底薪是(888元), ...

  4. hdu 4857 逃生 拓扑排序

    逃生 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=4857 Description 糟糕的事情发生啦,现在大家都忙着逃命.但是逃命的通道很窄,大家只 ...

  5. HDU 4857 逃生 拓扑排序好题 第一次做CLJ出的题

    逃生 Problem Description 糟糕的事情发生啦,现在大家都忙着逃命.但是逃命的通道很窄,大家只能排成一行. 现在有n个人,从1标号到n.同时有一些奇怪的约束条件,每个都形如:a必须在b ...

  6. HDU 5438 Ponds (搜索)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5438 题面: Ponds Time Limit: 1500/1000 MS (Java/Others) ...

  7. hdu 5439 Ponds(长春网络赛——拓扑排序+搜索)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5438 Ponds Time Limit: 1500/1000 MS (Java/Others)     ...

  8. HDU 3342 Legal or Not(拓扑排序判断成环)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3342 题目大意:n个点,m条有向边,让你判断是否有环. 解题思路:裸题,用dfs版的拓扑排序直接套用即 ...

  9. HDU 1811 Rank of Tetris(并查集按秩合并+拓扑排序)

    Rank of Tetris Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

最新文章

  1. java.math.BigDecimal()的用法
  2. vue el-checkbox循环多个如何选中当前的_一次关于Vue的自我模拟面试
  3. xctf php序列化,xctf一道反序列化题
  4. C++STL之双端队列Deque
  5. 绘制电气电路中的电阻——Visio制图总结【电控类】(二)
  6. 典型计算机控制系统硬件组成框图,计算机控制技术重要.docx
  7. Android 签名文件的sha1值查看
  8. 飞鱼星的虚拟服务器设置,设置简单功能丰富 三步就能设置好_飞鱼星 G7_网络设备无线网络和技术-中关村在线...
  9. python高级编程书籍推荐_Python书籍推荐
  10. 移动应用程序和网页应用程序_设计网站和移动应用程序的空白状态页
  11. 数据科学必备用Python进行描述性统计数据分析详解
  12. 利用Python爬虫爬取淘宝商品做数据挖掘分析实战篇,超详细教程!
  13. MD-Pandoc进阶教程:图表、文献和目录
  14. java中逗号运算符的含义_逗号运算符什么时候有用?
  15. 解决crontab定时任务多次执行
  16. 为什么 5G 网络有时候会比较慢
  17. 苹果电脑带有双系统Mac/Win,如何更改默认启动系统?
  18. 【Unity】预计算刚体运动轨迹
  19. elevation的使用
  20. (二)GL 简单绘制

热门文章

  1. docker报错:driver failed programming external connectivity on endpoint, iptables:No chain by that name
  2. 2022-2028年中国盲盒产业研究及前瞻分析报告
  3. 2022-2028年中国氢化环氧树脂产业发展动态及投资趋势预测报告
  4. volatile关键字之全面深度剖析
  5. Apache POI:解决数据库和Excel之间相互转换的烦恼~
  6. EDA电子设计技术与应用
  7. matrix_multiply代码解析
  8. 双圆弧插值算法(三,代码实现)
  9. 深夜凌晨女朋友问什么是数据仓库,我的回答让她惊讶,然后发现。。。
  10. Docker的安装和版本详细介绍