考场上自己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: 攻略 优先队列 并查集 贪心的更多相关文章

  1. 219.01.19 bzoj3252: 攻略(长链剖分+贪心)

    传送门 长链剖分好题. 题意:给一棵带点权的树,可以从根节点到任一叶节点走kkk次,走过的点只能计算一次,问kkk次走过的点点权值和最大值. 思路: 考虑将整棵树带权长链剖分,这样链与链之间是不会重复 ...

  2. HDU 1598 find the most comfortable road 并查集+贪心

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1598 find the most comfortable road Time Limit: 1000 ...

  3. [POJ2054]Color a Tree (并查集+贪心)

    POJ终于修好啦 题意 和UVA1205是同一题,在洛谷上是紫题 有一棵树,需要给其所有节点染色,每个点染色所需的时间是一样的都是11.给每个点染色,还有一个开销“当前时间×ci×ci”,cici是每 ...

  4. 【链接】LINUX SHELL脚本攻略笔记[速查]

    LINUX SHELL脚本攻略笔记[速查]

  5. LINUX SHELL脚本攻略笔记[速查]

    Linux Shell脚本攻略笔记[速查] 资源 shell script run shell script echo printf 环境变量和变量 pgrep shell数学运算 命令状态 文件描述 ...

  6. [bzoj3252]攻略_dfs序_线段树_贪心

    攻略 bzoj-3252 题目大意:给定一棵n个节点的有根树,点有点权.让你选出至多k个节点,使得他们到根的链的并最大. 注释:$1\le n\le 2\cdot 10^5$,$1\le val_i\ ...

  7. [POJ1456]Supermarket(贪心 + 优先队列 || 并查集)

    传送门 1.贪心 + 优先队列 按照时间排序从前往后 很简单不多说 ——代码 #include <queue> #include <cstdio> #include <i ...

  8. 【BZOJ-3252】攻略 DFS序 + 线段树 + 贪心

    3252: 攻略 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 339  Solved: 130[Submit][Status][Discuss] D ...

  9. BZOJ3252攻略——长链剖分+贪心

    题目描述 题目简述:树版[k取方格数] 众所周知,桂木桂马是攻略之神,开启攻略之神模式后,他可以同时攻略k部游戏.今天他得到了一款新游戏<XX 半岛>,这款游戏有n个场景(scene),某 ...

随机推荐

  1. 线上服务CPU100%问题快速定位实战--转

    来自微信公众号 架构师之路 功能问题,通过日志,单步调试相对比较好定位. 性能问题,例如线上服务器CPU100%,如何找到相关服务,如何定位问题代码,更考验技术人的功底. 58到家架构部,运维部,58 ...

  2. 为什么不建议用table进行布局

    本文部分内容转载自:http://www.html5tricks.com/why-not-table-layout.html 刚刚接触html的时候,利用table进行页面布局是比较容易的.但是,在实 ...

  3. Edge 浏览器

    Edge浏览器设计理念 无法播放:https://edgewelcomecdn.microsoft.com/site/images/tabs/rs3/tabs_screen.acd367a2.mp4 ...

  4. Java文件(io)编程——File类的基本用法

    1.首先了解文件流的相关概念: 2.文件File类的基本用法 public class Demo_1 { public static void main(String[] args) { //创建一个 ...

  5. eclipse01

    http://blog.csdn.net/luman1991/article/category/6354903

  6. 【codeforces 239B】Easy Tape Programming

    [题目链接]:http://codeforces.com/contest/239/problem/B [题意] 给你一个长度为n的字符串,只包括'<">'以及数字0到9; 给你q ...

  7. MySQL中锁详解(行锁、表锁、页锁、悲观锁、乐观锁等)

    原文地址:http://blog.csdn.net/mysteryhaohao/article/details/51669741 锁,在现实生活中是为我们想要隐藏于外界所使用的一种工具.在计算机中,是 ...

  8. A simpleHttp Proxy

    http://www.java2s.com/Code/Java/Network-Protocol/Asimpleproxyserver.htm

  9. How to customize Skin Gallery - Remove / rename skins and groups

    1. REMOVE (HIDE) A SPECIFIC SKIN Traverse through the gallery group collection, then through its gal ...

  10. [Mobx] Using mobx to isolate a React component state

    React is great for diffing between Virtual-DOM and rendering it to the dom. It also offers a naïve s ...