题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5438

Ponds

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

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
 
Source
 
题目大意:有一些池塘,每一个池塘都有一个价值,现在想删除一些池塘。
有如下删除条件:1、一个池塘有两个管道连接的不可以删除。
2、求最后剩下的为奇数环的池塘的价值。
 
解题思路:用拓扑将所有入度为0和1的点都可以删掉,直到删完为止。在一个点一个点搜过去,判断环中是否为奇数个池塘。如果可以就return和,否则就不加,return0即可。
 
详见代码。
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue> using namespace std;
#define ll long long
const int N=; ll p,m,v[N],vis[N],indir[N];
vector<ll>G[N]; ll bfs(ll x)
{
queue<ll>q;
q.push(x);
vis[x]=;
ll k=;
ll sum=v[x];
while (!q.empty())
{ int s=q.front();
q.pop(); //cout<<s<<endl;
k++;
for (int i=; i<G[s].size(); i++)
{
if (!vis[G[s][i]]&&indir[G[s][i]]>=)//删掉的点不可以加进来
{
sum+=v[G[s][i]];
q.push(G[s][i]);
vis[G[s][i]]=;
}
}
}
if (k>=&&k%==)
return sum;
else
return ;
} int main()
{
int T,a,b;
scanf("%d",&T);
while (T--)
{
scanf("%lld%lld",&p,&m);
memset(vis,,sizeof(vis));
memset(G,,sizeof(G));
memset(indir,,sizeof(indir));
for (int i=; i<=p; i++)
{
scanf("%lld",&v[i]);
}
for (int i=; i<=p; i++)
G[i].clear();
for (int i=; i<=m; i++)
{
scanf("%d%d",&a,&b);
G[a].push_back(b);//将b放在a队列的最后一个
G[b].push_back(a);
indir[a]++;
indir[b]++;
}
int j;
for (int i=; i<=p; i++)
{
for ( j=; j<=p; j++)
{
if (indir[j]==||indir[j]==)
{
break;
}
}
if (j>p)
break;
indir[j]=-;
for (int k=; k<G[j].size(); k++)
{
indir[G[j][k]]--;
}
}
ll ans=;
for (int i=; i<=p; i++)//搜遍所有的环
if (!vis[i]&&indir[i]>=)
ans+=bfs(i);
cout<<ans<<endl;
}
return ;
}

hdu 5438 Ponds(长春网络赛 拓扑+bfs)的更多相关文章

  1. hdu 5442 (ACM-ICPC2015长春网络赛F题)

    题意:给出一个字符串,长度是2*10^4.将它首尾相接形成环,并在环上找一个起始点顺时针或逆时针走一圈,求字典序最大的走法,如果有多个答案则找起始点最小的,若起始点也相同则选择顺时针. 分析:后缀数组 ...

  2. hdu 5446(2015长春网络赛J题 Lucas定理+中国剩余定理)

    题意:M=p1*p2*...pk:求C(n,m)%M,pi小于10^5,n,m,M都是小于10^18. pi为质数 M不一定是质数 所以只能用Lucas定理求k次 C(n,m)%Pi最后会得到一个同余 ...

  3. 2012年长春网络赛(hdu命题)

    为迎接9月14号hdu命题的长春网络赛 ACM弱校的弱菜,苦逼的在机房(感谢有你)呻吟几声: 1.对于本次网络赛,本校一共6名正式队员,训练靠的是完全的自主学习意识 2.对于网络赛的群殴模式,想竞争现 ...

  4. HDU 4763 Theme Section (2013长春网络赛1005,KMP)

    Theme Section Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  5. HDU 4764 Stone (2013长春网络赛,水博弈)

    Stone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  6. HDU 4762 Cut the Cake (2013长春网络赛1004题,公式题)

    Cut the Cake Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  7. HDU 4759 Poker Shuffle(2013长春网络赛1001题)

    Poker Shuffle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  8. HDU 4768 Flyer (2013长春网络赛1010题,二分)

    Flyer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  9. 15年-ICPC长春-网络赛

    ID name status one word  POJ 5437 Alisha’s Party 赛后AC. 优先队列,模拟.对时间t排序 POJ 5438 Ponds 赛后AC 循环链表 POJ 5 ...

随机推荐

  1. MFC各种属性设置

    在使用MFC的时候经常需要对例如对话框的外观进行一些设置.MFC哪些属性的含义和设置可以参照博客: http://www.cnblogs.com/lzmfywz/archive/2012/04/20/ ...

  2. Delphi开发单机瘦数据库程序要点(后缀cds)

    一.概述 Delphi作为Windows下的一种快速开发工具,不仅能开发一般的Windows应用程序,而且还具有强大的数据库应用程序开发功能.Delphi本身提供了对BDE,ODBC,ADO和Inte ...

  3. P2461 [SDOI2008]递归数列

    题目描述 一个由自然数组成的数列按下式定义: 对于i <= k:ai = bi 对于i > k: ai = c1ai-1 + c2ai-2 + ... + ckai-k 其中bj 和 cj ...

  4. AtCoder Grand Contest 004

    AtCoder Grand Contest 004 A - Divide a Cuboid 翻译 给定一个\(A*B*C\)的立方体,现在要把它分成两个立方体,求出他们的最小体积差. 题解 如果有一条 ...

  5. 【bzoj3575】 Hnoi2014—道路堵塞

    http://www.lydsy.com/JudgeOnline/problem.php?id=3575 (题目链接) 题意 给出一个有向图和一条最短路,问最短路上任意一条边断掉,此时的最短路是多少. ...

  6. 【bzoj2594】 Wc2006—水管局长数据加强版

    http://www.lydsy.com/JudgeOnline/problem.php?id=2594 (题目链接) 题意 给出一个带边权的无向简单,要求维护两个操作,删除${u,v}$之间的连边: ...

  7. BGP的那些安全痛点(转)

    0x00 BGP(RFC 1771. RFC 4271)定义 全称是Border Gateway Protocol, 对应中文是边界网关协议,最新版本是BGPv4. BGP是互联网上一个核心的互联网去 ...

  8. Linux内核分析实验二:mykernel实验指导(操作系统是如何工作的)

    计算机是如何工作的?(总结)——三个法宝 存储程序计算机工作模型,计算机系统最最基础性的逻辑结构: 函数调用堆栈,高级语言得以运行的基础,只有机器语言和汇编语言的时候堆栈机制对于计算机来说并不那么重要 ...

  9. C++中#define用法

    http://blog.sina.com.cn/s/blog_686188ef0100klku.html #define是C语言中提供的宏定义命令,其主要目的是为程序员在编程时提供一定的方便,并能在一 ...

  10. Linux发不出分片包的问题分析

    今日有个网络攻击模拟需求,要打分片的ip包,程序写好了,在开发机上验证也没问题,然后部署到沙盒环境之后不行,就是发不出来数据包,而不分片的数据包能够正常发送,定位过程如下 1.对比了两台机器/proc ...