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到死…… 题意: 题面上告诉了我们这是一棵二叉树,然后告诉了我们它的先序遍历,然后,没了……没了! 反复读题,终于在偶然间 ...
随机推荐
- Django 的 之 视图
Django的View(视图) 一个视图函数(类),简称视图, 是个简单的python函数(类),它接受wed请求并且返回web 响应. 响应可以是一张网页的HTML内容,一个重定向,一个404错误, ...
- UVaLive 3266 Tian Ji -- The Horse Racing (贪心)
题意:田忌赛马,每胜一局就得200,负一局少200,问最多得多少钱. 析:贪心,如果最快的马比齐王的还快,就干掉它,如果最慢的马比齐王的马快,就干掉它,否则用最慢的马去和齐王最快的马比. 代码如下: ...
- HDU - 4821 String(窗口移动+map去重+hash优化)
String Given a string S and two integers L and M, we consider a substring of S as “recoverable” if a ...
- ACM-ICPC2018北京网络赛 80 Days(双端队列+尺取)
题目4 : 80 Days 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 80 Days is an interesting game based on Jules Ve ...
- MVC笔记-模板页布局
MVC分区后 要加上Ares下的主目录 如:@Html.ActionLink("这个帖子真奇葩", "HotIndex", "Prosceniu ...
- JS 检查是否在微信浏览器
/** * 检查是否在微信浏览器 * zengkai */ function isWeixinBrowser(){ var ua = navigator.userAgent.toLowerCase() ...
- CentOS6.5安装sqlite3[转]
1.下载安装包:https://www.sqlite.org/download.html 2.解压 [root@mycentos ~]# tar xzvf sqlite-snapshot-201809 ...
- JS中的MOD运算
最近研究汉诺塔非递归的时候,看到书上写了个MOD,久违啊,感觉好久没看到过了,都忘了怎么用了. 某人:我知道,这不就是取余嘛,直接%就行了. 嗯......,如果是python语言,你说的很对,但是我 ...
- TensorFlow高层封装:从入门到喷这本书
目录 TensorFlow高层封装:从入门到喷这本书 0. 写在前面 1. TensorFlow高层封装总览 2. Keras介绍 2.1 Keras基本用法 2.2 Keras高级用法 3. Est ...
- excel 公式2列合并
=A2&"="&C2 ="UPDATE comm_department SET parent_id='"&D2&"' ...