题目链接: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. 安装iproute2

    yum install -y gcc bison flex #tar -xzf  iproute2-3.17.0.tar.gz #cd iproute2-3.17.0#sed -i '/^TARGET ...

  2. m3u8 player

    m3u8 player m3u8 是一种基于 HTTP Live Streaming 文件视频格式,它主要是存放整个视频的基本信息和分片(Segment)组成.目前 由 Apple.inc 率先提出的 ...

  3. 图像分割——graph cuts

    Graph cuts是一种基于图论的方法,它是一种能量优化算法,在计算机视觉领域应用于前景背景分割,立体视觉,抠图等. 这类方法首先使用无向图G=<V,E>表示要分割的图像,V和E分别是顶 ...

  4. MT【143】统一分母

    已知$a,b>0$,则$m=\dfrac{b^2+2}{a+b}+\dfrac{a^2}{ab+1}$的最小值是______ 解答: $$m\geqslant \dfrac{b^2+2}{\sq ...

  5. BZOJ 2521: [Shoi2010]最小生成树

    2521: [Shoi2010]最小生成树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 445  Solved: 262[Submit][Statu ...

  6. 【BZOJ5334】数学计算(线段树)

    [BZOJ5334]数学计算(线段树) 题面 BZOJ 洛谷 题解 简单的线段树模板题??? 咕咕咕. #include<iostream> #include<cstdio> ...

  7. BZOJ4037:[HAOI2015]数字串拆分——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=4037 你有一个长度为n的数字串.定义f(S)为将S拆分成若干个1~m的数的和的方案数,比如m=2时 ...

  8. HTML的标签元素分类的区别

    HTML ,即Hyper Text Markup Language 超文本标记语言: 文本:纯字符,如window中的txt文本 超文本:在纯文本中嵌入样式,图片,音频,视频,链接等内容 HTML的基 ...

  9. 解题:POI 2013 Triumphal arch

    题面 二分答案,问题就转化为了一个可行性问题,因为我们不知道国王会往哪里走,所以我们要在所有他可能走到的点建造,考虑用树形DP解决(这个DP还是比较好写的,你看我这个不会DP的人都能写出来=.=) 定 ...

  10. 【loj2586】【APIO2018】选圆圈

    题目 有 \(n\) 个圆$c_1,c_2, \cdots , c_n $,执行如下的操作: 找到剩下的半径最大的圆删除并删除所有和它有交的其他并没有被删除的圆: 求每个圆是被那个圆删除的: $1 \ ...