HDU5438:Ponds(拓扑排序)
Ponds
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 3204 Accepted Submission(s): 995
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.
#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]&°[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(拓扑排序)的更多相关文章
- hdu 5438 Ponds 拓扑排序
Ponds Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/contests/contest_showproblem ...
- HDU - 5438 Ponds(拓扑排序删点+并查集判断连通分量)
题目: 给出一个无向图,将图中度数小于等于1的点删掉,并删掉与他相连的点,直到不能在删为止,然后判断图中的各个连通分量,如果这个连通分量里边的点的个数是奇数,就把这些点的权值求和. 思路: 先用拓扑排 ...
- hdu5438 拓扑排序+DFS
解析 对一个有向无环图(Directed Acyclic Graph,简称DAG)G进行拓扑排序,是将G中所有顶点排成一个线性序列,使得图中任意一对顶点u和v,若<u,v> ∈E(G),则 ...
- HDU 5438 拓扑排序+DFS
Ponds Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Sub ...
- hdu5438 Ponds
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total Submissi ...
- 算法与数据结构(七) AOV网的拓扑排序
今天博客的内容依然与图有关,今天博客的主题是关于拓扑排序的.拓扑排序是基于AOV网的,关于AOV网的概念,我想引用下方这句话来介绍: AOV网:在现代化管理中,人们常用有向图来描述和分析一项工程的计划 ...
- 有向无环图的应用—AOV网 和 拓扑排序
有向无环图:无环的有向图,简称 DAG (Directed Acycline Graph) 图. 一个有向图的生成树是一个有向树,一个非连通有向图的若干强连通分量生成若干有向树,这些有向数形成生成森林 ...
- 【BZOJ-2938】病毒 Trie图 + 拓扑排序
2938: [Poi2000]病毒 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 609 Solved: 318[Submit][Status][Di ...
- BZOJ1565 [NOI2009]植物大战僵尸(拓扑排序 + 最大权闭合子图)
题目 Source http://www.lydsy.com/JudgeOnline/problem.php?id=1565 Description Input Output 仅包含一个整数,表示可以 ...
随机推荐
- activiti踩坑
最近在学习activiti,偶然间遇到一个错误:加载引擎的时候报错,显示空指针错误,跟代码发现初始化配置文件返回为null.几经排查,可能是因为我发布流程后又清空了数据库数据导致的.然后我把表全部删除 ...
- ubuntu14.04允许root远程链接、修改主机名
1.设置root密码 sudo passwd root 2.修改主机名 第一步:ubuntu主机名位于/etc/hostname里,将其修改为自己需要的名称. 第二步:修改/etc/hosts文件,将 ...
- 中国移动OnetNet云平台 使用WIFI模块ESP8266 TCP透传模式传输数据流步骤
测试使用工具: WIFI模块型号:ESP8266 https://item.taobao.com/item.htm?spm=a1z10.1-c.w137712-175513579.2.btbD9X&a ...
- shell中替换json中指定的值
在linux中部署软件的时候,有时会遇到用shell动态改动json格式的配置文件,比如一下rabbitmq.json文件: { "rabbitmq": { "ssl&q ...
- C#转换人民币大写金额
/// <summary> /// 转换人民币大写金额. /// </summary> public class RMBConverter { /// <summary& ...
- hd acm1017
Problem Description Given two integers n and m, count the number of pairs of integers (a,b) such tha ...
- Java -- AWT , GUI图形界面
1. AWT 容器继承关系 示例1: public class Main { public static void main(String[] args) throws Exception { Fra ...
- 大话设计模式--命令模式 Command -- C++实现实例
1. 命令模式: 将请求封装为一个对象,从而使你可以用不同的请求对客户进行参数化,对请求排队或记录请求日志,以及支持可撤销的操作. 命令模式有点: a. 较容易的设计一个命令队列 b. 在需要的的情况 ...
- linux 设备文件
一.设备文件 在dev目录下 外部设备通过创建好的设备文件连接到服务器上,例如可以通过设备号给连接上去的硬件发消息. 二.设备文件分类 块设备 按块为单位,随机访问的设备 常见的有 硬盘 字符设备 按 ...
- [转载]C++Assert()函数
assert宏的原型定义在<assert.h>中,其作用是如果它的条件返回错误,则终止程序执行,原型定义: #include <assert.h> void assert( i ...