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