Ponds

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

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
 
题意:一些池塘,能够破坏其的条件是其只与一个池塘相连或者不与任何池塘相连,问最后每个连通分量里面池塘数为奇数的点的权值之和为多少??
被坑了!。。刚刚只考虑了度为1的点,度为0的被忽视了。WA三次。。
#include<stdio.h>
#include<iostream>
#include<string.h>
#include <stdlib.h>
#include<math.h>
#include<algorithm>
#include<queue>
using namespace std;
typedef long long LL;
const int N = ;
const int M = ;
struct Edge{
int v,next;
}edge[*M];
int head[N];
LL value[N];
int indegree[N];
int tot;
int vis[N];
queue<int> q;
void init(){
while(!q.empty()) q.pop();
memset(indegree,,sizeof(indegree));
memset(head,-,sizeof(head));
memset(vis,,sizeof(vis));
tot=;
}
void addEdge(int u,int v,int &k){
edge[k].v = v,edge[k].next = head[u],head[u]=k++;
}
LL weight;
int cnt;
void dfs(int u){
vis[u]=;
cnt++;
weight+=value[u];
for(int k=head[u];k!=-;k=edge[k].next){
int v=edge[k].v;
if(!vis[v]){
dfs(v);
}
}
}
int main()
{
int tcase;
scanf("%d",&tcase);
while(tcase--)
{
init();
int n,m;
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++) {
scanf("%lld",&value[i]);
}
for(int i=;i<=m;i++){
int a,b;
scanf("%d%d",&a,&b);
indegree[a]++,indegree[b]++;
addEdge(a,b,tot);
addEdge(b,a,tot);
}
for(int i=;i<=n;i++){
if(indegree[i]==) q.push(i);
}
while(!q.empty()){
int u = q.front();
vis[u]=;
q.pop();
for(int k = head[u];k!=-;k=edge[k].next){
int v = edge[k].v;
if(!vis[v]){
indegree[v]--;
indegree[u]--;
if(indegree[v]==) q.push(v);
}
}
}
LL res = ;
for(int i=;i<=n;i++){
weight=,cnt=;
if(!vis[i])
dfs(i);
if(cnt&&&cnt>){
res+=weight;
}
}
printf("%lld\n",res);
}
return ;
}

hdu 5438(类似拓扑排序)的更多相关文章

  1. hdu 5438 Ponds 拓扑排序

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

  2. HDU.2647 Reward(拓扑排序 TopSort)

    HDU.2647 Reward(拓扑排序 TopSort) 题意分析 裸的拓扑排序 详解请移步 算法学习 拓扑排序(TopSort) 这道题有一点变化是要求计算最后的金钱数.最少金钱值是888,最少的 ...

  3. bfs+dfs乱搞+类似拓扑排序——cf1182D

    代码不知道上了多少补丁..终于过了 用类似拓扑排序的办法收缩整棵树得到x,然后找到x直连的最远的和最近的点 只有这三个点可能是根,依次判一下即可 另外题解的第一种方法时找直径,然后判两端点+重心+所有 ...

  4. ACM: hdu 2647 Reward -拓扑排序

    hdu 2647 Reward Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Des ...

  5. HDU 2647 Reward (拓扑排序)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 题意是给你n点m条有向边,叶子点(出度为0)上的值为888,父亲点为888+1,依次计算... ...

  6. hdu 4857 逃生 拓扑排序+PQ,剥层分析

    pid=4857">hdu4857 逃生 题目是求拓扑排序,但不是依照字典序最小输出,而是要使较小的数排在最前面. 一開始的错误思路:给每一个点确定一个优先级(该点所能到达的最小的点) ...

  7. HDU 1285 经典拓扑排序入门题

    确定比赛名次 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  8. HDU 2647 逆向拓扑排序

    令每一个员工都有一个自己的等级level[i] , 员工等级越高,那么工资越高,为了使发的钱尽可能少,所以每一级只增加一单位的钱 输入a b表示a等级高于b,那么我们反向添加边,令b—>a那么i ...

  9. HDU 4917 Permutation 拓扑排序的计数

    题意: 一个有n个数的排列,给你一些位置上数字的大小关系.求合法的排列有多少种. 思路: 数字的大小关系可以看做是一条有向边,这样以每个位置当点,就可以把整个排列当做一张有向图.而且题目保证有解,所以 ...

随机推荐

  1. jQuery获取动态添加的元素,live和on的区别

    今天给大家说一下如果用jQuery获取动态添加的元素,通常如果你在网页上利用jQuery添加一个元素,那么用平常的jQuery获取元素的方法无效的获取不到的.可以用以下的方法获取动态元素!假设我们现在 ...

  2. pandas时间数据的集成处理

    工作中遇到的一个问题: 统计各地区新能源汽车的充电时长 数据来源是北理新源的单日全球的运行数据. 这里仅统计北上广重庆四个地区的 数据处理的代码就省略了 需要整理好的是4个dataframe(数据已保 ...

  3. Geode 集群搭建,快速上手使用

    Geode 介绍: Geode是一个提供实时.一致访问大型分布式云平台下数据密集型应用的数据管理平台. Geode 通过跨多进程,把内存.CPU.网络资源和可选的本地磁盘汇集起来,来管理应用程序对象及 ...

  4. Java-framework-Vaadin

    安装vaadin: (1) 首先试了maven+vaadin. 安装maven: 1. unzip apache-maven-3.3.9-bin.zip 2. modify PATH environm ...

  5. atag信息处理

    machine_desc->boot_params参数保存的是u-boot传入的启动参数的地址,如果没有传入启动参数,使用如下的默认参数: /* * This holds our default ...

  6. debian卸载vmware

    原因: 由于vagrant默认支持virtualbox,而要支持vmware需要一个商用付费的插件.所以卸载vmware,使用virtualbox 具体操作: $ sudo vmware-instal ...

  7. 用PHP写的一个简单的分页类 2.0版

    <?php /* 分页类 用于实现对多条数据分页显示 version:2.0 //基于1.0 数据库查询用mysqli实现 author:Knight E-Mail:S.Knight.Work@ ...

  8. postgres 用户管理

    首次安装完成 pg 数据库后,会默认自带一个用户, 用户名: postgres 密码: postgres 可以使用命令 \du 查看数据库用户 创建新用户: create user dev with ...

  9. Python序列化、date、random、os模块

    知识点一:序列化与反序列化(json和pickple) 01 什么是序列化/反序列化    序列化就是将内存中的数据结构转换成一种中间格式存储到硬盘或者基于网络传输    发序列化就是硬盘中或者网络中 ...

  10. PHP魔术函数、魔术常量、预定义常量

    一.魔术函数(13个) 1.__construct() 实例化对象时被调用, 当__construct和以类名为函数名的函数同时存在时,__construct将被调用,另一个不被调用. 2.__des ...