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. 2017-2018-1 20179209《Linux内核原理与分析》第八周作业

    Linux内核如何装载和启动一个可执行程 一.实验 1.1理解编译链接的过程和ELF可执行文件格式. 1.1.1编译链接过程 能用图说明的问题,就少用文字描述: 1.1.2ELF可执行文件 ELF可执 ...

  2. 16.Django提交表单

    Django表单提交一共有三种方式: 1.不使用Django组件进行提交 2.使用django.forms.Form(所有表单类的父类)进行提交 3.使用django.forms.ModelForm( ...

  3. centos7 使用 maven

    http://www.cnblogs.com/jackluo/archive/2013/02/06/2901816.html

  4. pom.xml配置文件详解(转发)

    setting.xml主要用于配置maven的运行环境等一系列通用的属性,是全局级别的配置文件:而pom.xml主要描述了项目的maven坐标,依赖关系,开发者需要遵循的规则,缺陷管理系统,组织和li ...

  5. 说说JavaScript 中的new吧

    在其他语言中,new操作符都是用来实例化创建一个对象的,JavaScript 中同样如此,但是它又有一些不同.为了说清楚这个问题我们先来看一下JavaScript 中的类.原型.原型链.继承这些概念吧 ...

  6. Lean In - 读书笔记

    以下就是摘录了这本书中的相关内容. 他告诉我,想要“赢得每个人的喜欢”这种想法会阻碍我的发展.他说,当你想要让事情有所改变时,你不可能取悦每个人:而如果你去取悦每个人,你就不会获得充分的进步.扎克伯格 ...

  7. spring-boot4

    1.1.1. Starter pom 除了官方也有其他第三方提供的starter Websocket是服务端推数据到客户端.长连接. 1.1.1.Xml 配置文件 有些时候必须使用xml配置. 1.1 ...

  8. P4103 [HEOI2014]大工程

    题目 P4103 [HEOI2014]大工程 化简题目:在树上选定\(k\)个点,求两两路径和,最大的一组路径,最小的一组路径 做法 关键点不多,建个虚树跑一边就好了 \(sum_i\)为\(i\)子 ...

  9. web前端框架之自定义form表单验证

    自定义form验证初试 .在后端创建一个类MainForm,并且在类中自定义host ip port phone等,然后写入方法,在post方法中创建MainForm对象,并且把post方法中的sel ...

  10. Recovery模式【转】

    本文转载自:http://tieba.baidu.com/p/2299027486 Recovery模式是手机系统的一个工程模式,作用是恢复和清除.用户进入这个模式之后,可以对当前系统的一些数据进行清 ...