Language: C++

#include <bits/stdc++.h>
using namespace std;

// #define DEBUG

struct T {
	int key, val;
	int siz, fix;
	T *l, *r;
	T(int k, int v) {
		key = k; val = v;
		siz = 1; fix = rand();
		l = r = NULL;
	}
	void upd() {
		siz = 1;
		if (l) siz += l -> siz;
		if (r) siz += r -> siz;
	}
};

void split(T *x, int v, T *&a, T *&b) {
	if (!x) {a = b = NULL; return ;}
	if (x -> key <= v) {
		a = x; split(x -> r, v, a -> r, b);
		a -> upd();
	} else {
		b = x; split(x -> l, v, a, b -> l);
		b -> upd();
	}
}

T *merge(T *a, T *b) {
	if (!a || !b) return a ? a : b;
	if (a -> fix < b -> fix) {
		a -> r = merge(a -> r, b);
		a -> upd(); return a;
	} else {
		b -> l = merge(a, b -> l);
		b -> upd(); return b;
	}
}

void _combine(T *&a, T *b) {
	if (!b) return ;
	_combine(a, b -> l);
	_combine(a, b -> r);
	T *l, *m, *r;
	split(a, b -> key, l, r);
	split(l, b -> key - 1, l, m);
	if (!m) m = new T(b -> key, b -> val);
	else m -> val += b -> val;
	a = merge(merge(a, m, b));
}

T *combine(T *a, T *b) {
	if (!a || !b) return a ? a : b;
	if (a -> siz < b -> siz) swap(a, b);
	_combine(a, b);
	return a;
}

const int N = 2e5 + 5;
int n;
vector <int> g[N];
T *f[N];
int fmx[N], fmx2[N];
vector <int> mx;
int ans[N];

void prt(T *x) {
	if (!x) return ;
	prt(x -> l);
	cerr << "(" << x -> key << ", " << x -> val << ") ";
	prt(x -> r);
}

void dfs(int u, int p) {
	f[u] = NULL;
	fmx[u] = 1;
	for (int i = 0; i < g[u].size(); ++ i) {
		int v = g[u][i];
		if (v == p) continue;
		mx.push_back(max(mx[mx.size() - 1], u));
		dfs(v, u);
		mx.resize(mx.size() - 1);
		fmx[u] += fmx2[v];
		f[u] = combine(f[u], f[v]);
	}
	T *I = NULL;
	for (int i = 0; i < n; ++ i) I = merge(I, new T(i, 1));
	f[u] = combine(f[u], I);
	T *a, *b;
	split(f[u], u, a, b);
	f[u] = b;
	int mx2 = mx.size() >= 2 ? mx[mx.size() - 2] : 0;
	T *l, *m, *r;
	split(f[u], mx2, l, r);
	split(l, mx2 - 1, l, m);
	if (m) fmx2[u] = m -> val;
	f[u] = merge(merge(l, m), r);
	// cerr << u << ": " << fmx[u] << ", " << fmx2[u] << endl;
	// prt(f[u]); cerr << endl;
}

void dfsans(int u, int p) {
	if (u) ans[u] = ans[p] - fmx2[u] + fmx[u];
	for (int i = 0; i < g[u].size(); ++ i) {
		int v = g[u][i];
		if (v == p) continue;
		dfsans(v, u);
	}
}

int main() {
	scanf("%d", &n);
	for (int i = 1; i < n; ++ i) {
		int u, v;
		scanf("%d %d", &u, &v); -- u, -- v;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	mx.push_back(0);
	dfs(0, -1);
	dfsans(0, -1);
	for (int i = 1; i < n; ++ i)
		printf("%d%c", ans[i], " \n"[i + 1 == n]);
	return 0;
}


Compiler output:
a.cc: In function 'void _combine(T*&, T*)':
a.cc:53:24: error: no matching function for call to 'merge(T*&, T*&, T*&)'
   53 |         a = merge(merge(a, m, b));
      |                   ~~~~~^~~~~~~~~
In file included from /usr/include/c++/14/algorithm:61,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algo.h:4857:5: note: candidate: 'template<class _IIter1, class _IIter2, class _OIter> _OIter std::merge(_IIter1, _IIter1, _IIter2, _IIter2, _OIter)'
 4857 |     merge(_InputIterator1 __first1, _InputIterator1 __last1,
      |     ^~~~~
/usr/include/c++/14/bits/stl_algo.h:4857:5: note:   candidate expects 5 arguments, 3 provided
/usr/include/c++/14/bits/stl_algo.h:4908:5: note: candidate: 'template<class _IIter1, class _IIter2, class _OIter, class _Compare> _OIter std::merge(_IIter1, _IIter1, _IIter2, _IIter2, _OIter, _Compare)'
 4908 |     merge(_InputIterator1 __first1, _InputIterator1 __last1,
      |     ^~~~~
/usr/include/c++/14/bits/stl_algo.h:4908:5: note:   candidate expects 6 arguments, 3 provided
In file included from /usr/include/c++/14/algorithm:86:
/usr/include/c++/14/pstl/glue_algorithm_defs.h:412:1: note: candidate: 'template<class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _ForwardIterator, class _Compare> __pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2> std::merge(_ExecutionPolicy&&, _ForwardIterator1, _ForwardIterator1, _ForwardIterator2, _ForwardIterator2, _ForwardIterator, _Compare)'
  412 | merge(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2,
      | ^~~~~
/usr/include/c++/14/pstl/glue_algorithm_defs.h:412:1: note:   candidate expects 7 arguments, 3 provided
/usr/include/c++/14/pstl/glue_algorithm_defs.h:417:1: note: candidate: 'template<class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _ForwardIterator> __pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2> std::merge(_ExecutionPolicy&&, _ForwardIterator1, _ForwardIterator1, _ForwardIterator2, _ForwardIterator2, _ForwardIterator)'
  417 | merge(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2,
      | ^~~~~
/usr/include/c++/14/pstl/glue_algorithm_defs.h:417:1: note:   candidate expects 6 arguments, 3 provided
a.cc:33:4: note: candidate: 'T* merge(T*, T*)'
   33 | T *merge(T *a, T *b) {
      |    ^~~~~
a.cc:33:4: note:   candidate expects 2 arguments, 3 provided