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. [HAOI2016]找相同子串

    这题感觉有点坑啊. 题目还是不难想的,先对一个字符串建后缀自动机,然后拿另一个字符串在上面跑. 假设当前跑到了p点,匹配长度为len. 那么当前会对答案产生贡献的串是哪些呢? 显然当前会对p及p到根的 ...

  2. Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=os&infra=stoc

    今天在使用yum安装文件时,出现了以下问题: root@localhost opt]# yum update Loaded plugins: fastestmirror Could not retri ...

  3. iap 应用内购买相关的解释

    应用范围app Store Review Guidelines : https://developer.apple.com/app-store/review/guidelines/  中 11.12  ...

  4. netty ssl

    netty提供的例子中有secury的实现,不过是一个伪证书.修改了一下其中的SecureChatSslContextFactory类,使用证书的方式实现ssl.修改后代码如下: public fin ...

  5. HihoCoder 1508 : 剑刃风暴(占位)

    描述 主宰尤涅若拥有一招非常厉害的招式——剑刃风暴,“无论是战士还是法师,都害怕尤涅若的武士刀剑技”. 现在战场上有N名敌对英雄,他们的位置分别为(Xi, Yi),而剑刃风暴的伤害范围是一个半径为R的 ...

  6. git解决冲突方式

    Git解决冲突 安装beyond compare 4 2.配置git对比工具 #difftool 配置 git config --global diff.tool bc4 git config --g ...

  7. HDMI 8193 配置

    1, User space:ProjectConfig.mkMTK_HDMI_SUPPORT = yes MTK_MULTIBRIDGE_SUPPORT = yesMTK_INTERNAL_HDMI_ ...

  8. camera调试命令

    问题:adb install -r Camera.apk 2864 KB/s (7603465 bytes in 2.592s)Failure [INSTALL_FAILED_UPDATE_INCOM ...

  9. 杂项:MIS

    ylbtech-杂项:MIS 1.返回顶部 1. 管理信息系统(Management Information System,简称MIS)是一个以人为主导,利用计算机硬件.软件.网络通信设备以及其他办公 ...

  10. Firebug的安装与使用

    第一步,点击 Firefox 浏览器上的“工具”选项,然后点击“附加软件”,在弹出的小窗口中,点击右下角的“获取扩展”选项,如图 4 所示. 图 4. 获取扩展 第二步,在点击“获取扩展”选项后,打开 ...