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 ...
随机推荐
- 在win7系统下安装把Ubuntu17.04安装在另一个硬盘开机无法进入Ubuntu问题的一种解决办法。【转】
本文转载自:http://blog.csdn.net/u012879090/article/details/74937762 在win7系统下安装把Ubuntu17.04安装在另一个硬盘开机无法进入U ...
- webstorm代码提示按键改为alt+/
webstorm代码提示默认按键为ctrl+空格 但是windows输入法中英文输入法的默认按键也是ctrl+空格 这就导致webstorm按键冲突,无法使用代码快捷提示按键 解决方法: 按ctrl+ ...
- 使用NSTask调用shell
- (NSString *)cmd:(NSString *)cmd { // 初始化并设置shell路径 NSTask *task = [[NSTask alloc] init]; [task set ...
- maven中常用命令
1. 更新本地仓库, 首先确认C:\users\pengqiong\ 路径下有相应的pom文件 mvn clean package install:
- hdu-5742 It's All In The Mind(数学)
题目链接: It's All In The Mind Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (J ...
- cygwin Could not create directory '/home/Administrator/.ssh'
在cygwin下运行: ssh-keygen -C "634772208@qq.com" -t rsa 时,出现如下错误: cygwin Could not create dire ...
- java前三天
JDK :开发 JRE:运行 JVM:运行环境(跨平台) 注释:推荐使用第二种方式设置环境变量 nopad++ :设置 注释:1字节等于8位,也就等于2的8次方 2字节等于16位,也就等于2的16次方 ...
- codevs 2102 石子归并2
传送门 2102 石子归并 2 时间限制: 10 s 空间限制: 256000 KB 题目等级 : 黄金 Gold 题目描述 Description 在一个园形操场的四周摆放N堆石子,现要将 ...
- DLL的远程注入技术
DLL的远程注入技术是目前Win32病毒广泛使用的一种技术.使用这种技术的病毒体通常位于一个DLL中,在系统启动的时候,一个EXE程序会将这个DLL加载至某些系统进程(如Explorer.exe)中运 ...
- kafka之六:为什么Kafka那么快
转自: http://mp.weixin.qq.com/s?__biz=MzIxMjAzMDA1MQ==&mid=2648945468&idx=1&sn=b622788361 ...