Maximum Control (medium) Codeforces - 958B2
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的更多相关文章
- 【Helvetic Coding Contest 2018】B2. Maximum Control (medium)
Description 传送门(翻译就别想了,本人英语太垃圾) Solution 设ans[i]为设置i个船时能控制的最多星球数(看到这你可能因为是dp,然而我可以很负责地告诉你是假的) 首先一个显然 ...
- Guard Duty (medium) Codeforces - 958E2 || (bzoj 2151||洛谷P1792) 种树 || 编译优化
https://codeforces.com/contest/958/problem/E2 首先求出N个时刻的N-1个间隔长度,问题就相当于在这些间隔中选K个数,相邻两个不能同时选,要求和最小 方法1 ...
- Maximum Xor Secondary CodeForces - 281D (单调栈)
Bike loves looking for the second maximum element in the sequence. The second maximum element in the ...
- 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 ...
- URL Quoting
[URL Quoting] The URL quoting functions focus on taking program data and making it safe for use as U ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- Unity3d-Particle System 5.x系统的学习(四)
Unity3d-Particle System 5.x系统的学习(四) 今天,我们来聊聊unity5.x的粒子系统和unity4.x粒子系统的区别. 我大致看了下,区别还是蛮多的,但是总体的粒子制作思 ...
- Persisting Data to the Device
Persisting Data to the Device Edit PagePage History Overview The Android framework offers several op ...
- Local Databases with SQLiteOpenHelper
Overview For maximum control over local data, developers can use SQLite directly by leveraging SQLit ...
随机推荐
- Codeforces Round #383 (Div. 2) B. Arpa’s obvious problem and Mehrdad’s terrible solution —— 异或
题目链接:http://codeforces.com/contest/742/problem/B B. Arpa's obvious problem and Mehrdad's terrible so ...
- 使用Dubbo实现RPC调用
启动Dubbo服务有2个方式,1是通过xml配置,2是通过注解来实现,这点和Spring相似. 采用XML配置如下: <?xml version="1.0" encoding ...
- cnn汉字识别 tensorflow demo
# -*- coding: utf-8 -*- import tensorflow as tf import os import random import tensorflow.contrib.sl ...
- MongoDB监控之一:运行状态、性能监控,分析
为什么要监控? 监控及时获得应用的运行状态信息,在问题出现时及时发现. 监控什么? CPU.内存.磁盘I/O.应用程序(MongoDB).进程监控(ps -aux).错误日志监控 1.4.1 Mong ...
- silverlight RadGridView 动态添加数据列
public void BindFaultGridInfo(IList<HealthStatusApp.Web.Models.FaultMajorModel> list) { rg_Fau ...
- jquery.Datatable.js
http://www.cnblogs.com/nier/archive/2012/03/18/2404836.html http://blog.csdn.net/mickey_miki/article ...
- 通过url在两个页面之间传值
今天开发遇到一个需要通过url传值才能解决的问题,就查了一下资料 一下是整理的结果: 假如需要把a页面的值传到b页面 a页面代码 <script type="text/javascri ...
- 《剑指offer面试题4》替换空格——实现函数把字符串中每个空格替换成“%20”
思路: 例如把we are happy这个字符串中所有空格替换成"%20",最直接的做法是从头开始扫苗,遇到空格就替换,并且把空格后面的字符都顺序后移.复杂度O(n^2). 重要思 ...
- [WIP]webpack 概念
创建: 2019/04/09 概念 入口 指示 webpack 应该使用哪个模块,来作为构建其内部依赖图的开始. 进入入口起点后,webpack 会找出有哪些模块和库是入口起点(直接和间接)依赖 ...
- oop的三大特点
1.封装性:也称为信息隐藏,就是将一个类的使用和实现分开,只保留部分接口和方法与外部联系,或者说只公开了一些供开发人员使用的方法.于是开发人员只 需要关注这个类如何使用,而不用去关心其具体的实现过程, ...