链接:https://codeforces.ml/contest/1337/problem/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 nn cities and n−1n−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 11 to nn, and the city 11 is the capital of the kingdom. So, the kingdom has a tree structure.

As the queen, Linova plans to choose exactly kk 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 kk 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 nn and kk (2≤n≤2⋅1052≤n≤2⋅105, 1≤k<n1≤k<n)  — the number of cities and industry cities respectively.

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

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

Copy

7 4
1 2
1 3
1 4
3 5
3 6
4 7

output

Copy

7

input

Copy

4 1
1 2
1 3
2 4

output

Copy

2

input

Copy

8 5
7 5
1 7
6 1
3 7
8 3
2 1
4 5

output

Copy

9

Note

In the first example, Linova can choose cities 22, 55, 66, 77 to develop industry, then the happiness of the envoy from city 22 is 11, the happiness of envoys from cities 55, 66, 77 is 22. The sum of happinesses is 77, and it can be proved to be the maximum one.

正在上传…重新上传取消

In the second example, choosing cities 33, 44 developing industry can reach a sum of 33, but remember that Linova plans to choose exactly kk cities developing industry, then the maximum sum is 22.

代码:

#include<bits/stdc++.h>
using namespace std;
long long n,k,l,r,t,s,a[200001];
long long vis[200001],f[200001];
map<long long,long long>m[200001],p;
struct node{long long sum;long long id;
}x[200001];
void dfs(int i,int tip)
{for(int j=1;j<=p[i];j++){if(f[m[i][j]]==0){f[m[i][j]]=1;dfs(m[i][j],tip+1);x[m[i][j]].sum=tip+1;a[i]+=a[m[i][j]];}}a[i]++;
}
int main()
{cin>>n>>k;for(int i=1;i<=n-1;i++){cin>>l>>r;p[l]++;p[r]++;m[l][p[l]]=r;m[r][p[r]]=l;}f[1]=1;dfs(1,0);f[1]=0;x[1].sum=0;for(int i=1;i<=n;i++){x[i].id=i;vis[i]=x[i].sum;}s=0;for(int i=1;i<=n;i++){vis[i]+=1-a[i];//cout<<vis[i]<<" ";}sort(vis+1,vis+1+n);for(int i=n;i>=n-k+1;i--){s+=vis[i];}cout<<s<<endl;
}

C. Linova and Kingdom相关推荐

  1. Linova and Kingdom

    Linova and Kingdom 题目来源:Codeforces Round #635 (Div. 2) C题 Writing light novels is the most important ...

  2. CF1336 A. Linova and Kingdom

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

  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. python发送邮件带附件_Python发送邮件(带附件)
  2. win 对比工具-Araxis Merge
  3. 修改 SQL Server 服务器的 IP 地址
  4. linux怎么开ssh端口,如何查看linux中的ssh端口开启状态
  5. python快速编程入门课本第六章_python编程快速上手第六章实践项目参考code
  6. 将angular转化为手机app_手机照片快速转化为PBR材质流程
  7. MyEclipse提示Errors occurred during the build
  8. Python中dict详解
  9. 虚拟机安装Centos 7网上教程整合
  10. 解决Hadoop时no namenode to stop异常或则 是 jps中没有namenode
  11. 集群服务器分析系统,集群服务器系统可扩展性的研究与实现
  12. Mariadb数据库设置及操作 一主多从 备份还原(实测笔记)
  13. sweetalert2使用教程
  14. php怎么异步执行,php怎么实现异步
  15. 4170万元人民币的“绿坝·花季护航”预装1年软件,到底值不值?
  16. adobe flash builder 4.6最新能用的序列号
  17. 沪深证券交易机制概述
  18. 微信公众号之自定义菜单
  19. uboot中展示gpio接口的驱动
  20. python对excel分列转多行

热门文章

  1. Spring Boot之OneToMany、ManyToOne示例分析
  2. 安装了Tomcat Plugin插件,为什么没有出现三个Tomcat的猫图标的解决方法
  3. 百度交易中台之订单系统架构浅析
  4. 当无线通信正式成为战略能源:5G时代的蝴蝶振翅
  5. List、Set 区别
  6. FPGA-Verilog代码编写及项目设计总结
  7. uniapp实现自定义拍照窗口
  8. Linux源码编译安装Postgres
  9. html文件都被感染,virus.html.cloud.1是什么病毒?网站备份被感染如何处理
  10. ctrl-z、fg、bg、nohup、setsid、()与、disown、screen