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 ...
随机推荐
- spring mvc 接受数组
@RequestParam(value = "customerIds[]")Integer[] customerIds 加上 requestParam value设置为 &qu ...
- JDK和SDK的区别:
参考链接:https://www.cnblogs.com/vaelailai/p/7976158.html jdk,是Java开发工具包,主要用于编写Java程序:也就是说你要使用Java语言,就需要 ...
- ETROBOT——审题
参加了比赛,但是总要理解比赛相关的东西,发现以前瞎写的东西有人看,并且还有挺多人看的,所以打算继续在这里面,做记录. 源: http://www.etrobo.jp/2018/gaiyou/intro ...
- Mysql 免安装版本配置
1. 安装命令 (制定安装目录的my.ini文件) mysqld --install MySQL --defaults-file="C:\mysql-5.7.26-winx64\bin\my ...
- debian下重装mysql
mysql总是报错,说sock文件不存在,网上若干方法,更改权限,更改配置文件,结果还是不能正常生成.sock文件.没办法,删除,重新安装. 完全删除: 删除 mysqlsudo apt-get au ...
- ES6新数据类型Symbol
Symbol 需计算字面量属性时使用 const benz = Symbol('benz'); const car = { [benz]: 'benz car' }; const info = Sym ...
- 牛客练习赛51 C 勾股定理https://ac.nowcoder.com/acm/contest/1083/C
题目描述 给出直角三角形其中一条边的长度n,你的任务是构造剩下的两条边,使这三条边能构成一个直角三角形. 输入描述: 一个整数n. 输出描述: 另外两条边b,c.答案不唯一,只要输出任意一组即为合理, ...
- JVM内存模型及参数调优
堆.栈.方法区概念区别 1.堆 堆内存用于存放由new创建的对象和数组.在堆中分配的内存,由java虚拟机自动垃圾回收器来管理.根据垃圾回收机制的不同, Java堆有可能拥有不同的结构,最为常见的就是 ...
- centos7.4安装mysql
yum -y install mysql mysql-server mysql-devel mysql-server报错:No package mysql-server available,另外两个没 ...
- 第二讲shiro异常及执行流程
在认证过程中,有一个父异常为:AuthenticationException 该异常有几个子类,分别对应不同的异常情况: (1)DisabledAccountException:账户失效异常 (2)E ...