Linova and Kingdom

2024-01-24 13:48
文章标签 kingdom linova

本文主要是介绍Linova and Kingdom,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/639885

相关文章

UVa1670/LA5920 Kingdom Roadmap

UVa1670/LA5920 Kingdom Roadmap 题目链接题意分析AC 代码 题目链接   本题是2011年icpc欧洲区域赛东北欧赛区的K题 题意   输入一个n(n≤100000)个结点的树,添加尽量少的边,使得任意删除一条边之后图仍然连通。如下图所示,最优方案用虚线表示。 分析   首先统计叶结点数c,即可知道答案是 ⌈ c 2 ⌉ \lceil\fr

hdu4777 Rabbit Kingdom 离线树状数组 求询问区间内的区间数

题意:询问区间内有多少个数与区间中其他的数都互质 分析:易得,一个区间内的数的个数减去,与其他数不互质的数即可——即离当前数i左边最近的不互质的数的位置(设为L[i])和右边最近的不互质的数的位置(设为R[i])有一个在区间[L,R]内。那么问题就变成统计:1.区间[L,R]中有多少个数的L[i]或R[i]在区间[L,R]内。2.多少个数的L[i]且R[i]在区间[L,R]内。对于每个询问,答案

hdu 5583 Kingdom of Black and White(高效)

题目链接:hdu 5583 Kingdom of Black and White 代码 #include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn = 1e5 + 5;typedef long long ll;char str[maxn];int L[maxn][2

Middle Kingdom of Egypt(中王国时期)

Middle Kingdom of Egypt(中王国时期) 储物罐子 地理 Jietu20190402-162434@2x.jpg 介绍 埃及中部王国(也称为统一时期)是古埃及历史上经过一段称为第一中间阶段的政治分裂的时期。中王国时期从公元前2050年左右持续到公元前1710年左右。 文物 Jietu20190402-162706@2x.jp

Rabbit Kingdom HDU - 4777 (离线处理+树状数组)

Rabbit Kingdom  HDU - 4777  题意:给定n个数a[i] ( 1=< i <=n) 现在给定m个询问,每个询问一个区间[l,r],问该区间有多少个数与其它所有的数互素。 1 =< n,m,a[i] <= 200000 思路:对于每个数a[i] 处理后可以得到一个区间[L,R]在这个区间里面,a[i]对所有包含i的[L,R]的子区间都能贡献一个结果。每个a[i] 得到

Contest1002 - HHU ACM 综合训练1 A题 Kingdom of Black and White(朴素)

题意:给定一串01序列,序列的值表示为序列中连续的0或1的个数的平方相加的和。最多可以改变其中一个数字变为0或1,求序列最大值。 思路:很朴素的想法,一开始我以为有什么简便算法,然而并没有= = 首先离散化,数组存储的是连续的0或1的个数,之后对这个数组进行遍历,每次把a[i]+1的平方+a[i+1]-1的平方计算出来的和与a[i]的平方+a[i+1]的平方计算出来的和相比较,若更新后的值大,

Execution in the Kingdom of Nouns

They've a temper, some of them—particularly verbs: they're the proudest—adjectives you can do anything with, but not verbs—however, I can manage the whole lot of them! Impenetrability! That's what

《功夫之王》The Forbidden Kingdom 功夫专有名词

《功夫之王》正在全球热映,影片中成龙、李连杰两位华人功夫巨星华丽的武打让功夫迷们欲罢不能,电影票房在全球一路飘红,影片成为《卧虎藏龙》、《英雄》 后在全球刮起“中国旋风”的又一华语力作。《功夫之王》编剧约翰•福斯科是一个地道的功夫迷,他在影片中向中国武侠电影致敬的同时也对中国古典文化进行了 大力展示和融合。《功夫之王》中出现了很多中国的谚语和功夫迷们耳熟能详的功夫专有名词,比如螳螂拳,水上漂等。