Codeforces Round #507 Div. 1
D:类似于noip2018d1t3,子树内的链应该贪心的尽量合并而不是拆开。则设f[i]为i子树内满足选的链尽量多的情况下根所在的链的最长长度即可。于是可以线性对某个k求得答案。
注意到长度为k的链不多于n/k个。类似于整除分块可以得到答案不同的k只有O(√n)种。于是我们每做一次dp,可以二分一下该答案的k的上界,就能做到O(n√nlogn)了。卡不来常。
本来一直在考虑的是所有答案总和不超过nlogn,想了一年也不太会,结果好像确实可以用这个性质做到O(nlog2n)?先不管了。
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
#define ll long long
#define N 100010
char getc(){char c=getchar();while ((c<'A'||c>'Z')&&(c<'a'||c>'z')&&(c<'0'||c>'9')) c=getchar();return c;}
int gcd(int n,int m){return m==0?n:gcd(m,n%m);}
int read()
{
int x=0,f=1;char c=getchar();
while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar();
return x*f;
}
int n,p[N],t,f[N],ans[N];
struct data{int to,nxt;
}edge[N<<1];
void addedge(int x,int y){t++;edge[t].to=y,edge[t].nxt=p[x],p[x]=t;}
int calc(int k,int from,int x)
{
int s=0;
for (int i=p[k];i;i=edge[i].nxt)
if (edge[i].to!=from) s+=calc(edge[i].to,k,x);
int mx=0,mx2=0;
for (int i=p[k];i;i=edge[i].nxt)
if (edge[i].to!=from)
if (f[edge[i].to]>mx) mx2=mx,mx=f[edge[i].to];
else mx2=max(mx2,f[edge[i].to]);
if (mx+mx2+1>=x) s++,f[k]=0;
else f[k]=mx+1;
return s;
}
signed main()
{
#ifndef ONLINE_JUDGE
freopen("a.in","r",stdin);
freopen("a.out","w",stdout);
#endif
n=read();
for (int i=1;i<n;i++)
{
int x=read(),y=read();
addedge(x,y),addedge(y,x);
}
for (int i=1;i<=n;i++)
{
ans[i]=calc(1,1,i);
int l=i+1,r=n,u=i;
while (l<=r)
{
int mid=l+r>>1;
if (calc(1,1,mid)!=ans[i]) r=mid-1;
else l=mid+1,u=mid;
}
for (int j=i+1;j<=u;j++) ans[j]=ans[i];
i=u;
}
for (int i=1;i<=n;i++) printf("%d\n",ans[i]);
return 0;
//NOTICE LONG LONG!!!!!
}
E:咕咕咕
Codeforces Round #507 Div. 1的更多相关文章
- Codeforces Round #507 (Div. 2, based on Olympiad of Metropolises) D mt19937
https://codeforces.com/contest/1040/problem/D 用法 mt19937 g(种子); //种子:time(0) mt19937_64 g(); //long ...
- Codeforces Round #507 (Div. 2)C(贪心,逆向遍历,构造)
#include<bits/stdc++.h>using namespace std;int order[300007];long long t[300007],ans[300007];i ...
- 【Codeforces Round #507 (Div. 2, based on Olympiad of Metropolises) B】Shashlik Cooking
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 翻转一次最多影响2k+1个地方. 如果n<=k+1 那么放在1的位置就ok.因为能覆盖1..k+1 如果n<=2k+1 ...
- 【Codeforces Round #507 (Div. 2, based on Olympiad of Metropolises) A】Palindrome Dance
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] i从1..n/2循环一波. 保证a[i]和a[n-i+1]就好. 如果都是2的话填上min(a,b)*2就好 其他情况跟随非2的. ...
- Codeforces Round #366 (Div. 2) ABC
Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
- Codeforces Round #368 (Div. 2)
直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
- Codeforces Round #279 (Div. 2) ABCDE
Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name A Team Olympiad standard input/outpu ...
随机推荐
- Plugin 'Lombok Plugin' is incompatible with this installation
作者:13 GitHub:https://github.com/ZHENFENG13 版权声明:本文为原创文章,未经允许不得转载. Installation Error Plugin 'Lombok ...
- JWT认证原理及使用
一.JWT原理: 参考文章:https://www.jianshu.com/p/180a870a308a 1.传统的登录方式: 浏览器输入用户名密码,服务端校验通过,根据用户信息生成一个token,将 ...
- Python入门-函数
定义:将一组语句的集合通过一个名字(函数名)封装起来,要先执行这个函数,只需要调用其函数名即可.特性:1.减少重复代码2.使程序变的可扩展3.使程序变的易维护 函数参数:形参和实参形参:位置参数,关键 ...
- 反射reflect
JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意方法和属性:这种动态获取信息以及动态调用对象方法的功能称为java语言的反射机制. ...
- pycharm设置pytest运行程序
- (Git 学习)Git SSH Key 创建步骤
首先感谢segmentfalut上的朋友对我帮助. 首先:查看你是否有../ssh 这个文件:怎么查看:找到你的git安装目录,在安装目录下查看是否./ssh,以我的为例: 在C盘/Users/11/ ...
- Python之自测代码标识__name__=='__main__'
__name__是python的默认的自测代码标识,其他文件导入该python文件时,不会执行这行代码以下部分. def yangfan(a): print('yangfan %s' %a) prin ...
- async并发处理
- C#复习笔记(3)--C#2:解决C#1的问题(结束C#2的内容:最后一些特性)
结束C#2的内容:最后一些新性 这是本章要讲的内容: 分部类型:可以在多个源文件中为 一个类型编写代码. 特别适用于部分代码是自动生成, 而其他部分的代码为手写的类型. 静态类:对工具类进行整理, 以 ...
- ToroiseGit提交代码上传到阿里云的Gitlab
https://blog.csdn.net/xiaomogg/article/details/51903004(copy) 准备 具体过程 准备 1.拥有一个GitHub账户 2.安装了Tortois ...