Ponds

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

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
 
思路:拓扑排序,动态删边,注意结果要用long long类型存储,int 会爆
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
const int MAXN=;
int n,m;
vector<int> arc[MAXN];
int val[MAXN],deg[MAXN],vis[MAXN];
void topsort()
{
queue<int> que;
for(int i=;i<=n;i++)
{
if(!vis[i]&&deg[i]<=)
{
que.push(i);
vis[i]=;
}
}
while(!que.empty())
{
int u=que.front();que.pop();
for(int i=;i<arc[u].size();i++)
{
int v=arc[u][i];
if(!vis[v])
{
deg[v]--;
if(deg[v]<=)
{
que.push(v);
vis[v]=;
}
}
}
}
}
void dfs(int u,int& num,long long& sum)
{
sum+=val[u];
num++;
vis[u]=;
for(int i=;i<arc[u].size();i++)
{
int v=arc[u][i];
if(!vis[v])
{
dfs(v,num,sum);
}
}
}
int main()
{
int T;
cin>>T;
while(T--)
{
cin>>n>>m;
for(int i=;i<=n;i++) arc[i].clear();
memset(deg,,sizeof(deg));
memset(vis,,sizeof(vis));
for(int i=;i<=n;i++)
{
cin>>val[i];
}
for(int i=;i<m;i++)
{
int u,v;
cin>>u>>v;
arc[u].push_back(v);
arc[v].push_back(u);
deg[u]++;
deg[v]++;
}
topsort();
long long res=;
for(int i=;i<=n;i++)
{
if(vis[i]) continue;
int num=;
long long sum=;
dfs(i,num,sum);
if(num&) res+=sum;
}
cout<<res<<endl;
}
return ;
}

HDU5438:Ponds(拓扑排序)的更多相关文章

  1. hdu 5438 Ponds 拓扑排序

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

  2. HDU - 5438 Ponds(拓扑排序删点+并查集判断连通分量)

    题目: 给出一个无向图,将图中度数小于等于1的点删掉,并删掉与他相连的点,直到不能在删为止,然后判断图中的各个连通分量,如果这个连通分量里边的点的个数是奇数,就把这些点的权值求和. 思路: 先用拓扑排 ...

  3. hdu5438 拓扑排序+DFS

    解析 对一个有向无环图(Directed Acyclic Graph,简称DAG)G进行拓扑排序,是将G中所有顶点排成一个线性序列,使得图中任意一对顶点u和v,若<u,v> ∈E(G),则 ...

  4. HDU 5438 拓扑排序+DFS

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

  5. hdu5438 Ponds

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

  6. 算法与数据结构(七) AOV网的拓扑排序

    今天博客的内容依然与图有关,今天博客的主题是关于拓扑排序的.拓扑排序是基于AOV网的,关于AOV网的概念,我想引用下方这句话来介绍: AOV网:在现代化管理中,人们常用有向图来描述和分析一项工程的计划 ...

  7. 有向无环图的应用—AOV网 和 拓扑排序

    有向无环图:无环的有向图,简称 DAG (Directed Acycline Graph) 图. 一个有向图的生成树是一个有向树,一个非连通有向图的若干强连通分量生成若干有向树,这些有向数形成生成森林 ...

  8. 【BZOJ-2938】病毒 Trie图 + 拓扑排序

    2938: [Poi2000]病毒 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 609  Solved: 318[Submit][Status][Di ...

  9. BZOJ1565 [NOI2009]植物大战僵尸(拓扑排序 + 最大权闭合子图)

    题目 Source http://www.lydsy.com/JudgeOnline/problem.php?id=1565 Description Input Output 仅包含一个整数,表示可以 ...

随机推荐

  1. lua解析json

    自己写的lua解析json,带容错,如果要是用于格式检查,得修改下.很简单直接贴代码 --------------------------------------------------json解析- ...

  2. Django 之 ModelForm 组件

    Django的model form组件 扩展:Django 之Form组件 首先我们要知道 Model 和 Form 分别时干什么的 Model  生成表数据 Form  对表单.字段进行校验 Dja ...

  3. PAT 1054. 求平均值 (20)

    本题的基本要求非常简单:给定N个实数,计算它们的平均值.但复杂的是有些输入数据可能是非法的.一个“合法”的输入是[-1000,1000]区间内的实数,并且最多精确到小数点后2位.当你计算平均值的时候, ...

  4. SAP文件夹的判断与创建

    [转自 http://blog.csdn.net/saphome/article/details/6956918] SAP文件夹存在的判断与创建 2010-08-29 20:15 相关函数:WS_QU ...

  5. tomcat调试页面的时候,不刷新

    1.调试一个页面的时候,js文件不刷新,也就是相当于没有改(cache) 2.解决:在Server中将当前tomcat的模式改为debug即可

  6. activiti--5 -----------------Activiti 工作流 流程各个步骤所涉及到的表

    ACT_RE_*: 'RE'表示repository. 这个前缀的表包含了流程定义和流程静态资源 (图片,规则,等等). ACT_RU_*: 'RU'表示runtime. 这些运行时的表,包含流程实例 ...

  7. 第10条:尽量用enumerate取代range

    核心要点: (1)enumerate函数提供了一种精简的写法,可以在遍历迭代器时获知每个元素的索引. (2)尽量用enumerate来改写那种将range与下标访问相结合的序列遍历代码. (3)可以给 ...

  8. Data Structure Trie: suffix problem

    http://www.geeksforgeeks.org/suffix-array-set-1-introduction/ http://www.geeksforgeeks.org/pattern-s ...

  9. P3988 [SHOI2013]发牌

    题目 P3988 [SHOI2013]发牌 做法 我们切牌时的状态: 手玩几次后我们发现切\(K\)次牌就是求堆顶一下的\(K+1\)大值,套上主席树就好了 My complete code #inc ...

  10. android电池(四):电池 电量计(MAX17040)驱动分析篇【转】

    本文转载自:http://blog.csdn.net/xubin341719/article/details/8969369 电池电量计,库仑计,用max17040这颗电量IC去计量电池电量,这种方法 ...