https://codeforces.com/contest/958/problem/B2

题解:https://www.cnblogs.com/Cool-Angel/p/8862649.html

upd2018-11-01:

修了一个bug(第60行加入inq[1]=1)

 #include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
struct E
{
int to,nxt;
}e[];
int f1[],ne;
int rt;
bool inq[];
int dep[],d[],mlen[],sz[],mson[];
void dfs1(int u,int fa)
{
dep[u]=dep[fa]+;
for(int k=f1[u];k;k=e[k].nxt)
if(e[k].to!=fa)
{
dfs1(e[k].to,u);
mlen[u]=max(mlen[u],mlen[e[k].to]);
sz[u]+=sz[e[k].to];
}
mlen[u]++;
sz[u]++;
}
void dfs2(int u,int fa)
{
for(int k=f1[u];k;k=e[k].nxt)
if(e[k].to!=fa)
if(!mson[u]||mlen[e[k].to]>mlen[mson[u]])
mson[u]=e[k].to;
d[u]=;
if(u==mson[fa]) d[u]+=d[fa];
for(int k=f1[u];k;k=e[k].nxt)
if(e[k].to!=fa)
dfs2(e[k].to,u);
}
queue<int> q;
int n,nn;
int tt[];
int main()
{
int i,x,y,u;
scanf("%d",&n);nn=n;
for(i=;i<n;i++)
{
scanf("%d%d",&x,&y);
e[++ne].to=y;e[ne].nxt=f1[x];f1[x]=ne;
e[++ne].to=x;e[ne].nxt=f1[y];f1[y]=ne;
}
q.push();inq[]=;
while(!q.empty())
{
u=q.front();q.pop();
rt=u;
for(int k=f1[u];k;k=e[k].nxt)
if(!inq[e[k].to])
{
inq[e[k].to]=;
q.push(e[k].to);
}
}
dfs1(rt,);
dfs2(rt,);
printf("%d ",);nn--;
/*
for(i=1;i<=n;i++)
printf("t%d %d %d\n",i,sz[i],d[i]);
*/
for(i=;i<=n;i++)
if(sz[i]==)
tt[++tt[]]=d[i];
sort(tt+,tt+tt[]+);
int sum=;
for(i=tt[];i>=&&nn;i--)
{
sum+=tt[i];
printf("%d ",sum);
nn--;
}
//puts("test");
for(;nn;nn--) printf("%d ",sum);
return ;
}

Maximum Control (medium) Codeforces - 958B2的更多相关文章

  1. 【Helvetic Coding Contest 2018】B2. Maximum Control (medium)

    Description 传送门(翻译就别想了,本人英语太垃圾) Solution 设ans[i]为设置i个船时能控制的最多星球数(看到这你可能因为是dp,然而我可以很负责地告诉你是假的) 首先一个显然 ...

  2. Guard Duty (medium) Codeforces - 958E2 || (bzoj 2151||洛谷P1792) 种树 || 编译优化

    https://codeforces.com/contest/958/problem/E2 首先求出N个时刻的N-1个间隔长度,问题就相当于在这些间隔中选K个数,相邻两个不能同时选,要求和最小 方法1 ...

  3. Maximum Xor Secondary CodeForces - 281D (单调栈)

    Bike loves looking for the second maximum element in the sequence. The second maximum element in the ...

  4. Codeforces Round #670 (Div. 2) 深夜掉分(A - C题补题)

    1406A. Subset Mex https://codeforces.com/contest/1406/problem/A Example input 4 6 0 2 1 5 0 1 3 0 1 ...

  5. URL Quoting

    [URL Quoting] The URL quoting functions focus on taking program data and making it safe for use as U ...

  6. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  7. Unity3d-Particle System 5.x系统的学习(四)

    Unity3d-Particle System 5.x系统的学习(四) 今天,我们来聊聊unity5.x的粒子系统和unity4.x粒子系统的区别. 我大致看了下,区别还是蛮多的,但是总体的粒子制作思 ...

  8. Persisting Data to the Device

    Persisting Data to the Device Edit PagePage History Overview The Android framework offers several op ...

  9. Local Databases with SQLiteOpenHelper

    Overview For maximum control over local data, developers can use SQLite directly by leveraging SQLit ...

随机推荐

  1. ansible-playbook 打通ssh无秘钥

    建议参考: http://www.cnblogs.com/jackchen001/p/6514018.html 这个代码清晰,效果佳! 参考链接: http://www.cnblogs.com/cao ...

  2. javaScrpit中NaN的秘密

    NaN,不是一个数字,是一种特殊的值来代表不可表示的值,使用typeof或其他任何与之比较的处理方式,‘NaN’则会引起一些混乱, 一些操作会导致NaN值的产生.这里有些例子: Math.sqrt(- ...

  3. BZOJ_3123_[Sdoi2013]森林_主席树+启发式合并

    BZOJ_3123_[Sdoi2013]森林_主席树+启发式合并 Description Input 第一行包含一个正整数testcase,表示当前测试数据的测试点编号.保证1≤testcase≤20 ...

  4. 疯狂LCM

    传送门 题目要求求: \[\sum_{i=1}^nlcm(i,n)\] 先转化成gcd处理: \[n\sum_{i=1}^n\frac{i}{gcd(i,j)}\] 之后老套路 枚举gcd,并且先把d ...

  5. Tensorboard 的简单使用

    确保环境以及安装好tensorflow以及tensorboard 下面通过一个简单的例子来显示一下使用方式,一个向量加法的图结构. import tensorflow as tf a = tf.con ...

  6. MakeFile 文件的使用

    什么是Makefile? 一个工程中的源文件不计其数,其按类型.功能.模块分别放在若干个目录中,makefile定义了一系列的规则来指定,哪些文件需要先编译,哪些文件需要后编译,哪些文件需要重新编译, ...

  7. window.open全屏

    window.open全屏   1. window.open(url,'资金计划项超支提醒','width='+(window.screen.availWidth-10)+',height='+(wi ...

  8. Flexpaper二次开发入门教程》(十) Flexpaper简单使用-第一个Flexpaper例子

    4. Flexpaper简单使用 通过上面三章的内容,大家对Flexpaper.SWFTools应该有大概的了解了,SWF文件也已经生成了,我们开始进入Flexpaper的使用的介绍. 本章中只演示F ...

  9. Eclipse -- 自动补齐设置和其他用法

    1:自动补齐设置:最简单的修改方式是:Windows——>Preferences——>Java-->Editor-->Content Asist,在Auto activatio ...

  10. ActionBar 溢出菜单和兼容问题解决

    当我们想做一个类似于微信菜单 很快我们应该可以想到可以用support-v7包或者actionbarshecklock,然后就写menu布局,如下 <menu xmlns:android=&qu ...