2015 ACM/ICPC Asia Regional Changchun Online Pro 1002 Ponds(拓扑排序+并查集)
Ponds
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 0 Accepted Submission(s): 0
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
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.
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.
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e4+,maxm = 2e5+;
int n,m;
int val[maxn];
int head[maxn],nxt[maxm],to[maxm];
int deg[maxn],ecnt;
bool rmvd[maxn]; void addEdge(int u,int v)
{
to[ecnt] = v;
nxt[ecnt] = head[u];
head[u] = ecnt++;
deg[u]++;
} void topo()
{
queue<int> q;
for(int i = ; i <= n; i++){
if(deg[i] <= ){
rmvd[i] = true;
q.push(i);
}
}
while(q.size()){
int u = q.front(); q.pop();
for(int i = head[u]; ~i; i = nxt[i]){
int v = to[i];
if(!rmvd[v] && --deg[v] == ){
q.push(v); rmvd[v] = true;
}
}
}
}
long long sum[maxn];
int pa[maxn],cnt[maxn];
int fdst(int x) { return x==pa[x]?x:pa[x]=fdst(pa[x]); } int main()
{
//freopen("in.txt","r",stdin);
int T; scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
for(int i = ; i <= n; i++) scanf("%d",val+i);
memset(head,-,sizeof(head));
memset(deg,,sizeof(deg));
memset(rmvd,,sizeof(rmvd));
ecnt = ;
for(int i = ; i < m; i++){
int u,v; scanf("%d%d",&u,&v);
addEdge(u,v); addEdge(v,u);
}
topo();
for(int i = ; i <= n; i++) pa[i] = i,sum[i] = val[i],cnt[i] = ;
for(int i = ,M = *m; i < M; i += ){
int u = to[i], v = to[i^];
if(!rmvd[u] && !rmvd[v]){
int a = fdst(u), b = fdst(v);
if(a != b){
pa[a] = b;
sum[b] += sum[a];
cnt[b] += cnt[a];
}
}
}
long long ans = ;
for(int i = ; i <= n ;i++){
int f = fdst(i);
if(!rmvd[f]){
if(cnt[f]&){
ans += sum[f];
}
rmvd[f] = true;
}
}
printf("%I64d\n",ans);
}
return ;
}
2015 ACM/ICPC Asia Regional Changchun Online Pro 1002 Ponds(拓扑排序+并查集)的更多相关文章
- 2015 ACM/ICPC Asia Regional Changchun Online Pro 1008 Elven Postman (BIT,dfs)
Elven Postman Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)T ...
- 2015 ACM/ICPC Asia Regional Changchun Online Pro 1005 Travel (Krsukal变形)
Travel Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Su ...
- hdu 5444 Elven Postman(二叉树)——2015 ACM/ICPC Asia Regional Changchun Online
Problem Description Elves are very peculiar creatures. As we all know, they can live for a very long ...
- (并查集)Travel -- hdu -- 5441(2015 ACM/ICPC Asia Regional Changchun Online )
http://acm.hdu.edu.cn/showproblem.php?pid=5441 Travel Time Limit: 1500/1000 MS (Java/Others) Memo ...
- (二叉树)Elven Postman -- HDU -- 54444(2015 ACM/ICPC Asia Regional Changchun Online)
http://acm.hdu.edu.cn/showproblem.php?pid=5444 Elven Postman Time Limit: 1500/1000 MS (Java/Others) ...
- 2015 ACM/ICPC Asia Regional Changchun Online HDU 5444 Elven Postman【二叉排序树的建树和遍历查找】
Elven Postman Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)T ...
- 2015 ACM/ICPC Asia Regional Changchun Online HDU - 5441 (离线+并查集)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5441 题意:给你n,m,k,代表n个城市,m条边,k次查询,每次查询输入一个x,然后让你一个城市对(u,v ...
- (线段树 区间查询)The Water Problem -- hdu -- 5443 (2015 ACM/ICPC Asia Regional Changchun Online)
链接: http://acm.hdu.edu.cn/showproblem.php?pid=5443 The Water Problem Time Limit: 1500/1000 MS (Java/ ...
- hdu 5444 Elven Postman(根据先序遍历和中序遍历求后序遍历)2015 ACM/ICPC Asia Regional Changchun Online
很坑的一道题,读了半天才读懂题,手忙脚乱的写完(套上模板+修改模板),然后RE到死…… 题意: 题面上告诉了我们这是一棵二叉树,然后告诉了我们它的先序遍历,然后,没了……没了! 反复读题,终于在偶然间 ...
随机推荐
- ubuntu系统开root以及(su:认证失败)完美解决
开机进入桌面,ctrl+alt+T打开终端————在此时终端显示的是 用户名@电脑名:-$ 表示普通用户 在此处输入:sudo passwd root 此时提示———— [sudo] passwo ...
- Image Processing - Pseudo(False) Color Processing
最近在一个项目中有需要用到将图片从GrayScale转为FalseColor,然而百度了一下Halcon 伪彩色等关键字均找不到相关答案,倒是有很多OpenCV和Matlab的...后来在搜索中看到了 ...
- Fedora/CentOS使用技巧
命令 获取系统安装包的编译源码及脚本 # dnf download --source package # yumdownloader --source virt-viewer 远程连接windows ...
- offsetLeft在各浏览器的值
上网找了好久没有找到一个offsetLeft在各浏览器的值,自己用了一晚上的时间在各浏览器测试出来的offsetLeft的值. <!DOCTYPE html> <html lang= ...
- 深入Java集合学习系列:HashSet的实现原理
1. HashSet概述: HashSet实现Set接口,由哈希表(实际上是一个HashMap实例)支持.它不保证set 的迭代顺序:特别是它不保证该顺序恒久不变.此类允许使用null元素. 2. H ...
- Python数据科学手册Seaborn马拉松可视化里时分秒转化为秒数的问题
Python数据科学手册Seaborn马拉松可视化里时分秒转化为秒数的问题 问题描述: 我实在是太懒了,问题描述抄的网上的哈哈哈:https://www.jianshu.com/p/6ab7afa05 ...
- github最火的springboot开源学习资料
https://github.com/JeffLi1993/springboot-learning-example https://github.com/ityouknow/spring-boot-e ...
- [TCP/IP]IP协议
IP数据报 IP是TCP/IP协议族中最核心的协议,所有的TCP.UDP.ICMP.IGMP数据都以IP数据报的格式传输.IP仅提供尽力而为的传输服务,如果发生某种错误,IP会丢失该数据,然后发送IC ...
- 51nod1244 欧拉函数之和 杜教筛
和上一题差不多,一个是μ*I=e,一个是φ*I=Id 稍改就得到了这题的代码 (我会告诉你我一开始逆元算错了吗) #include <bits/stdc++.h> #define MAX ...
- JavaScript引擎基本原理: 优化prototypes
原文链接: JavaScript engine fundamentals: optimizing prototypes 这篇文章介绍了一些JavaScript引擎常用的优化关键点, 并不只是Bened ...