题目链接: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. docker中间件安装记录

    rabbitmq docker pull rabbitmq docker run --restart=on-failure: --name rabbitmq -d -p : -p : docker.i ...

  2. macOS & SVN

    macOS & SVN mac 下已经自带了svn环境; 使用 svn –version 查看版本号 安装方法: 已安装 XCode,只需要在 code > Preferences &g ...

  3. C# 键盘记录器

    设计背景 Win系统带有API可以获取键入值,本小程序主要应用了一个网上广为流传的类,可以说一个测试DEMO.有俗称为键盘钩子 设计思路 使用Win API获取建入值 相关技术 Win API 功能 ...

  4. Everyone is tester

    有一本书叫<人人都是产品经理>,作者在书中介绍了在做产品的过程中学到的思维方式和做事方式,受到行业大众的认可 作为一名测试老鸟,我想说,其实Everyone is tester     为 ...

  5. PHP是什么?

    PHP是什么? PHP是一门后端动态解释型计算机高级语言,一般用来编写或者生成动态网页,主要负责数据的处理与渲染.(这里是指用PHP嵌入网页里面的形式,现在可以直接用一些JS的框架去渲染网页数据了,P ...

  6. Mybatis笔记六:Mybatis中SqlSessionFactoryBuilder/SqlSessionFactory/SqlSession/映射器实例的作用域(Scope)和生命周期

    SqlSessionFactoryBuilder 这个类可以被实例化.使用和丢弃,一旦创建了 SqlSessionFactory,就不再需要它了.因此 SqlSessionFactoryBuilder ...

  7. 【BZOJ2724】蒲公英(分块)

    [BZOJ2724]蒲公英(分块) 题面 洛谷 谴责权限题的行为 题解 分块什么的都不会,根本就没写过几次. 复杂度根本不会分析,吓得我赶快来练练. 这题要求的是区间众数,显然没有什么很好的主席树之类 ...

  8. Timus 1005 解题报告

    题目链接 http://acm.timus.ru/problem.aspx?space=1&num=1005 题目大意 给你一堆石头,现在需要你将这堆石头分成两堆,要求两堆石头的重量相差最小, ...

  9. Miiler-Robin素数测试与Pollard-Rho大数分解法

    板题 Miiler-Robin素数测试 目前已知分解质因数以及检测质数确定性方法就只能\(sqrt{n}\)试除 但是我们可以基于大量测试的随机算法而有大把握说明一个数是质数 Miler-Robin素 ...

  10. 洛谷 P1446 [HNOI2008]Cards 解题报告

    P1446 [HNOI2008]Cards 题目描述 小春现在很清闲,面对书桌上的\(N\)张牌,他决定给每张染色,目前小春只有\(3\)种颜色:红色,蓝色,绿色.他询问Sun有多少种染色方案,Sun ...