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 ...
随机推荐
- unigui 服务器 是否显示 程序窗口
unigui 服务器 是否显示 程序窗口 servermodule 窗体的这个standaloneserver属性 为false 时 显示窗体. 为true 时 不显示窗体. 哈哈
- jmeter逻辑控制器详解(2)
逻辑控制器 8.Runtime Controller 运行周期控制器,顾名思义,这是一种设置运行时间的控制器,它的效果就是使该控制器下的子项运行时间为[Runtime]中的数值(单位:s). Runt ...
- jdbc步骤:
一.注册数据库驱动 Class.forName("com.mysql.jdbc.Driver"); 二.建立连接(Connection) Connection conn = Dri ...
- SpringBoot(二) -- SpringBoot配置
一.配置文件 SpringBoot可以使用两种类型的配置文件(文件名固定): application.properties application.yml 配置文件的作用就是来修改SpringBoot ...
- ubuntu开机只有一条横杠在闪的解决办法
1.制作U盘启动盘,并试用ubuntu 2.输入以下命令,根据提示完成修复 sudo add-apt-repository ppa:yannubuntu/boot-repair && ...
- java_第一年_JavaWeb(12)
SimpleTag标签 定义了五个方法:setJspContext.setJspBody.setParent和getParent以及最重要的doTag方法(完成了所有的业务逻辑): setJspCon ...
- exec 命令
source source命令即点(.)命令. 在bash下输入man source,找到source命令解释处,可以看到解释”Read and execute commands from filen ...
- 用命令行远程导出MySQL数据
mysqldump -h10.10.9.197 -uroot -proot --default-character-set=utf8 0610_eshop >C:/Users/Adm inist ...
- 初学css 行内元素与块级元素
行内元素与块级元素直观上的区别 1.行内元素会在一条直线上排列,都是同一行的,水平方向排列块级元素各占据一行,垂直方向排列.块级元素从新行开始结束接着一个断行. 2.块级元素可以包含行内元素和块级元素 ...
- 构造函数与new的汇编实现
this指针,通常是通过ecx传递:gcc是通过堆栈传递的,是最后一个被压栈.传递this指针是为了访问成员变量.除了虚函数,所有成员函数被编译之后都是全局函数.mov eax,[ecx] ; 将第一 ...