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到死…… 题意: 题面上告诉了我们这是一棵二叉树,然后告诉了我们它的先序遍历,然后,没了……没了! 反复读题,终于在偶然间 ...
随机推荐
- Zookeeper发布订阅之SpringBoot+Mybatis多数据源
1.前言 数据发布/订阅系统,即所谓的配置中心,顾名思义就是发布者将数据发布到Zookeeper的一个或一系列节点上,供订阅者进行数据订阅,进而达到动态获取数据的目的,实现配置信息的集中管理和数据的动 ...
- C# 测试代码#if DEBUG使用
代码示例: #if DEBUG Console.WriteLine("DEBUG:11111111111"); #else Console.WriteLine ...
- Docker Compose 多容器应用
Docker Compose是一个用来定义并运行复杂应用程序的工具.用Compose,你可以在一个文件中定义多个容器应用程序,然后只需一条命令就可以完成使其运行所需的所有操作. 一个使用Docker容 ...
- Python:关于subprocess.stdout.read()导致程序死锁的问题
subprocess.stdout.read()导致程序死锁的问题解决 今天有位老哥联系我说,在我的python之路系列中,解决粘包问题那一章的代码有BUG 这里当运行命令过于庞大的时候,会导致程序直 ...
- 容易忘记的css属性和动画属性
动画属性 @keyframes 关键帧 --> animation 活泼 (配合使用) transform 变换 --> transition 过渡 (配合使用) 1.animation ...
- python 之 软件开发目录规范 、logging模块
6.4 软件开发目录规范 软件(例如:ATM)目录应该包含: 文件名 存放 备注 bin start.py,用于起动程序 core src.py,程序核心功能代码 conf settings. ...
- vue 开发笔记
vue 开发记录 marked 插件的使用 import marked from "marked"; import hljs from "highlight.js&quo ...
- js json与字符串相互转换
JSON.parse(jsonstr); //可以将json字符串转换成json对象 JSON.stringify(jsonobj); //可以将json对象转换成json对符串
- 命令行 IRC 客户端 irssi 的基本操作
登录与退出 启动 irssi $ irssi 登陆 IRC 服务器/connect server port 修改昵称/nick nickname 直接带昵称登录指定的服务器$ irssi -c [se ...
- SpringBoot---Web开发---SSL配置
1.[生成证书] 2.[SpringBoot配置SSL] 3.[http转向https]