bzoj3252: 攻略 优先队列 并查集 贪心
考场上自己yy出来的做法.....
Code:
#include<cstdio>
#include<algorithm>
#include<queue>
#include<vector>
#include<string>
using namespace std;
void setIO(string a){
freopen((a+".in").c_str(),"r",stdin);
freopen((a+".out").c_str(),"w",stdout);
}
void shutIO(){
fclose(stdin);
fclose(stdout);
} #define ll long long
#define maxn 200009
struct Node{
ll dist;
int u;
Node(ll dist=0,int u=0):dist(dist),u(u){}
bool operator<(Node a) const{
return a.dist>dist;
}
};
priority_queue<Node>Q;
int p[maxn], tag[maxn],n,m,k,cnt,val[maxn],head[maxn],to[maxn<<1],nex[maxn<<1];
ll sumv[maxn];
int find(int x){
return p[x]==x?x:p[x]=find(p[x]);
}
void merge(int a,int b){
int x=find(a),y=find(b);
if(x==y) return;
p[x]=y;
}
void addedge(int u,int v){
nex[++cnt]=head[u],head[u]=cnt,to[cnt]=v;
}
bool check(int a){
int x=find(a);
if(tag[x]) return false;
tag[x]=1;
return true;
}
void dfs(int u,int fa){
sumv[u]=(ll)val[u];
int flag=0;
ll MAX=0;
for(int v=head[u];v;v=nex[v]){
if(to[v]==fa) continue;
dfs(to[v],u);
if(sumv[to[v]]>MAX) MAX=sumv[to[v]], flag=to[v];
}
if(flag){
sumv[u]+=MAX, merge(u,flag);
for(int v=head[u];v;v=nex[v]){
if(to[v]==fa) continue;
if(to[v]!=flag) Q.push(Node(sumv[to[v]],to[v]));
}
}
}
int main(){
//setIO("game");
scanf("%d%d",&n,&k);
for(int i=1;i<=n;++i) scanf("%d",&val[i]);
for(int i=1;i<=n;++i) p[i]=i;
for(int i=1;i<n;++i){
int a,b;
scanf("%d%d",&a,&b);
addedge(a,b),addedge(b,a);
}
dfs(1,0);
Q.push(Node(sumv[1],1));
int cur=0;
ll fin=0;
while(!Q.empty()&&cur<k){
Node a=Q.top();Q.pop();
if(check(a.u))fin+=a.dist,cur+=1;
}
printf("%lld",fin);
//shutIO();
return 0;
}
bzoj3252: 攻略 优先队列 并查集 贪心的更多相关文章
- 219.01.19 bzoj3252: 攻略(长链剖分+贪心)
传送门 长链剖分好题. 题意:给一棵带点权的树,可以从根节点到任一叶节点走kkk次,走过的点只能计算一次,问kkk次走过的点点权值和最大值. 思路: 考虑将整棵树带权长链剖分,这样链与链之间是不会重复 ...
- HDU 1598 find the most comfortable road 并查集+贪心
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1598 find the most comfortable road Time Limit: 1000 ...
- [POJ2054]Color a Tree (并查集+贪心)
POJ终于修好啦 题意 和UVA1205是同一题,在洛谷上是紫题 有一棵树,需要给其所有节点染色,每个点染色所需的时间是一样的都是11.给每个点染色,还有一个开销“当前时间×ci×ci”,cici是每 ...
- 【链接】LINUX SHELL脚本攻略笔记[速查]
LINUX SHELL脚本攻略笔记[速查]
- LINUX SHELL脚本攻略笔记[速查]
Linux Shell脚本攻略笔记[速查] 资源 shell script run shell script echo printf 环境变量和变量 pgrep shell数学运算 命令状态 文件描述 ...
- [bzoj3252]攻略_dfs序_线段树_贪心
攻略 bzoj-3252 题目大意:给定一棵n个节点的有根树,点有点权.让你选出至多k个节点,使得他们到根的链的并最大. 注释:$1\le n\le 2\cdot 10^5$,$1\le val_i\ ...
- [POJ1456]Supermarket(贪心 + 优先队列 || 并查集)
传送门 1.贪心 + 优先队列 按照时间排序从前往后 很简单不多说 ——代码 #include <queue> #include <cstdio> #include <i ...
- 【BZOJ-3252】攻略 DFS序 + 线段树 + 贪心
3252: 攻略 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 339 Solved: 130[Submit][Status][Discuss] D ...
- BZOJ3252攻略——长链剖分+贪心
题目描述 题目简述:树版[k取方格数] 众所周知,桂木桂马是攻略之神,开启攻略之神模式后,他可以同时攻略k部游戏.今天他得到了一款新游戏<XX 半岛>,这款游戏有n个场景(scene),某 ...
随机推荐
- 14.MongoDBUtils工具类
1. public class DbUtils { public static MongoCollection<Document> getMongoCollection(String lo ...
- [hihocoder #1384] Genius ACM 解题报告(倍增)
题目链接:http://hihocoder.com/problemset/problem/1384 题目大意: 给定一个整数 M,对于任意一个整数集合 S,定义“校验值”如下: 从集合 S 中取出 M ...
- win-visualviewport-space
html.win-hoverable <div class="win-visualviewport-space"></div> <section cl ...
- iF.svnadmin 安装遇到的坑
iF.svnadmin 官网:http://svnadmin.insanefactory.com/ 安装配置iF.svnadmin : http://blog.linhere.com/archives ...
- 死锁,线程协作(同步,阻塞队列,Condition,管道流)
synchronized死锁 package com.thread.demo.deadlock; public class DeadLock { private static Object lock1 ...
- 路飞学城-Python开发-第三章
# 数据结构: # goods = [ # {"name": "电脑", "price": 1999}, # {"name&quo ...
- NodeJS学习笔记 (5)网络服务-http-req(ok)
原文:https://github.com/chyingp/nodejs-learning-guide 自己敲代码: 概览 本文的重点会放在req这个对象上.前面已经提到,它其实是http.Incom ...
- Python学习笔记-练习编写ATM+购物车(购物商城)
作业需求: 模拟实现一个ATM + 购物商城程序: 1.额度 15000或自定义 2.实现购物商城,买东西加入 购物车,调用信用卡接口结账 3.可以提现,手续费5% 4.支持多账户登录 5.支持账户间 ...
- 关于ValueAnimation以及Interpolator +Drawable实现的自己定义动画效果
ValueAnimation : Android中的属性动画,他跟objectAnimation是比补间动画拥有更强大的功能,能够操作对象.所以我们能够在自 定义View中通过他们来实现些特别的功能. ...
- cocos2d-iphone 动作
(1)CCMoveTo [CCMoveTo alloc]initWithDuration:<#(ccTime)#> position:<#(CGPoint)#> 參数说明 : ...