Ponds
Ponds
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1394 Accepted Submission(s): 467
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
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.
题意:有很多池塘,有的两个池塘间有管道连着。经费问题,要删除一些就连着一个管道的池塘。注意删除一个池塘之后,原本可能不用删除的,可能需要删除。4连着两个池塘2和3,2被删除后,4也要删除,因为连着4的管道由之前的2变成1个了。问剩下的在一个联通块里边池塘个数是奇数的,池塘价值之和。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<stack>
#include<vector>
#include<algorithm> using namespace std; #define maxn 10007 vector<vector<int> > G;
int used[maxn];
int a[maxn*], b[maxn*];
int f[maxn], v[maxn], sum[maxn], r[maxn];
long long ans; int found(int x)
{
if(f[x] != x)
f[x] = found(f[x]);
return f[x];
} int main()
{
int c, n, m;
scanf("%d", &c);
queue<int> Q; while(c--)
{
ans = ;
G.resize(maxn+);
G.clear();
memset(used, , sizeof(used));
memset(sum, , sizeof(sum));
memset(a, , sizeof(a));
memset(b, , sizeof(b)); scanf("%d%d", &n, &m);
for(int i = ; i <= n; i++)
{
f[i] = i;
r[i] = ;
scanf("%d", &v[i]);
} for(int i = ; i < m; i++)
{
scanf("%d%d", &a[i], &b[i]);
r[a[i]]++, r[b[i]]++;
G[a[i]].push_back(b[i]);
G[b[i]].push_back(a[i]);
}
for(int i = ; i <= n; i++)
if(r[i] < )
Q.push(i); while(Q.size())
{
int u = Q.front();
Q.pop();
used[u] = ;
int len = G[u].size();
for(int i = ; i < len; i++)
{
int v = G[u][i];
r[v]--;
if(r[v] < && !used[v])
Q.push(v);
}
}
for(int i = ; i < m; i++)
{
if(!used[a[i]] && !used[b[i]])
{
int u = found(a[i]);
int v = found(b[i]);
if(u != v)
f[v] = u;
}
}
for(int i = ; i <= n; i++)
if(!used[i])
sum[found(i)]++; for(int i = ; i <= n; i++)
{
if(!used[i] && sum[f[i]] % )
ans += v[i];
}
printf("%I64d\n", ans);
}
return ;
}
Ponds的更多相关文章
- hdu5438 Ponds dfs 2015changchun网络赛
Ponds Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Sub ...
- hdu 5438 Ponds dfs
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Problem Descr ...
- hdu 5438 Ponds 拓扑排序
Ponds Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/contests/contest_showproblem ...
- hdu 5438 Ponds(长春网络赛 拓扑+bfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5438 Ponds Time Limit: 1500/1000 MS (Java/Others) ...
- HDU5438:Ponds(拓扑排序)
Ponds Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Sub ...
- 2015 ACM/ICPC Asia Regional Changchun Online Pro 1002 Ponds(拓扑排序+并查集)
Ponds Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Sub ...
- HDU 5438 Ponds
Ponds Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Sub ...
- hdu5438 Ponds[DFS,STL vector二维数组]
目录 题目地址 题干 代码和解释 参考 题目地址 hdu5438 题干 代码和解释 解答本题时参考了一篇代码较短的博客,比较有意思,使用了STL vector二维数组. 可以结合下面的示例代码理解: ...
- hdu5438 Ponds
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total Submissi ...
随机推荐
- jvm jstack log分析工具,在线分析
http://spotify.github.io/threaddump-analyzer Spotify提供的Web版在线分析工具,可以将锁或条件相关联的线程聚合到一起.
- 安装element-ui
element地址:https://element.eleme.cn/2.0/#/zh-CN/component/quickstart 1.在新建终端 [安装element-ui组件依赖]cnpm i ...
- 使用fiddler,提示系统找不到相应的文件FSE2.exe文件
使用fiddler时候遇到了如下问题: Rules中customize rules 时,提示系统找不到相应的文件FSE2.exe文件. 这个文件的位置可以在Tools->opinions-> ...
- python函数及调用
python的函数是一段重复多次可调用的代码,依据python的函数,我们可以利用函数式的编程,来减少代码的重复. 在本章节中,详细的介绍python的函数,以及python的函数式编程的与众不同,函 ...
- 【SD系列】SAP 退货冲账过账成本更新
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[SD系列]SAP 退货冲账过账成本更新 前 ...
- Haystack Python全文检索框架
Haystack 1.什么是Haystack Haystack是django的开源全文搜索框架(全文检索不同于特定字段的模糊查询,使用全文检索的效率更高 ),该框架支持Solr,Elasticsear ...
- C程序优化
1 优化内存使用 后期程序优化的时候看内存分配是否浪费:是否留有安全裕量:有些内存是否分配过多,有些内存是否分配了没使用:尤其是创建对象的时候
- C++析构函数的自动调用(析构函数必须是虚拟的,这样删除父类指针指向的子类对象,才能同时调用两者的析构函数,否则就没有机会调用子类析构函数)
class A {public: A() { printf("A \n"); } ~A() { printf(" ~A \n"); } // 这里不管写不写vi ...
- SQL server 关于 GROUP BY 详细讲解和用法
1. Group By 语句简介: Group By语句从英文的字面意义上理解就是“根据(by)一定的规则进行分组(Group)”.它的作用是通过一定的规则将一个数据集划分成若干个小的区域,然后针对若 ...
- 【JAVA】Java 异常中e的getMessage()和toString()方法的异同
参考链接 CSDN: Java 异常中e的getMessage()和toString()方法的异同 示例代码1: public class TestInfo { private static ...