CF1039D You Are Given a Tree [根号分治]

考虑到 \(ans_{i+1} \leq ans_i\)
\(ans_{i} \leq \frac{n}{i}\)

然后胡乱分析一波,考虑根号分治,一部分暴力,一部分按根号的性质来,考虑到块取成 \(q\),一部分暴力的复杂度是 \(nq\),然后你发现剩下的值域仅仅是 \([0,\frac{n}{q}]\),由于这个部分你需要一个二分,所以复杂度带个 \(\log\),考虑到每个块,然后剩下的部分复杂度就是 \(\frac{n}{q} n \log n\),所以把两部分搞起来,理论复杂度是 \(nq+\frac{n}{q} n \log n\)

\(nq+\frac{n}{q} n \log n = n(q + \frac{n}{q}\log n)\),发现 \(q = \sqrt {n \log n}\) 的时候取到最优复杂度,显然实际上不是这样的,因为每个不可能都取到 \(\frac{n}{i}\),所以需要把块搞小一点233,200左右就够了

// powered by c++11
// by Isaunoya
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#pragma GCC optimize( \
	"inline,-fgcse,-fgcse-lm,-fipa-sra,-ftree-pre,-ftree-vrp,-fpeephole2,-ffast-math,-fsched-spec,unroll-loops,-falign-jumps,-falign-loops,-falign-labels,-fdevirtualize,-fcaller-saves,-fcrossjumping,-fthread-jumps,-funroll-loops,-freorder-blocks,-fschedule-insns,inline-functions,-ftree-tail-merge,-fschedule-insns2,-fstrict-aliasing,-fstrict-overflow,-falign-functions,-fcse-follow-jumps,-fsched-interblock,-fpartial-inlining,no-stack-protector,-freorder-functions,-findirect-inlining,-fhoist-adjacent-loads,-frerun-cse-after-loop,inline-small-functions,-finline-small-functions,-ftree-switch-conversion,-foptimize-sibling-calls,-fexpensive-optimizations,inline-functions-called-once,-fdelete-null-pointer-checks")
#include <bits/stdc++.h>
#define rep(i, x, y) for (register int i = (x); i <= (y); ++i)
#define Rep(i, x, y) for (register int i = (x); i >= (y); --i)
using namespace std;
using db = double;
using ll = long long;
using uint = unsigned int;
#define Tp template
using pii = pair<int, int>;
#define fir first
#define sec second
Tp<class T> void cmax(T& x, const T& y) {if (x < y) x = y;} Tp<class T> void cmin(T& x, const T& y) {if (x > y) x = y;}
#define all(v) v.begin(), v.end()
#define sz(v) ((int)v.size())
#define pb emplace_back
Tp<class T> void sort(vector<T>& v) { sort(all(v)); } Tp<class T> void reverse(vector<T>& v) { reverse(all(v)); }
Tp<class T> void unique(vector<T>& v) { sort(all(v)), v.erase(unique(all(v)), v.end()); }
const int SZ = 1 << 23 | 233;
struct FILEIN { char qwq[SZ], *S = qwq, *T = qwq, ch;
#ifdef __WIN64
#define GETC getchar
#else
  char GETC() { return (S == T) && (T = (S = qwq) + fread(qwq, 1, SZ, stdin), S == T) ? EOF : *S++; }
#endif
  FILEIN& operator>>(char& c) {while (isspace(c = GETC()));return *this;}
  FILEIN& operator>>(string& s) {while (isspace(ch = GETC())); s = ch;while (!isspace(ch = GETC())) s += ch;return *this;}
  Tp<class T> void read(T& x) { bool sign = 0;while ((ch = GETC()) < 48) sign ^= (ch == 45); x = (ch ^ 48);
    while ((ch = GETC()) > 47) x = (x << 1) + (x << 3) + (ch ^ 48); x = sign ? -x : x;
  }FILEIN& operator>>(int& x) { return read(x), *this; } FILEIN& operator>>(ll& x) { return read(x), *this; }
} in;
struct FILEOUT {const static int LIMIT = 1 << 22 ;char quq[SZ], ST[233];int sz, O;
  ~FILEOUT() { flush() ; }void flush() {fwrite(quq, 1, O, stdout); fflush(stdout);O = 0;}
  FILEOUT& operator<<(char c) {return quq[O++] = c, *this;}
  FILEOUT& operator<<(string str) {if (O > LIMIT) flush();for (char c : str) quq[O++] = c;return *this;}
  Tp<class T> void write(T x) {if (O > LIMIT) flush();if (x < 0) {quq[O++] = 45;x = -x;}
		do {ST[++sz] = x % 10 ^ 48;x /= 10;} while (x);while (sz) quq[O++] = ST[sz--];
  }FILEOUT& operator<<(int x) { return write(x), *this; } FILEOUT& operator<<(ll x) { return write(x), *this; }
} out;

int n ;
const int maxn = 1e5 + 51 ;
vector < int > g[maxn] ;
int rev[maxn] , fa[maxn] , idx = 0 ;

void dfs(int u , int f) {
	for(int v : g[u]) 
		if(v != f) dfs(v , u) ;
	fa[u] = f , rev[++ idx] = u ;
}

int f[maxn] ;
int ans[maxn] ;
int solve(int x) {
	if(ans[x]) return ans[x] ;
	int cnt = 0 ;
	rep(i , 1 , n) f[i] = 1 ;
	rep(i , 1 , n) {
		int qwq = rev[i] ;
		if(fa[qwq] && (~ f[fa[qwq]]) && (~ f[qwq])) {
			if(f[qwq] + f[fa[qwq]] >= x) ++ cnt , f[fa[qwq]] = -1 ;
			else cmax(f[fa[qwq]] , f[qwq] + 1) ;
		}
	}
	return ans[x] = cnt ;
}

signed main() {
  // code begin.
	in >> n ;
	rep(i , 2 , n) { int u , v ; in >> u >> v ; g[u].pb(v) , g[v].pb(u) ; }
	dfs(1 , 0) ; int q = min(n , 200) ;
	ans[1] = n ; rep(i , 2 , q) ans[i] = solve(i) ;
	int Ans ;
	for(int i = q + 1 ; i <= n ; i = Ans + 1) {
		int l = i , r = n ;
		int qwq = solve(l) ;
		while(l <= r) {
			int mid = l + r >> 1 ;
			if(solve(mid) ^ qwq) r = mid - 1 ;
			else l = (Ans = mid) + 1 ;
		}
		for(int p = i ; p <= Ans ; p ++) ans[p] = qwq ;
	}
	rep(i , 1 , n) out << ans[i] << '\n' ;
	return 0;
  // code end.
}
posted @ 2020-03-03 13:56  _Isaunoya  阅读(126)  评论(0编辑  收藏  举报