Linova and Kingdom

题目来源:Codeforces Round #635 (Div. 2) C题

Writing light novels is the most important thing in Linova’s life. Last night, Linova dreamed about a fantastic kingdom. She began to write a light novel for the kingdom as soon as she woke up, and of course, she is the queen of it.

There are n cities and n−1 two-way roads connecting pairs of cities in the kingdom. From any city, you can reach any other city by walking through some roads. The cities are numbered from 1 to n, and the city 1 is the capital of the kingdom. So, the kingdom has a tree structure.

As the queen, Linova plans to choose exactly k cities developing industry, while the other cities will develop tourism. The capital also can be either industrial or tourism city.

A meeting is held in the capital once a year. To attend the meeting, each industry city sends an envoy. All envoys will follow the shortest path from the departure city to the capital (which is unique).

Traveling in tourism cities is pleasant. For each envoy, his happiness is equal to the number of tourism cities on his path.

In order to be a queen loved by people, Linova wants to choose k cities which can maximize the sum of happinesses of all envoys. Can you calculate the maximum sum for her?

Input
The first line contains two integers n and k (2≤n≤2⋅105, 1≤k<n) — the number of cities and industry cities respectively.

Each of the next n−1 lines contains two integers u and v (1≤u,v≤n), denoting there is a road connecting city u and city v.

It is guaranteed that from any city, you can reach any other city by the roads.

Output
Print the only line containing a single integer — the maximum possible sum of happinesses of all envoys.

Examples
input
7 4
1 2
1 3
1 4
3 5
3 6
4 7
output
7
input
4 1
1 2
1 3
2 4
output
2
input
8 5
7 5
1 7
6 1
3 7
8 3
2 1
4 5
output
9
Note

In the first example, Linova can choose cities 2, 5, 6, 7 to develop industry, then the happiness of the envoy from city 2 is 1, the happiness of envoys from cities 5, 6, 7 is 2. The sum of happinesses is 7, and it can be proved to be the maximum one.

In the second example, choosing cities 3, 4 developing industry can reach a sum of 3, but remember that Linova plans to choose exactly k cities developing industry, then the maximum sum is 2.
题目大意:给定一棵树,1为首都(首都可以是工业城市也可以是旅游城市),一共有n个点,其中要有k个工业城市,每个工业城市出一个代表去首都,其快乐值是其途径旅游城市的个数,求所有快乐值相加的最大值。

这题其实很水,但是就是一点小bug找不出来,比赛刚结束,试着交了一发,然后过了……哭辽。

思路是这样的:先重新建树(变成有向图),顺便记录其层数及所有子节点个数(注意:是所有,意思是它子节点的子节点也算),最后贪心,找(深度-子节点个数)最大的k个值,将其相加即可。

#pragma GCC optimize(3,"Ofast","inline")
#pragma G++ optimize(3)
#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <sstream>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pll;
typedef pair<int,int> pii;
typedef queue<int> q_i;
typedef queue<string> q_s;
typedef queue<double> q_d;
typedef queue<ll> q_ll;
typedef queue<char> q_c;
typedef priority_queue<int> pq_i;
typedef priority_queue<string> pq_s;
typedef priority_queue<double> pq_d;
typedef priority_queue<ll> pq_ll;
typedef stack<int> s_i;
typedef stack<string> s_s;
typedef stack<double> s_d;
typedef stack<ll> s_ll;
typedef stack<char> s_c;
typedef map<ll,ll> m_ll_ll;
typedef map<int,ll> m_i_ll;
typedef map<int,int> m_i_i;
typedef map<string,ll> m_s_ll;
typedef map<char,int> m_c_i;
typedef map<char,ll> m_c_ll;
const ll INF=0x3f3f3f3f;
#define rep(i,l,r) for(ll i=l;i<=r;i++)
#define per(i,l,r) for(ll i=r;i>=l;i--)
#define eif else if
#define N 2000005
#define mm(dp) memset(dp,0,sizeof(dp))
#define mm1(dp) memset(dp,-1,sizeof(dp))
#define mm2(dp) memset(dp,0x3f,sizeof(dp))
#define IT set<int>::iterator
#define fs(n) fixed<< setprecision(n)
const double e=2.71828182845;
const double pi = acos(-1.0);
queue<int>que;
vector<int>edge[200005];
struct STU
{int to,shen;
};
vector<STU>edge1[200005];
typedef struct
{int num;int shen,zi;
} STU1;
STU1 stu1[200005];
bool cmp(STU1 x,STU1 y)
{return (x.zi-x.shen)<(y.zi-y.shen);
}
bool cmp1(STU1 x,STU1 y)
{return x.shen>y.shen;
}
int main()
{ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);int n,k;cin>>n>>k;int flag[n+1];int flag1[n+1];int flag2[n+1];mm(flag);mm(flag1);mm(flag2);int m=n-1;while(m--){int u,v;cin>>u>>v;edge[u].push_back(v);edge[v].push_back(u);}que.push(1);flag[1]=1;flag1[1]=0;flag2[1]=0;while(!que.empty()){int p=que.front();que.pop();for(int i=0; i<edge[p].size(); i++){int k=edge[p][i];if(flag[k]==0){STU stu;stu.to=k;stu.shen=flag1[p]+1;flag1[k]=stu.shen;edge1[p].push_back(stu);que.push(k);flag[k]=1;flag2[p]++;}}}rep(i,1,n){stu1[i].num=i;stu1[i].shen=flag1[i];stu1[i].zi=flag2[i];}sort(stu1+1,stu1+1+n,cmp1);rep(i,1,n){int p=stu1[i].num;for(int j=0; j<edge1[p].size(); j++){STU stu;stu=edge1[p][j];int v=stu.to;stu1[i].zi+=flag2[v];}}sort(stu1+1,stu1+1+n,cmp);ll ans=0;rep(i,1,k){ans+=(stu1[i].shen-stu1[i].zi);}cout<<ans;return 0;
}

Linova and Kingdom相关推荐

  1. CF1336 A. Linova and Kingdom

    A. Linova and Kingdom 题意 给你一颗 n n n个节点根为1号节点的树,选 k k k个城市作为工业城市,其余为旅游城市,工业城市的使节每经过旅游城市开心值+1,求所有工业城市的 ...

  2. C. Linova and Kingdom

    链接:https://codeforces.ml/contest/1337/problem/C Writing light novels is the most important thing in ...

  3. CodeForces - 1337C Linova and Kingdom(贪心)

    题目链接:点击查看 题目大意:给出一棵树表示一个国家,点1表示首都,现在需要分配 k 个城市为工业城市,其余 n - k 个城市为旅游城市,这个国家会定时在首都召开会议,换句话说,所有工业城市都必须派 ...

  4. 1336A - Linova and Kingdom

    链接: https://codeforces.com/problemset/problem/1336/A 题意: 有一个n节点树,根为1,现在要设置k个工业城市,剩下n-k个旅游城市,让每个工业城市前 ...

  5. CF1336A Linova and Kingdom

    看这里 大意: 有一个有 n 个点的树,以 1 为根,你可以选择 k 个节点,使得这 k 个节点到 1 节点的最短路径中经过的非选择的点最多. 思路: 简单树形dp 我们肯定会优先最好的叶子结点,这个 ...

  6. bottom sheets_Excel 2013中的SHEET和SHEETS函数

    bottom sheets I've been experimenting with the new SHEET and SHEETS functions in Excel 2013, to see ...

  7. bottom sheets_使用Sheetson建立由Google Sheets支持的免费电子邮件列表

    bottom sheets When building an initial MVP for your product, it's wise to avoid unnecessary expenses ...

  8. Codeforces补题记录(1)

    文章目录 Codeforces补题记录(1) 1.Codeforces Round #632 (Div. 2)(2020.4.11) A.Little Artem B.Kind Anton *C.Eu ...

  9. Codeforces Round #635 (Div. 2)(A~D)题解

    Codeforces #635 A~D A.Ichihime and Triangle B.Kana and Dragon Quest game C.Linova and Kingdom D.Xeni ...

最新文章

  1. kappa一致性检验教程_SPSS在线_SPSSAU_Kappa一致性检验
  2. WIFI网络,两台笔记本互联Oracle,一台是11g,一台是12c
  3. android 适合mvp模式,Android中的MVP:如何使Presenter层系统化?
  4. Linq查询datatable的记录集合
  5. 【牛客 - 327G】处女座与复读机(可编辑距离问题,dp)
  6. 上下定高 中间自适应_上下固定中间自适应布局
  7. git本地分支和远程分支操作
  8. c语言完整版 pdf,(完整版)C语言技术与应用.pdf
  9. excel_applications
  10. html5swf小游戏源码,亲测可用120个H5小游戏实例源码
  11. Qt5+STM32F407+步进电机 | 通过电脑控制步进电机实现:6+2通道、速度可变、运动精确步数的教程——基础知识(2/4)
  12. 工业相机像元与像素之间的关系
  13. EXCEL操作之:为纯数字编码添加前缀/后缀后以文本的方式保存
  14. 已然神话的区块链的应用了解一下-MGCEX
  15. mathcad 15.0安装教程
  16. android 多线程创建texture,从源码角度剖析Android系统EGL及GL线程
  17. 如何携号转网只需三步
  18. 开放英语计算机考试,(荐)最新电大《英语I(1)》(开放英语1)完成句子、翻译题归纳汇总复习小抄.doc...
  19. 《矩阵理论》大萌课程笔记 - 线性空间与子空间
  20. .NET MAUI 安卓 UI 资源设置

热门文章

  1. MySQL高可用和灾备调研
  2. android tag的使用方法,Android setTag方法的key问题解决办法
  3. Linux 部署开源WAF模块 ModSecurity
  4. 微信小程序身份证扫描OCR(信息自动带入)
  5. JavaScript原型链实现继承
  6. 多边形的单边裁剪算法-JS
  7. php大转盘程序,jQuery幸运大转盘_jQuery+PHP抽奖程序(下)
  8. Java自学教程!mysql环境变量配置好了之后怎么办
  9. 新手安装hadoop教程和个人遇到的错误及解决办法(错误:./hadoop-daemon.sh start journalnode用不了和WARNING: HADOOP_SECURE_DN_USER)
  10. 电子护照阅读器|证件阅读机MEPR100+与MEPR100性能分析与差异化对比