CF980E The Number Games

题意翻译

Panel 国将举办名为数字游戏的年度表演。每个省派出一名选手。

国家有 n 个编号从 1 到 n 的省,每个省刚好有一条路径将其与其他省相连。第 i 个省出来的代表有 2i 名粉丝。

今年,主席打算削减开支,他想要踢掉 k 个选手。但是,被踢掉的选手的省将很 angry 并且不会让别的任何人从这个省经过。

主席想确保所有剩下选手的省都互相可达,他也希望最大化参与表演的选手的粉丝数。

主席该踢掉哪些选手呢?

输入格式

输入n,k 。( k<n≤106 )

下来是 n−1 行,一行两个数,代表一条道路的起终点。

输出格式

升序输出要踢掉的选手编号。

感谢@poorpool 提供的翻译

题目描述

The nation of Panel holds an annual show called The Number Games, where each district in the nation will be represented by one contestant.

The nation has nn districts numbered from 11 to nn , each district has exactly one path connecting it to every other district. The number of fans of a contestant from district ii is equal to 2^i2i .

This year, the president decided to reduce the costs. He wants to remove kk contestants from the games. However, the districts of the removed contestants will be furious and will not allow anyone to cross through their districts.

The president wants to ensure that all remaining contestants are from districts that can be reached from one another. He also wishes to maximize the total number of fans of the participating contestants.

Which contestants should the president remove?

输入输出格式

输入格式:

The first line of input contains two integers n and k ( 1≤k<n≤106 ) — the number of districts in Panel, and the number of contestants the president wishes to remove, respectively.

The next n−1 lines each contains two integers aa and b ( 1≤a,b≤n , a≠b ), that describe a road that connects two different districts a and b in the nation. It is guaranteed that there is exactly one path between every two districts.

输出格式:

Print k space-separated integers: the numbers of the districts of which the contestants should be removed, in increasing order of district number.

输入输出样例

输入样例#1: 复制

6 3
2 1
2 6
4 2
5 6
2 3
输出样例#1: 复制

1 3 4
输入样例#2: 复制

8 4
2 6
2 7
7 8
1 2
3 1
2 4
7 5
输出样例#2: 复制

1 3 4 5

说明

In the first sample, the maximum possible total number of fans is 2^2 + 2^5 + 2^6 = 100 . We can achieve it by removing the contestants of the districts 1, 3, and 4.


Solution

哭,写完才发现为什么别人的代码那么短QAQ

暴拍了个树剖+线段树修改QAQ

思路也就是贪心了,把$n$定为根节点,从大往小选,要选它选意味着这个点到根节点的点全都要选,所以每次计算这条路上可以选的数量,能选就选,然后修改线段树,最后查询线段树上没有被修改的就行了。

Code

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std; int n, k; struct Node {
int u, v, nex;
Node(int u = , int v = , int nex = ) :
u(u), v(v), nex(nex) { }
} Edge[]; int h[], stot;
void add(int u, int v) {
Edge[++stot] = Node(u, v, h[u]);
h[u] = stot;
} int fa[], siz[], son[];
void dfs1(int u, int f) {
fa[u] = f; siz[u] = ;
for(int i = h[u]; i; i = Edge[i].nex) {
int v = Edge[i].v;
if(v == f) continue;
dfs1(v, u);
siz[u] += siz[v];
if(siz[v] > siz[son[u]]) son[u] = v;
}
} int top[], in[], idc;
void dfs2(int u, int t) {
top[u] = t; in[u] = ++idc;
if(son[u]) dfs2(son[u], t);
for(int i = h[u]; i; i = Edge[i].nex) {
int v = Edge[i].v;
if(v == fa[u] || v == son[u]) continue;
dfs2(v, v);
}
} int TR[], tag[];
void update(int nd) {
TR[nd] = TR[nd << ] + TR[nd << | ];
} void push_down(int nd, int l, int r) {
if(~tag[nd]) {
int mid = (l + r) >> ;
TR[nd << ] = ;
TR[nd << | ] = ;
tag[nd << ] = ; tag[nd << | ] = ;
tag[nd] = -;
}
} void build(int nd, int l, int r) {
tag[nd] = -;
if(l == r) {
TR[nd] = ;
return ;
}
int mid = (l + r) >> ;
build(nd << , l, mid);
build(nd << | , mid + , r);
update(nd);
} int query(int nd, int l, int r, int L, int R) {
if(l >= L && r <= R) return TR[nd];
push_down(nd, l, r);
int ans = ; int mid = (l + r) >> ;
if(L <= mid) ans += query(nd << , l, mid, L, R);
if(R > mid) ans += query(nd << | , mid + , r, L, R);
return ans;
} void modify(int nd, int l, int r, int L, int R) {
if(l >= L && r <= R) {
TR[nd] = ;
tag[nd] = ;
return ;
}
push_down(nd, l, r);
int mid = (l + r) >> ;
if(L <= mid) modify(nd << , l, mid, L, R);
if(R > mid) modify(nd << | , mid + , r, L, R);
update(nd);
} int query(int u) {
int ans = ;
while(top[u] != ) {
ans += query(, , n, in[top[u]], in[u]);
u = fa[top[u]];
}
ans += query(, , n, in[n], in[u]);
return ans;
} void modify(int u) {
while(top[u] != ) {
modify(, , n, in[top[u]], in[u]);
u = fa[top[u]];
}
modify(, , n, in[n], in[u]);
} int use[];
int main() {
scanf("%d%d", &n, &k);
memset(use, , sizeof(use));
for(int i = ; i < n; i ++) {
int u, v;
scanf("%d%d", &u, &v);
add(u, v); add(v, u);
}
dfs1(n, ); dfs2(n, );
build(, , n);
int tot = n - k;
for(int i = n; i >= && tot; i --) {
int tmp = query(i);
if(tmp <= tot) {
tot -= tmp;
modify(i);
}
}
for(int i = ; i <= n; i ++)
if(query(, , n, in[i], in[i])) printf("%d ", i);
return ;
}

CF980E The Number Games【树链剖分/线段树】的更多相关文章

  1. Aizu 2450 Do use segment tree 树链剖分+线段树

    Do use segment tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.bnuoj.com/v3/problem_show ...

  2. 【POJ3237】Tree(树链剖分+线段树)

    Description You are given a tree with N nodes. The tree’s nodes are numbered 1 through N and its edg ...

  3. Spoj Query on a tree SPOJ - QTREE(树链剖分+线段树)

    You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, ...

  4. 【BZOJ-2325】道馆之战 树链剖分 + 线段树

    2325: [ZJOI2011]道馆之战 Time Limit: 40 Sec  Memory Limit: 256 MBSubmit: 1153  Solved: 421[Submit][Statu ...

  5. 【BZOJ2243】[SDOI2011]染色 树链剖分+线段树

    [BZOJ2243][SDOI2011]染色 Description 给定一棵有n个节点的无根树和m个操作,操作有2类: 1.将节点a到节点b路径上所有点都染成颜色c: 2.询问节点a到节点b路径上的 ...

  6. BZOJ2243 (树链剖分+线段树)

    Problem 染色(BZOJ2243) 题目大意 给定一颗树,每个节点上有一种颜色. 要求支持两种操作: 操作1:将a->b上所有点染成一种颜色. 操作2:询问a->b上的颜色段数量. ...

  7. POJ3237 (树链剖分+线段树)

    Problem Tree (POJ3237) 题目大意 给定一颗树,有边权. 要求支持三种操作: 操作一:更改某条边的权值. 操作二:将某条路径上的边权取反. 操作三:询问某条路径上的最大权值. 解题 ...

  8. bzoj4034 (树链剖分+线段树)

    Problem T2 (bzoj4034 HAOI2015) 题目大意 给定一颗树,1为根节点,要求支持三种操作. 操作 1 :把某个节点 x 的点权增加 a . 操作 2 :把某个节点 x 为根的子 ...

  9. HDU4897 (树链剖分+线段树)

    Problem Little Devil I (HDU4897) 题目大意 给定一棵树,每条边的颜色为黑或白,起始时均为白. 支持3种操作: 操作1:将a->b的路径中的所有边的颜色翻转. 操作 ...

  10. HDU 2460 Network(双连通+树链剖分+线段树)

    HDU 2460 Network 题目链接 题意:给定一个无向图,问每次增加一条边,问个图中还剩多少桥 思路:先双连通缩点,然后形成一棵树,每次增加一条边,相当于询问这两点路径上有多少条边,这个用树链 ...

随机推荐

  1. 20165230 2017-2018-2 《Java程序设计》第7周学习总结

    20165230 2017-2018-2 <Java程序设计>第7周学习总结 教材学习内容总结 第十一章 JDBC与MySQL数据库 本周了解了如何在Java程序中使用JDBC语提供的AP ...

  2. Spring入门实例

    Spring 是一个开源框架,是为了解决企业应用程序开发复杂性而创建的.框架的主要优势之一就是其分层架构,分层架构允许您选择使用哪一个组件,同时为 J2EE 应用程序开发提供集成的框架. 控制反转:应 ...

  3. 数据结构之队列(Python 版)

    数据结构之队列(Python 版) 队列的特点:先进先出(FIFO) 使用链表技术实现 使用单链表技术,在表首尾两端分别加入指针,就很容易实现队列类. 使用顺序表list实现 # 队列类的实现 cla ...

  4. (一)问候 HttpClient

    第一节: HttpClient 简介 HttpClient 是 Apache Jakarta Common 下的子项目,可以用来提供高效的.最新的.功能丰富的支持 HTTP 协议的客户端编程工具包,并 ...

  5. Effective STL 笔记 -- Item 9: Choose carefully among erasing options

    假设有一个容器中存放着 int ,Container<int> c, 现在想从其中删除数值 1963,可以有如下方法: 1: c.erase(remove(c.begin(), c.end ...

  6. CVE-2011-0104 Microsoft Office Excel缓冲区溢出漏洞 分析

    漏洞简述   Microsoft Excel是Microsoft Office组件之一,是流行的电子表格处理软件.        Microsoft Excel中存在缓冲区溢出漏洞,远程攻击者可利用此 ...

  7. 批处理命令篇--配置免安装mysql

    免安装版的mysql是进行软件绿色发布的绝佳助手,本文介绍一种使用批处理命令自动配置mysql的方法. (1)建立三个文件,分别是:service install.bat,temp.txt,updat ...

  8. 使用mongo-java-driver-3.0.2连接MongoDB数据库

    这里使用的mongodb的java驱动版本是:3.0.2,文件名mongo-java-driver-3.0.2.jar  博客本地下载下载网址(也可以下载其它版本):http://central.ma ...

  9. 站点的安全防范都是后端的职责?非也,Web前端安全同样不可忽视

    前言 随着网络的快速普及,网络安全问题的受害者不再只是政府.企业等集体,每一个接触网络的普通人都有可能成为网络攻击的受害者.随着网络的普及,黑客进行网络攻击的手段越来也多,越来越复杂.以网站的攻击为例 ...

  10. 基于Bootstrap的Asp.net Mvc 分页的实现

    最近写了一个mvc 的 分页,样式是基于 bootstrap 的 ,提供查询条件,不过可以自己写样式根据个人的喜好,以此分享一下.首先新建一个Mvc 项目,既然是分页就需要一些数据,我这边是模拟了一些 ...