题意

给你一颗有根树,你要选择\(k\)个点,最大化\(\sum_{i \in S} val_i\),其中\(S\)是被选点的集合,\(val_i\)等于节点\(i\)到根的路径上未被选择点的个数。

解题思路

这题思路挺有意思的。

我们可以逐个点进行选择。新选择一个点对答案的贡献为点到跟的距离减去子树中被选择点的个数。

这样子的话,很容易可以得出,如果点\(u\)在答案中,那么它所有的后代也必须在答案中。

由此,我们可以为每个点赋一个权值\(v_i = d_i - sub_i\),\(d_i\)表示点\(i\)到根的距离,\(sub_i\)表示点\(i\)后代的个数。

AC代码

#include <bits/stdc++.h>
using namespace std; // #include <ext/rope>
// using namespace __gnu_cxx; // #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds; // typedef ll key_type;
// typedef null_mapped_type value_type;
// typedef tree<key_type, value_type, less<key_type>, rb_tree_tag, tree_order_statistics_node_update> rbtree; // typedef __gnu_pbds::priority_queue<pi,greater<pi>,pairing_heap_tag > heap; // mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// int rnd(int l,int r){return l+rng()%(r-l+1);} typedef long long ll;
typedef double db;
typedef pair<int,int> PI;
typedef vector<int> VI; #define rep(i,_,__) for (int i=_; i<=__; ++i)
#define per(i,_,__) for (int i=_; i>= __; --i) #define pb push_back
#define mp make_pair
#define fi first
#define se second
#define x1 _x
#define x2 __x
#define y1 _y
#define y2 __y
#define SZ(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define endl '\n' const double pi = acos(-1.0); namespace IO{
bool REOF = 1; //为0表示文件结尾
inline char nc() {
static char buf[1 << 20], *p1 = buf, *p2 = buf;
return p1 == p2 && REOF && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2) ? (REOF = 0, EOF) : *p1++;
} template<class T>
inline bool read(T &x) {
char c = nc();bool f = 0; x = 0;
while (c<'0' || c>'9')c == '-' && (f = 1), c = nc();
while (c >= '0'&&c <= '9')x = (x << 3) + (x << 1) + (c ^ 48), c = nc();
if(f)x=-x;
return REOF;
} template<class T>
inline bool write(T x){
if(x > 9) write(x / 10);
putchar('0'+x%10);
return REOF;
} template<typename T, typename... T2>
inline bool read(T &x, T2 &... rest) {
read(x);
return read(rest...);
} inline bool need(char &c) { return ((c >= 'a') && (c <= 'z')) || ((c >= '0') && (c <= '9')) || ((c >= 'A') && (c <= 'Z')); }
// inline bool need(char &c) { return ((c >= 'a') && (c <= 'z')) || ((c >= '0') && (c <= '9')) || ((c >= 'A') && (c <= 'Z')) || c==' '; } inline bool read_str(char *a) {
while ((*a = nc()) && need(*a) && REOF)++a; *a = '\0';
return REOF;
} inline bool read_db(double &x){
bool f = 0; char ch = nc(); x = 0;
while(ch<'0'||ch>'9') {f|=(ch=='-');ch=nc();}
while(ch>='0'&&ch<='9'){x=x*10.0+(ch^48);ch=nc();}
if(ch == '.') {
double tmp = 1; ch = nc();
while(ch>='0'&&ch<='9'){tmp=tmp/10.0;x=x+tmp*(ch^48);ch=nc();}
}
if(f)x=-x;
return REOF;
} template<class TH>
inline void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<'='<<h<<endl; } template<class TH, class... TA>
inline void _dbg(const char *sdbg, TH h, TA... a) {
while(*sdbg!=',')cerr<<*sdbg++;
cerr<<'='<<h<<','<<' '; _dbg(sdbg+1, a...);
} template<class T>
ostream &operator<<(ostream& os, vector<T> V) {
os << "[ "; for (auto vv : V) os << vv << ","; return os << " ]";
} template<class T>
ostream &operator<<(ostream& os, set<T> V) {
os << "[ "; for (auto vv : V) os << vv << ","; return os << " ]";
} template<class T>
ostream &operator<<(ostream& os, map<T,T> V) {
os << "[ "; for (auto vv : V) os << vv << ","; return os << " ]";
} template<class L, class R>
ostream &operator<<(ostream &os, pair<L,R> P) {
return os << "(" << P.x << "," << P.y << ")";
} #ifdef BACKLIGHT
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...)
#endif
} using namespace IO;
const int N = 2e5 + 5;
const int M = 2e5 + 5;
const int MAXV = 1e6 + 5;
const int MOD = 1e9+7; // 998244353 1e9+7
const int INF = 0x3f3f3f3f; // 1e9+7 0x3f3f3f3f
const ll LLINF = 0x3f3f3f3f3f3f3f3f; // 1e18+9 0x3f3f3f3f3f3f3f3f
const double eps = 1e-8; // int dx[4] = { 0, 1, 0, -1 };
// int dx[8] = { 1, 0, -1, 1, -1, 1, 0, -1 };
// int dy[4] = { 1, 0, -1, 0 };
// int dy[8] = { 1, 1, 1, 0, 0, -1, -1, -1 }; // ll qp(ll a, ll b) {
// ll res = 1;
// a %= mod;
// assert(b >= 0);
// while(b){
// if(b&1)
// res = res * a % mod;
// a = a * a % mod;
// b >>= 1;
// }
// return res;
// }
// ll inv(ll x) {return qp(x, mod - 2);}
// ll factor[N], finv[N];
// void init() {
// factor[0]=1;
// for(int i=1; i<N; i++) factor[i] = factor[i-1] * i % mod;
// finv[N-1] = qp(factor[N-1], mod - 2);
// for(int i=N-2; i>=0; i--) finv[i] = finv[i+1] * (i+1) % mod;
// }
// ll c(ll n, ll m) {
// return factor[n] * finv[m] % mod * finv[n-m] % mod;
// } // #define ls (x<<1)
// #define rs (x<<1|1)
// #define mid ((l+r)>>1)
// #define lson ls,l,mid
// #define rson rs,mid+1,r #define fore(_, __) for(int _ = head[__]; _; _=e[_].nxt)
int head[N], tot = 1;
struct Edge {
int v, nxt;
Edge(){}
Edge(int _v, int _nxt):v(_v), nxt(_nxt) {}
}e[N << 1];
void addedge(int u, int v) {
e[tot] = Edge(v, head[u]); head[u] = tot++;
e[tot] = Edge(u, head[v]); head[v] = tot++;
} /**
* ********** Backlight **********
* 仔细读题
* 注意边界条件
* 记得注释输入流重定向
* 没有思路就试试逆向思维
* 我不打了,能不能把我的分还给我
*/ int d[N], sz[N];
void dfs(int u, int f) {
d[u] = d[f] + 1;
sz[u] = 1;
fore(i, u) {
int v = e[i].v;
if(v==f)continue;
dfs(v, u);
sz[u] += sz[v];
}
} int n, k, a[N];
void solve(int Case) {
read(n, k);
int u, v;
rep(i, 1, n-1) {
read(u, v);
addedge(u, v);
} dfs(1, 0); rep(i, 1, n) a[i] = d[i] - sz[i];
sort(a+1, a+1+n);
ll ans = 0;
rep(i, 1, k) ans += (ll)a[n-i+1];
printf("%lld\n", ans);
} int main()
{
#ifdef BACKLIGHT
freopen("in.txt", "r", stdin);
#endif
// ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
// int _T; read(_T); for (int _ = 1; _ <= _T; _++) solve(_);
// int _T=1; while(read(n)) solve(_T), _T++;
solve(1);
return 0;
}

Codeforces 1337C Linova and Kingdom的更多相关文章

  1. Codeforces Round #635C Linova and Kingdom 思维

    Linova and Kingdom 题意 现在有一颗n个节点的树,每个节点是一个城市,现在要选出k个城市作为工业城市,其他城市作为旅游城市,现在每个工业城市要派出一名特使前往根节点,每个特使的幸福度 ...

  2. Codeforces Round #635 C. Linova and Kingdom

    传送门:C. Linova and Kingdom 题意:给你一棵树,要求对k个结点涂色,然后统计每个未涂色结点到根结点的路径上未涂色结点的和,求和最大能为多少 题解:对着样例画几遍,然后贪心发现,最 ...

  3. codeforces:Roads in the Kingdom分析和实现

    题目大意:国家有n个城市,还有n条道路,每条道路连通两个不同的城市,n条道路使得所有n个城市相互连通.现在国家经费不足,要关闭一条道路.国家的不便度定义为国家中任意两个不同的城市之间的距离的最大值,那 ...

  4. CodeForces - 115E:Linear Kingdom Races (DP+线段树+lazy)

    pro: 从左到有有N个车道,都有一定程度损坏,所以有不同的修理费a[]: 有M场比赛,每场比赛的场地是[Li,Ri],即如果这个区间的车道都被修理好,则可以举办这个比赛,并且收益是Pi.问最多得到多 ...

  5. Linova and Kingdom(树型-贪心)

    题目大意:给定一棵树,1为首都(首都可以是工业城市也可以是旅游城市),一共有n个点. 其中要选出k个工业城市,每个工业城市出一个代表去首都,其快乐值是其途径旅游城市(非工业)的个数 求所有快乐值相加的 ...

  6. CF1336 Linova and Kingdom

    题面 给定 n 个节点的有根树,根是 1 号节点. 你可以选择 k 个节点将其设置为工业城市,其余设置为旅游城市. 对于一个工业城市,定义它的幸福值为工业城市到根的路径经过的旅游城市的数量. 你需要求 ...

  7. Codeforces Round #635 (Div. 2) 题解

    渭城朝雨浥轻尘,客舍青青柳色新. 劝君更尽一杯酒,西出阳关无故人.--王维 A. Ichihime and Triangle 网址:https://codeforces.com/contest/133 ...

  8. Codeforces Round #635 (Div. 2)

    Contest Info Practice Link Solved A B C D E F 4/6 O O Ø  Ø     O 在比赛中通过 Ø 赛后通过 ! 尝试了但是失败了 - 没有尝试 Sol ...

  9. Codeforces Round #635 (Div. 2)部分(A~E)题解

    虽然打的是div1,但最后半小时完全处于挂机状态,不会做1C,只有个 \(O(n^3)\) 的想法,水了水论坛,甚至看了一下div2的AB,所以干脆顺便写个div2的题解吧,内容看上去还丰富一些(X) ...

随机推荐

  1. “随手记”开发记录day13

    今天继续对我们的项目进行更改. 今天我们需要做的是增加“修改”功能.对于已经添加的记账记录,长按可以进行修改和删除的操作. 但是今天并没有完成……

  2. 数据洞察 | Python解读地摊——你想好摆摊去卖什么了吗?

    知乎上有一个问题:疫情结束后,你最想做的一件事是什么? 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手.很多已经做案例的人,却不知道如何去 ...

  3. 2020-06-02:千万级数据量的list找一个数据。

    福哥答案2020-06-02: 对于千万级长度的数组单值查找:序号小的,单线程占明显优势:序号大的,多线程占明显优势.单线程时间不稳定,多线程时间稳定. go语言测试代码如下: package mai ...

  4. Who Am I? Personality Detection based on Deep Learning for Texts 阅读笔记

    文章目录 源代码github地址 摘要 2CLSTM 过程 1. 词嵌入 2. 2LSTM处理 3. CNN学习LSGCNN学习LSG 4. Softmax分类 源代码github地址 https:/ ...

  5. My_Tomcat_Host 靶机

    1:扫描网段: 发现主机IP为192.168.1.203 2:nmap 扫描端口信息 发现8080端口开启了http服务  22ssh服务 3:尝试ssh连接是需要密码的,然后访问8080端口 4:发 ...

  6. JNDI和连接池的配置

    什么是JNDI: Java Naming and Directory Interface,Java命名和目录接口 通过名称将资源与服务进行关联 配置JNDI的步骤:在tomcat下的Context.x ...

  7. 数据库之Oracle优化技巧(一)

    数据库之Oracle优化技巧(一) 1.where子句中的连接顺序 在Oracle数据库中,where子句的执行顺序是自下而上进行解析,根据这个原理,表之间的连接必须写在其他where条件之前,那些可 ...

  8. 微信小程序setData局部刷新列表

    利用setData局部刷新列表 当列表管理加载到第几页时,这个list的数据有十几条的,如果重新setData的话就要重新刷新和渲染列表, 这是个比较麻烦的事,当数据量大时,就会造成白屏, 这时就要局 ...

  9. CSS动画实例:Loading加载动画效果(一)

    一些网站或者APP在加载新东西的时候,往往会给出一个好看有趣的Loading图,大部分的Loading样式都可以使用CSS3制作出来,它不仅比直接使用gif图简单方便,还能节省加载时间和空间.下面介绍 ...

  10. 目录扫描、Nmap

    一.基本定义 1.目录扫描: 扫描站点的目录,寻找敏感文件(目录名.探针文件.后台.robots.txt.备份文件等). 2.目录:站点结构,权限控制不严格. 3.探针文件:服务器配置信息,例:php ...