题目链接: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): 2837    Accepted Submission(s): 891
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
2015 ACM/ICPC Asia Regional Changchun Online
Recommend
hujie   |   We have carefully selected several similar problems for you:   5659  5658  5657  5656  5655 

题目大意:给出一个无向图,每一个节点都有一定的权值,最后要求输出每一个奇数圈内的权值和。

解题思路:首先 你要考虑怎么,保证它是一个圈。这样就想到采用拓扑排序的方法,去掉所有入度<2的点。再就是判断这个圈是否含有奇数个点,采用深搜的方法将每个点做一次起始点,对其进行搜索计数!

详见代码。

#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>using namespace std;#define N 10010
#define ll long longll val[N],V;
int vis[N];//判断这个点有没有删掉
int Vis[N];//判断这个点有没有做为起点进行搜索过
int indir[N],k;
vector<int>G[N];void dfs(int st)
{k++;V+=val[st];for (int i=0; i<G[st].size(); i++){if (!Vis[G[st][i]]&&!vis[G[st][i]]){Vis[G[st][i]]=1;dfs(G[st][i]);}}
}int main()
{int t,a,b;scanf("%d",&t);while (t--){int n,m;memset(vis,0,sizeof(vis));memset(Vis,0,sizeof(Vis));memset(val,0,sizeof(val));memset(indir,0,sizeof(indir));scanf("%d%d",&n,&m);for (int i=1; i<=n; i++)G[i].clear();for (int i=1; i<=n; i++)scanf("%lld",&val[i]);for (int i=1; i<=m; i++){scanf("%d%d",&a,&b);G[a].push_back(b);G[b].push_back(a);indir[a]++;indir[b]++;}while (1){int i;for (i=1; i<=n; i++){if (indir[i]<2&&!vis[i]){vis[i]=1;for (int j=0; j<G[i].size(); j++){if (!vis[G[i][j]])indir[G[i][j]]--;}break;}}if (i>n)break;}ll ans=0;for (int i=1; i<=n; i++){if (!Vis[i]&&!vis[i]){k=0;V=0;Vis[i]=1;dfs(i);if (k%2==1)ans+=V;}}printf ("%lld\n",ans);}return 0;
}

hdu 5439 Ponds(长春网络赛——拓扑排序+搜索)相关推荐

  1. 图论——AOV网络及拓扑排序

    引入 有向无环图(DAG) 如果一个有向图不存在环,也就是任意结点都无法通过一些有向边回到自身,那么称这个有向图为有向无环图 AOV 网络 在有向图中,用顶点表示活动,用有向边<Vi,Vj> ...

  2. 图算法入门3:活动网络-AOV网络和拓扑排序

    AOV网络 通常一个工程可以分成若干个子工程,这些子工程被称为活动(activity),完成这些活动,整个工程就完成了.给一个简单的例子,如下图,大学专业课程存在依赖关系,对于一些课程必须选修其他课程 ...

  3. 2015长春网络赛 —— B. Ponds (拓扑排序删点+DFS)

    题目描述 Description Betty owns a lot of ponds, some of them are connected with other ponds by pipes, an ...

  4. HDU 3342 Legal or Not(拓扑排序判断成环)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3342 题目大意:n个点,m条有向边,让你判断是否有环. 解题思路:裸题,用dfs版的拓扑排序直接套用即 ...

  5. [ACM] hdu 1285 确定比赛名次 (拓扑排序)

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

  6. AOV网络、拓扑排序、拓扑序列

    AOV网络 AOV网是有向图的一类应用,在AOV网中,用顶点表示某个有一定规模的"工程"里的不同活动,用图中的边表示各项活动之间的先后顺序关系.一种常见的AOV网实例是大学课程的先 ...

  7. HDU 1285 - 确定比赛名次(拓扑排序)

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

  8. hdu5438(2015长春网络赛B题)

    题意: 有n个池塘.m个管道,每个池塘有权值,m个管道用来连接池塘(无向边),我们要把度小于2的池塘删除,统计每个包含池塘数是奇数的连通分量的权值并输出. 思路: 拓扑排序之后,删点,然后dfs一遍就 ...

  9. HDU 3342 Legal or Not【拓扑排序】

    题意: 给出一些人的名次关系,问存不存在冲突的情况. 分析: 这题其实就是判断拓扑排序的过程中是否会出现环,判断环的方法是: 在某一次排序的过程中找不到入度为 0 的点,即说明有环. PS:  重边 ...

最新文章

  1. MySQL数据库实用教程考核_《MySQL数据库实用教程》郑明秋,蒙连超,赵海侠【pdf】...
  2. 73岁Hinton老爷子构思下一代神经网络:属于无监督对比学习
  3. unity拖拽 到固定位置_Unity3D拖动任意对象GameObject移动到任意地方
  4. boost::mp11::mp_max_element_q相关用法的测试程序
  5. Flex与.NET互操作(五):使用FileReference+HttpHandler实现文件上传/下载
  6. @Value 注入静态变量
  7. JavaFX技巧18:路径剪切
  8. MariaDB:在Linux下修改编码
  9. 1 CentOS下磁盘LVM扩容
  10. CAnimation-模拟时钟
  11. 自组织映射网络SOMnet
  12. 黑客X档案0911期PDF电子书
  13. 开关电源matlab仿真,用数学方法建立一种开关电源全系统的仿真模型
  14. 【全志A64/R18】Android平台获取Chip ID
  15. 利用python转载朋友微信表情包
  16. pci规划的三个原则_LTE中PCI规划目的和原则
  17. 核函数是什么-数据的升维与降维
  18. python数据采集仪_数据采集卡支持python么深入理解Python生成器(Generator)
  19. openstack上传镜像
  20. Java方法创建及调用--------06

热门文章

  1. sality感染型病毒
  2. 这可能是由于CredSSP加密数据库修正
  3. ActiveSync 3.7
  4. 快递员拍身份证照片,谁来保证信息安全?
  5. 深入Java虚拟机之 --- JVM的爱恨情仇
  6. 关闭win8安全中心服务器,Win8系统启动安全中心服务的办法
  7. WebSocket聊天室业务场景分析
  8. Supplier应用场景
  9. android机器人飞机游戏,机器人飞行模拟器
  10. linux ubuntu 22.04双屏显示颜色变绿问题解决