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 仅包含一个整数,表示可以 ...
随机推荐
- 洛谷 4035 [JSOI2008]球形空间产生器
题目戳这里 一句话题意 给你 n+1 个 n 维点,需要你求出这个n维球的球心.(n<=10) Solution 这个题目N维的话确实不好想,反正三维就已经把我搞懵了,所以只好拿二维类比. 首先 ...
- docker centos yum 源
aliyun yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.rep ...
- Java并发之BlockingQueue
一.Queue Queue是队列接口是 Collection的子接口.除了基本的 Collection操作外,队列还提供其他的插入.提取和检查操作.每个方法都存在两种形式:一种抛出异常(操作失败时 ...
- deviceToken的获取(二)
第一步:申请证书: 第二步:申请app ids,应用名字必须一致.然后再进入进行编辑,使其enable,绿灯. 第三步:申请provisioning profile,生成.mobileprovisio ...
- 【转】kalman滤波
Kalman Filter是一个高效的递归滤波器,它可以实现从一系列的噪声测量中,估 计动态系统的状态.广泛应用于包含Radar.计算机视觉在内的等工程应用领域,在控制理论和控制系统工程中也是一个非常 ...
- [原创]java WEB学习笔记24:MVC案例完整实践(part 5)---删除操作的设计与实现
本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...
- hd acm1017
Problem Description Given two integers n and m, count the number of pairs of integers (a,b) such tha ...
- hiho一下 第四十八周 拓扑排序·二【拓扑排序的应用 + 静态数组 + 拓扑排序算法的时间优化】
题目1 : 拓扑排序·二 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho所在学校的校园网被黑客入侵并投放了病毒.这事在校内BBS上立刻引起了大家的讨论,当 ...
- Spring Cloud之Hystrix服务保护框架
服务保护利器 微服务高可用技术 大型复杂的分布式系统中,高可用相关的技术架构非常重要. 高可用架构非常重要的一个环节,就是如何将分布式系统中的各个服务打造成高可用的服务,从而足以应对分布式系统环境中的 ...
- 红米.USB安装_无法打开
1.必须有 SIM卡,才能打开 USB安装 红米1s(miui8.5)就是这样 2. 3. 4. 5.