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. Krypton Suite of .NET WinForms Controls

    The Krypton Suite of .NET WinForms controls are now freely available for use in personal or commeric ...

  2. Shell脚本中的特殊字符(美元符、反斜杠、引号等)作用介绍

    Shell中的特殊字符有 1.$ 美元符 2.\ 反斜杠 3.` 反引号 4." 双引号 5.< ,>;,*,?,[,] 下面我一一举列说明 一.$符号 1.echo $? 显示 ...

  3. CGI 环境变量

    CGI 环境变量 环境变量 意义 SERVER_NAME CGI脚本运行时的主机名和IP地址. SERVER_SOFTWARE 你的服务器的类型如: CERN/3.0 或 NCSA/1.3. GATE ...

  4. 使用xmake优雅地描述工程

    描述语法 xmake的描述语法基于lua实现,因此描述语法继承了lua的灵活性和简洁性,并且通过28原则,将描述作用域(简单描述).脚本作用域(复杂描述)进行分离,使得工程更加的简洁直观,可读性非常好 ...

  5. C#里sqlDataAdapter.fill(DataSet,String)的用法

    第二个参数 String是指定DataSet 里表的名字,例如 sqlDataAdapter.fill(DataSet,"学生表") 指定后,以后就可以这样调用这张表 DataSe ...

  6. 存储过程SET XACT_ABORT ON

    设置事务回滚的当为ON时,如果你存储中的某个地方出了问题,整个事务中的语句都会回滚为OFF时,只回滚错误的地方

  7. JAVA总结--dubbo与zookeeper

    读累了就看看实操https://www.cnblogs.com/huasky/p/10214642.html 一.SOA 概念:SOA:Service-Oriented Architecture,面向 ...

  8. AtCoder Beginner Contest 133 -D — Rain Flows into Dams

    (https://atcoder.jp/contests/abc133/tasks/abc133_d) 思路:每座山为2Xi,每个坝为Ai.已知Ai,求出2Xi. 根据已知的X1,则可分别求出X2-n ...

  9. wxpython中复选框的基本使用源码实例

    #coding=utf-8 import wx class MyFrame(wx.Frame): def __init__(self): wx.Frame.__init__(self,None,-1, ...

  10. Spark Thrift Server

    ThriftServer是一个JDBC/ODBC接口,用户可以通过JDBC/ODBC连接ThriftServer来访问SparkSQL的数据.ThriftServer在启动的时候,会启动了一个Spar ...