- 11.12
删树直接Ac
- @ 2024-11-17 11:04:03
#pragma GCC optimize(3, "unroll-loops", "no-stack-protector")
#define atsum(l, r) accumulate(l, r, 0)
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/hash_policy.hpp>
using namespace std;
using ll = long long;
using ull = unsigned long long;
constexpr int inf = 0x3f3f3f3f;
constexpr ll INF = 0x3f3f3f3f3f3f3f3f;
template <typename T>
inline void chkmin(T &x, T y) { x = min(x, y); }
template <typename T>
inline void chkmax(T &x, T y) { x = max(x, y); }
namespace FastIO
{
// ------------------------------
#define IN_HAS_NEG
#define OUT_HAS_NEG
#define CHK_EOF
#define DISABLE_MMAP
// ------------------------------
#if __cplusplus < 201400
#error Please use C++14 or higher.
#endif
#if __cplusplus > 201700
#define INLINE_V inline
#else
#define INLINE_V
#endif
#if ( defined(LOCAL) || defined (_WIN32) ) && !defined(DISABLE_MMAP)
#define DISABLE_MMAP
#endif
#ifndef DISABLE_MMAP
#include<sys/mman.h>
#endif
#ifdef LOCAL
inline char gc() { return getchar(); }
inline void pc(char c) { putchar(c); }
#else
#ifdef DISABLE_MMAP
INLINE_V constexpr int _READ_SIZE = 1 << 18;
INLINE_V static char _read_buffer[_READ_SIZE], *_read_ptr = nullptr, *_read_ptr_end = nullptr;
inline char gc()
{
if ( __builtin_expect(_read_ptr == _read_ptr_end, false) )
{
_read_ptr = _read_buffer;
_read_ptr_end = _read_buffer + fread(_read_buffer, 1, _READ_SIZE, stdin);
#ifdef CHK_EOF
if ( __builtin_expect(_read_ptr == _read_ptr_end, false) ) return EOF;
#endif
}
return *_read_ptr++;
}
#else
INLINE_V static const char *_read_ptr = (const char *)mmap(nullptr, INT_MAX, 1, 2, 0, 0);
inline char gc() { return *_read_ptr++; }
#endif
INLINE_V constexpr int _WRITE_SIZE = 1 << 18;
INLINE_V static char _write_buffer[_WRITE_SIZE], *_write_ptr = _write_buffer;
inline void pc(char c)
{
*_write_ptr++ = c;
if ( __builtin_expect(_write_buffer + _WRITE_SIZE == _write_ptr, false) )
{
fwrite(_write_buffer, 1, _write_ptr - _write_buffer, stdout);
_write_ptr = _write_buffer;
}
}
INLINE_V struct _auto_flush
{
~_auto_flush() { fwrite(_write_buffer, 1, _write_ptr - _write_buffer, stdout); }
} _auto_flush;
#endif
#ifdef CHK_EOF
inline bool _isdigit(char c) { return ( c & 16 ) && c != EOF; }
inline bool _isgraph(char c) { return c > 32 && c != EOF; }
#else
inline bool _isdigit(char c) { return c & 16; }
inline bool _isgraph(char c) { return c > 32; }
#endif
template < class T >
INLINE_V constexpr bool _is_integer = numeric_limits < T >::is_integer;
template < class T >
INLINE_V constexpr bool _is_signed = numeric_limits < T >::is_signed;
template < class T >
INLINE_V constexpr bool _is_unsigned = _is_integer < T > && !_is_signed < T >;
template <> INLINE_V constexpr bool _is_integer < __int128 > = true;
template <> INLINE_V constexpr bool _is_integer < __uint128_t > = true;
template <> INLINE_V constexpr bool _is_signed < __int128 > = true;
template <> INLINE_V constexpr bool _is_unsigned < __uint128_t > = true;
#undef INLINE_V
inline void read(char &c) { do c = gc(); while ( !_isgraph(c) ); }
inline void read_cstr(char *s)
{
char c = gc(); while ( !_isgraph(c) ) c = gc();
while ( _isgraph(c) ) *s++ = c, c = gc();
*s = 0;
}
inline void read(string &s)
{
char c = gc(); s.clear(); while ( !_isgraph(c) ) c = gc();
while ( _isgraph(c) ) s.push_back(c), c = gc();
}
#ifdef IN_HAS_NEG
template < class T, enable_if_t < _is_signed < T >, int > = 0 >
inline void read(T &x)
{
char c = gc(); bool f = true; x = 0;
while ( !_isdigit(c) ) { if ( c == 45 ) f = false; c = gc(); }
if ( f ) while ( _isdigit(c) ) x = x * 10 + ( c & 15 ), c = gc();
else while ( _isdigit(c) ) x = x * 10 - ( c & 15 ), c = gc();
}
template < class T, enable_if_t < _is_unsigned < T >, int > = 0 >
#else
template < class T, enable_if_t < _is_integer < T >, int > = 0 >
#endif
inline void read(T &x)
{
char c = gc(); while ( !_isdigit(c) ) c = gc();
x = 0; while ( _isdigit(c) ) x = x * 10 + ( c & 15 ), c = gc();
}
inline void write(char c) { pc(c); }
inline void write_cstr(const char *s) { while ( *s ) pc(*s++); }
inline void write(const string &s) { for ( char c : s ) pc(c); }
#ifdef OUT_HAS_NEG
template < class T, enable_if_t < _is_signed < T >, int > = 0 >
inline void write(T x)
{
char buffer[numeric_limits < T >::digits10 + 1]; int digits = 0;
if ( x >= 0 ) do buffer[digits++] = ( x % 10 ) | 48, x /= 10; while ( x );
else { pc(45); do buffer[digits++] = -( x % 10 ) | 48, x /= 10; while ( x ); }
while ( digits ) pc(buffer[--digits]);
}
template < class T, enable_if_t < _is_unsigned < T >, int > = 0 >
#else
template < class T, enable_if_t < _is_integer < T >, int > = 0 >
#endif
inline void write(T x)
{
char buffer[numeric_limits < T >::digits10 + 1]; int digits = 0;
do buffer[digits++] = ( x % 10 ) | 48, x /= 10; while ( x );
while ( digits ) pc(buffer[--digits]);
}
template < int N > struct _tuple_io_helper
{
template < class ...T >
static inline void _read(tuple < T... > &x)
{ _tuple_io_helper < N - 1 >::_read(x), read(get < N - 1 > (x)); }
template < class ...T >
static inline void _write(const tuple < T... > &x)
{ _tuple_io_helper < N - 1 >::_write(x), pc(32), write(get < N - 1 > (x)); }
};
template <> struct _tuple_io_helper < 1 >
{
template < class ...T >
static inline void _read(tuple < T... > &x) { read(get < 0 > (x)); }
template < class ...T >
static inline void _write(const tuple < T... > &x) { write(get < 0 > (x)); }
};
template < class ...T >
inline void read(tuple < T... > &x) { _tuple_io_helper < sizeof...(T) >::_read(x); }
template < class ...T >
inline void write(const tuple < T... > &x) { _tuple_io_helper < sizeof...(T) >::_write(x); }
template < class T1, class T2 >
inline void read(pair < T1, T2 > &x) { read(x.first), read(x.second); }
template < class T1, class T2 >
inline void write(const pair < T1, T2 > &x) { write(x.first), pc(32), write(x.second); }
template < class T1, class ...T2 >
inline void read(T1 &x, T2 &...y) { read(x), read(y...); }
template < class ...T >
inline void read_cstr(char *x, T *...y) { read_cstr(x), read_cstr(y...); }
template < class T1, class ...T2 >
inline void write(const T1 &x, const T2 &...y) { write(x), write(y...); }
template < class ...T >
inline void write_cstr(const char *x, const T *...y) { write_cstr(x), write_cstr(y...); }
template < class T >
inline void print(const T &x) { write(x); }
inline void print_cstr(const char *x) { write_cstr(x); }
template < class T1, class ...T2 >
inline void print(const T1 &x, const T2 &...y) { print(x), pc(32), print(y...); }
template < class ...T >
inline void print_cstr(const char *x, const T *...y) { print_cstr(x), pc(32), print_cstr(y...); }
inline void println() { pc(10); }
inline void println_cstr() { pc(10); }
template < class ...T >
inline void println(const T &...x) { print(x...), pc(10); }
template < class ...T >
inline void println_cstr(const T *...x) { print_cstr(x...), pc(10); }
}
using namespace FastIO;
template <typename T>
inline void clear(T &x) {
T y;
swap(x, y);
}
const int N = 2e5 + 10;
struct Seg {
unsigned w[N << 2], len[N << 2], tag[N << 2];
bitset<N << 2> used;
int st[N << 2], top;
inline void maketag(int u, unsigned v) {
if (!used.test(u)) used.set(u), st[++top] = u;
w[u] += len[u] * v, tag[u] += v;
}
inline void pushdown(int u) {
if (tag[u]) maketag(u << 1, tag[u]), maketag(u << 1 | 1, tag[u]), tag[u] = 0;
}
inline void update(int u, int L, int R, int l, int r, unsigned v) {
if (!used.test(u)) used.set(u), st[++top] = u;
if (L >= l && R <= r) return maketag(u, v);
if (L > r || R < l) return;
int M = L + R >> 1;
pushdown(u);
update(u << 1, L, M, l, r, v), update(u << 1 | 1, M + 1, R, l, r, v);
w[u] = w[u << 1] + w[u << 1 | 1];
len[u] = len[u << 1] + len[u << 1 | 1];
}
inline void ins(int u, int L, int R, int x, unsigned v) {
if (!used.test(u)) used.set(u), st[++top] = u;
if (L == R) return len[u] += v, void(w[u] += tag[u] * v);
pushdown(u);
int M = L + R >> 1;
if (x <= M )ins(u << 1, L, M, x, v);
else ins(u << 1 | 1, M + 1, R, x, v);
w[u] = w[u << 1] + w[u << 1 | 1];
len[u] = len[u << 1] + len[u << 1 | 1];
}
inline unsigned query(int u, int L, int R, int l, int r) {
if (!used.test(u)) used.set(u), st[++top] = u;
if (L >= l && R <= r) return w[u];
if (L > r || R < l) return 0;
pushdown(u);
int M = L + R >> 1;
return query(u << 1, L, M, l, r) + query(u << 1 | 1, M + 1, R, l, r);
}
inline void clr() {
while (top) w[st[top]] = tag[st[top]] = len[st[top]] = 0, used.reset(st[top--]);
}
} seg;
struct Tree{
basic_string<int> vec[N];
int top[N], son[N], siz[N], dfn[N], dfncnt, fa[N], dep[N];
void dfs(int u) {
siz[u] = 1;
for (int v : vec[u])
if (v != fa[u]) {
fa[v] = u, dep[v] = dep[u] + 1, dfs(v), siz[u] += siz[v];
if (siz[v] > siz[son[u]]) son[u] = v;
}
}
void dfs2(int u) {
dfn[u] = ++dfncnt;
if (son[u]) top[son[u]] = top[u], dfs2(son[u]);
for (int v : vec[u])
if (v != fa[u] && v != son[u]) dfs2(top[v] = v);
}
inline int lca(int u, int v) {
while (top[u] != top[v]) dep[top[u]] > dep[top[v]] ? u = fa[top[u]] : v = fa[top[v]];
return dep[u] < dep[v] ? u : v;
}
inline int dist(int u, int v) { return dep[u] + dep[v] - 2 * dep[lca(u, v)]; }
inline void addedge(int u, int v) {
vec[u] += v;
vec[v] += u;
}
} mn, mx;
int fa[N], n;
bitset<N> qwq;
void dfsa(int u) {
qwq.set(u);
for (int v : mn.vec[u])
if (v != mn.fa[u]) dfsa(v);
}
int dfsb(int u) {
int ans = qwq.test(u);
for (int v : mx.vec[u])
if (v != mx.fa[u]) ans += dfsb(v);
return ans;
}
basic_string<int> G[N];
inline int find(int u) { return fa[u] == u ? u : (fa[u] = find(fa[u])); }
int T;
unsigned ans;
inline void update(int u) {
seg.ins(1, 1, n, mx.dfn[u], u);
int c = u;
while (c) {
seg.update(1, 1, n, mx.dfn[mx.top[c]], mx.dfn[c], 1);
c = mx.fa[mx.top[c]];
}
}
inline void ins(int u) {
update(u);
for (int v : mn.vec[u])
if (v != mn.fa[u]) ins(v);
}
inline unsigned query(int u) {
unsigned ans = 0;
while (u) ans += seg.query(1, 1, n, mx.dfn[mx.top[u]], mx.dfn[u]), u = mx.fa[mx.top[u]];
return ans;
}
inline void dsu(int u) {
for (int v : mn.vec[u])
if (v != mn.fa[u] && v != mn.son[u]) dsu(v), seg.clr();
if (mn.son[u]) dsu(mn.son[u]);
update(u);
for (int v : mn.vec[u])
if (v != mn.fa[u] && v != mn.son[u]) ins(v);
ans += u * query(u);
}
inline void work() {
ans = 0;
for (int i = 1; i <= n; i++) mn.son[i] = mx.son[i] = 0, clear(mx.vec[i]), clear(mn.vec[i]);
for (int i = 1; i <= n; i++) mn.fa[i] = mx.fa[i] = 0;
mn.dfncnt = mx.dfncnt = 0;
read(n);
for (int i = 1; i <= n; i++) clear(G[i]);
for (int i = 1; i < n; i++) {
int u, v;
read(u, v);
G[u] += v, G[v] += u;
}
for (int i = 1; i <= n; i++) fa[i] = i;
for (int i = 1; i <= n; i++)
for (int v : G[i])
if (v < i) mx.addedge(find(v), i), fa[find(v)] = i;
for (int i = 1; i <= n; i++) fa[i] = i;
for (int i = n; i; i--)
for (int v : G[i])
if (v > i) mn.addedge(find(v), i), fa[find(v)] = i;
mn.dfs(1), mx.dfs(n);
mn.dfs2(mn.top[1] = 1), mx.dfs2(mx.top[n] = n);
seg.clr();
dsu(1);
println(ans);
}
int main() {
freopen("delete.in","r",stdin);
freopen("delete.out","w",stdout);
read(T);
while (T--) work();
return 0;
}
1 条评论
-
zbr LV 1 @ 2024-11-26 15:00:17
%%%
- 1
京公网安备 11011102002149号