Ponds

Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1394    Accepted Submission(s): 467

Problem 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
 
Source

题意:有很多池塘,有的两个池塘间有管道连着。经费问题,要删除一些就连着一个管道的池塘。注意删除一个池塘之后,原本可能不用删除的,可能需要删除。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的更多相关文章

  1. hdu5438 Ponds dfs 2015changchun网络赛

    Ponds Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Sub ...

  2. hdu 5438 Ponds dfs

    Time Limit: 1500/1000 MS (Java/Others)     Memory Limit: 131072/131072 K (Java/Others) Problem Descr ...

  3. hdu 5438 Ponds 拓扑排序

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

  4. hdu 5438 Ponds(长春网络赛 拓扑+bfs)

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

  5. HDU5438:Ponds(拓扑排序)

    Ponds Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Sub ...

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

  7. HDU 5438 Ponds

    Ponds Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Sub ...

  8. hdu5438 Ponds[DFS,STL vector二维数组]

    目录 题目地址 题干 代码和解释 参考 题目地址 hdu5438 题干 代码和解释 解答本题时参考了一篇代码较短的博客,比较有意思,使用了STL vector二维数组. 可以结合下面的示例代码理解: ...

  9. hdu5438 Ponds

    Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submissi ...

随机推荐

  1. Jmeter的中英文互换

    1.jmeter的中英文互换:为了更深入的了解Jmeter,一般使用英文版的jmeter. 旧版本下载的默认中文较多.新版本5.1的下载后默认英文比较的多. 方法一: 选项->选择一种语言,但是 ...

  2. jmeter逻辑控制详解(1)

    逻辑控制器 Jmeter提供了多种逻辑控制器,下面进行讲解说明: 1.Simple Controller 简单控制器是最基本的控制器,对jmeter测试运行没有任何影响,可以将某些请求归集在一个简单控 ...

  3. 【FICO系列】SAP FICO 基本概念

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[FICO系列]SAP FICO 基本概念   ...

  4. pandas 入门(2)

    from pandas import Series, DataFrame, Index import numpy as np from numpy import nan as NA obj = Ser ...

  5. Java容器框架总结(一)

    本篇根据<Java编程思想> 第11章 持有对象 整理,总结Java容器框架中常用集合类及接口的特点及使用场景. (一)总结 1)数组将数字与对象联系起来:可以保存基本类型的数据:一旦生成 ...

  6. spring-第十五篇之AOP面向切面编程之AspectJ框架简单应用

    1.去官方网站下载aspectj-1.8.0.jar 2.在jar包目录启动cmd,执行java -jar aspectj-1.8.0.jar,Next 3.检查JAVA_HOME路径是否正确,如果不 ...

  7. git ssh key配置&解决git每次输入密码

    git ssh key配置&解决git每次输入密码:https://blog.csdn.net/qq_42817227/article/details/81415404

  8. Codeforces 990C (模拟+组合数学)

    题面: 传送门 分析: 此题O(n2l)" role="presentation" style="position: relative;">O( ...

  9. HDU 6468 /// DFS

    题目大意: 把 1~15 的数字典序排序后为 1, 10, 11, 12, 13, 14, 15, 2, 3, 4, 5, 6, 7, 8, 9 此时给定 n k, 求1~n的数组字典序排序后 第k个 ...

  10. python学习第四十六天dir( )函数用法

    dir( )函数有点像目录的意思,但是他是包含由模块定义的名称的字符串的排序列表.这个列表包含模块中定义的所有模块,变量和函数的名称. 列举其用法 import time content = dir( ...