题目链接: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. HTML标签参考手册

    按字母顺序排列 New : HTML5 中的新标签. 标签 描述 <!--...--> 定义注释. <!DOCTYPE>  定义文档类型. <a> 定义锚. < ...

  2. Laravel 5.1 中的异常处理器和HTTP异常处理 abort()

    原文  http://laravelacademy.org/post/1867.html 错误和异常是处理程序开发中不可回避的议题,在本地开发中我们往往希望能捕获程序抛出的异常并将其显示打印出来,以便 ...

  3. <context-param> 标签引出的 web.xml 文件的加载顺序 [转]

    代码示例 : <context-param> <param-name>contextConfigLocation</param-name> <param-va ...

  4. 80X86计算机组织

    计算机主要由运算器.控制器.存储器.和输入输出设备构成. 主频: 主频是指芯片所用的主时钟频率,它直接影响计算机的运行速度,由于处理器体系结构的差别,同样的主频可能产生不同的计算速度,但主频仍然是反映 ...

  5. Crowd Control(输出不在最大值最小化的最短路上的边)

    题意: 就是求完最大值最小化  然后输出在这条最大值最小化的最短路上的点的不在最短路上的边,emm.... 解析: 很明显,先套spfa最大值最小化模板,emm... 在更新d的时候 用一个pre去记 ...

  6. iPhone X 的原深感模组

    物理与数字世界正走向融合,我们每天醒来的时间.睡眠时长.心率和步数等数据都会被分享.上传并转化为分析数据.无处不自的 AI.互联互通和软件平台将改变用户对现实的感知. 2018 年的 CES 展(国际 ...

  7. 【Cf #502 H】The Films(莫队)

    题面的简述:总共有$m$种书,书架上共有$n$本书,给出$n$本书的种类,并有$Q$个询问,每次询问给出$l, r, k$.每次询问时都会先出现$k * m$本书,每种书各$k$本,然后再加入书架上的 ...

  8. 【BZOJ3309】DZY Loves Math 解题报告

    [BZOJ3309]DZY Loves Math Description 对于正整数\(n\),定义\(f(n)\)为\(n\)所含质因子的最大幂指数.例如\(f(1960)=f(2^3×5^1×7^ ...

  9. 解题:洛谷2633 Count on a tree

    题面 在树上建主席树...... 每个点从父亲那里建过来,最后建出来就是从根到$i$这条链上的主席树,查询的时候一边差分一边查询 ($cmt[u]+cmt[v]-cmt[lca(u,v)]-cmt[a ...

  10. golang简单实现二叉树的数据添加和遍历

    代码实现 package tree import "fmt" type Node struct { elem interface{} left, right *Node } typ ...