题目描述

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.

Samples
Input Copy
1
7 7
1 2 3 4 5 6 7
1 4
1 5
4 5
2 3
2 6
3 6
2 7
Output
21
Source
2015 ACM/ICPC Asia Regional Changchun Online

题意:

n点m边无向图,问将度数<2的点删除后,剩余的由奇数个点构成的连通块的权值之和。

思路:

拓扑排序删点,然后DFS。

代码:

#pragma GCC optimize(3)
#pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline")
#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll>PLL;
typedef pair<int,int>PII;
typedef pair<double,double>PDD;
#define I_int ll
inline ll read()
{ll x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;
}
char F[200];
inline void out(I_int x) {if (x == 0) return (void) (putchar('0'));I_int tmp = x > 0 ? x : -x;if (x < 0) putchar('-');int cnt = 0;while (tmp > 0) {F[cnt++] = tmp % 10 + '0';tmp /= 10;}while (cnt > 0) putchar(F[--cnt]);//cout<<" ";
}
ll ksm(ll a,ll b,ll p){ll res=1;while(b){if(b&1)res=res*a%p;a=a*a%p;b>>=1;}return res;}
const int inf=0x3f3f3f3f,mod=1e9+7;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int maxn=1e5+7,maxm=3e5+7,N=1e6+7;
const double PI = atan(1.0)*4;int h[N],e[N],ne[N],idx,ans=N,n,m;
int d[N];
int val[maxn];
queue<int>q;
bool vis[maxn];void init(){idx=0;memset(h,-1,sizeof h);memset(vis,0,sizeof vis);memset(d,0,sizeof d);
}void add(int a,int b){e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}void topsort(){for(int i=1;i<=n;i++)if(d[i]<2) q.push(i),vis[i]=1;while(!q.empty()){int t=q.front();q.pop();for(int i=h[t];~i;i=ne[i]){int j=e[i];if(vis[j]) continue;if(--d[j]<2) q.push(j),vis[j]=1;}}
}ll siz=0,sum=0;void dfs(int u){siz++;vis[u]=1;sum+=val[u];for(int i=h[u];~i;i=ne[i]){int j=e[i];if(vis[j]) continue;dfs(j);}
}int main(){int T=read();while(T--){init();n=read(),m=read();for(int i=1;i<=n;i++) val[i]=read();while(m--){int u=read(),v=read();add(u,v);add(v,u);d[u]++;d[v]++;}topsort();ll res=0;siz=sum=0;for(int i=1;i<=n;i++)if(!vis[i]){siz=sum=0;dfs(i);if(siz%2) res+=sum;}out(res);puts("");while(!q.empty()) q.pop();}return 0;
}

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

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

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

  2. hdu5441(2015长春网络赛E题)

    题意: 给出一个n个点.m条边的无向图,边上有权值,有q组询问,每组询问给出一个数字x,我们要在图中找出'点对'的个数,这些'点对'(例如a,b)满足从a到b有一条路径经过的每一条边都要小于x,输出每 ...

  3. hdu5442(2015长春网络赛F题)

    题意: 给出一个字符串,只由'a'~'z'组成,字符串是一个首尾相接的串.我们要找到一个起点,顺时针或者逆时针的读这个串,找到字典序最大的读法,如果有多种,输出起点坐标小的那个,如果起点坐标一样,输出 ...

  4. hdu5446(2015长春网络赛J题)

    题意: 求C(n,m)%(p1*p2*......pk),其中,p1*p2*......pk都是素数. 思路: 不会...数论是渣,赛后知道是Lucas定理+中国剩余定理. 代码: #include& ...

  5. hdu5444(2015长春网络赛H题)

    题意: 给出一棵树的描述,这棵树构造出来,满足从右到左数值递增,根在最下面,然后有一些询问x,我们要输出从根走到x的路径,w:向左.e:向右. 思路: 建树的时候,首先第一个点一定是根,然后比根小的建 ...

  6. hdu5443(2015长春网络赛G题)

    题意: 一个数列,求从L到R的最大值. 思路: 不多说... 代码: #include<cstdio> #include<cstring>int a[20000];int ma ...

  7. hdu5437(2015长春网络赛A题)

    题意: 有一个party,会有n个人来,每个人都带着礼物来,礼物有权值,由于屋子的大小有限,所以他会选择k个时间来开门,在t时间让p个人进来,接下来有q组询问,每组询问有一个数字ni,让你输出第ni个 ...

  8. hdu 5439 Ponds(长春网络赛——拓扑排序+搜索)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5438 Ponds Time Limit: 1500/1000 MS (Java/Others)     ...

  9. hdu 5438 Ponds 拓扑排序

    Ponds Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/contests/contest_showproblem ...

最新文章

  1. java 接口中 常量_讨论:Java 接口当中的 “常量接口”
  2. zabbix setup.php出错,apache配置zabbix下setup.php无法显示
  3. zbb20171108 tomcat 性能优化
  4. axure web组件_AXURE原型设计:移动端选择器的应用
  5. hdu4956 Poor Hanamichi
  6. 将不确定变为确定~一切归总为“二”(C#中的位运算有啥用)
  7. pdf 深入理解kotlin协程_协程初探
  8. 在RHEL4.0下面安装oracle10g数据库
  9. Atitit 中间件之道 attilax著 1. 第1章 中间件产生背景及分布式计算环境 2 2. 中间件分类 3 2.1. 商业中间件:weblogic,was,conherence 开源中间
  10. 简易数字时钟 按键可校准
  11. 最新详细VMware虚拟机下载与安装
  12. 程序人生-我已经努力了七年!!!
  13. Android Execution failed for task ‘:app:mergeDebugResources‘.
  14. Unity - 搬砖日志 - 获取 LODGroup 当前显示的 LOD 级别
  15. android sqlite加密数据库,Android Sqlite数据库加密
  16. Odoo 14 手册 采购订单 采购招标 代发货 供应商管理 对账
  17. 桥梁防撞智能预警系统方案
  18. 基于SpringBoot旅游信息管理系统
  19. vue 绘制体温单与三测单组件 实现前端js打印
  20. 网络经济与企业管理【十】之供应链管理

热门文章

  1. 2013年03月26日
  2. 【定位】无线电测向相关知识(1)
  3. Gdevops峰会:一起探讨国产分布式数据库的选型与应用
  4. XMUOJ·小H的超大背包
  5. linux外挂存储不同,51CTO博客-专业IT技术博客创作平台-技术成就梦想
  6. rust进水器怎么用_易水香教你如何正确使用家用净水器
  7. php 奥点云接口,奥点云-使用方式说明
  8. 浅谈indexedDB/indexedDB数据实际储存的位置
  9. ubuntu完美安装espeak支持中文和粤语 不再报错:Full dictionary is not installed for 'zh'
  10. js室内地图开发_使用JS+Three.js+Echart开发商场室内地图客流信息统计功能