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. Codeforces 724C Ray Tracing 扩展欧几里得

    吐槽:在比赛的时候,压根就没想到这题还可以对称: 题解:http://blog.csdn.net/danliwoo/article/details/52761839 比较详细: #include< ...

  2. jmeter使用笔记——脚本录制,JMeter使用plugins插件进行服务器性能监控

    脚本录制: 1.badboy录制 2.代理服务器录制 ①工作台添加HTTP代理服务器 ②设置目标控制器,分组,排除模式,包含模式(使用正则表达式筛选) ③设置浏览器,手动设置代理服务器,localho ...

  3. <ZZ>Linux rpm 命令参数使用详解[介绍和应用]

    http://www.cnblogs.com/xiaochaohuashengmi/archive/2011/10/08/2203153.html RPM是RedHat Package Manager ...

  4. java的一个爬虫

    进行抓取页面,我看了一下人家的教程,一般要用到htmlparser用来解析html得到一个网页的相关链接,用httpclient抓取网页数据, 下面是一我写的spider类 package com.o ...

  5. codeforces 467B Fedor and New Game 解题报告

    题目链接:http://codeforces.com/contest/467/problem/B 题目意思:有 m + 1 个 player 和 n 种类型的 soldiers.每个player被赋予 ...

  6. plsql导入csv数据,未响应,invalid identifier

    问题分析: 1.确保cvs字段名与表字段名一致,不要有空格 2.cvs字段对应表字段的大写,确保表字段都是大写 3.如果字段能对应上,plsql会自动识别出来

  7. 关于SelectObject之后是否要恢复之前的GDI对象

    以下列代码为例 { // 创建内存DC CDC mMemDc; mMemDc.CreateCompatibleDC( &dc ); // 创建兼容位图 CBitmap bmpMemBmp; b ...

  8. 「LuoguP3808」 【模板】AC自动机(简单版)

    题目背景 通过套取数据而直接“打表”过题者,是作弊行为,发现即棕名. 这是一道简单的AC自动机模板题. 用于检测正确性以及算法常数. 为了防止卡OJ,在保证正确的基础上只有两组数据,请不要恶意提交. ...

  9. bzoj 4559 [JLoi2016]成绩比较 —— DP+拉格朗日插值

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4559 看了看拉格朗日插值:http://www.cnblogs.com/ECJTUACM-8 ...

  10. Redis的小白应用

    在window下测试学习redis 步骤: 1.先下载安装 redis,(conf文件制定配置文件(redis-server.exe  redis.conf ),若不指定则默认), 基本上我是直接点击 ...