k-毛毛虫剖分

不是,正常的毛毛虫剖分有这么长吗?

bool CalcM1;
#include <bits/stdc++.h>
#define rep(i, a, b) for (long long i = (a), i##end = (b); i <= i##end; ++i)
#define per(i, a, b) for (long long i = (a), i##end = (b); i >= i##end; --i)
#define file(x, y) freopen(#x".in", "r", stdin); freopen(#y".out", "w", stdout);
#define fi first
#define se second
#define LOOK_TIME cerr << (1.0 * clock() / CLOCKS_PER_SEC) << endl;
using namespace std;
using LL = long long;
using iLL = __int128;
using PII = pair <int, int>;
const int N = 1e5 + 10;
const LL mod = 1e9 + 7;

template <typename T, typename T2> T chkmin(T &a, T2 b) {if (b < a) a = b; return a;}
template <typename T, typename T2> T chkmax(T &a, T2 b) {if (b > a) a = b; return a;}

template <typename T> T R(T &x) {
	x = 0; bool f = 0; char c = getchar(); while (!isdigit(c)) f = c == '-', c = getchar();
	while (isdigit(c)) x = x * 10 + c - '0', c = getchar(); return x = f ? -x : x;
}
template <typename T, typename... Tr> void R(T &x, Tr&... y) {R(x); R(y...);}

int n, m, K;
vector <int> e[N];

int fa[N], siz[N], son[N], dep[N], top[N], id[N], idx, iid[N];
vector <int> hp[N], k_son[N];
PII k_rng[N][5], sub_rng[N][2];
void dfs1(int x) {
	siz[x] = 1;
	for (int y : e[x]) if (y != fa[x]) {
		fa[y] = x; dep[y] = dep[x] + 1;
		dfs1(y); siz[x] += siz[y];
		if (siz[y] > siz[son[x]]) son[x] = y;
	}
}
void dfs2(int x, int tp) {
	top[x] = tp; hp[tp].push_back(x);
	sub_rng[x][0].fi = idx + 1;
	if (dep[x] - dep[top[x]] >= K) id[x] = ++idx;
	else {
		int u = x; rep(i, 1, K) u = fa[u];
		k_son[u].push_back(x);
	}
	if (son[x]) dfs2(son[x], tp);
	for (int y : e[x]) if (y != son[x] && y != fa[x]) dfs2(y, y);
	sub_rng[x][0].se = idx;
}
void dfs3(int x) {
	sub_rng[x][1].fi = idx + 1;
	for (int u : k_son[x]) id[u] = ++idx;
	if (son[x]) dfs3(son[x]);
	for (int y : e[x]) if (y != son[x] && y != fa[x]) dfs3(y);
	sub_rng[x][1].se = idx;
}

struct thirty_seven {
	int l, r, op;
	// 当需要链的方向时 op = 0 : l -> r;  op = 1 : r -> l
	thirty_seven() {}
	thirty_seven(int l, int r, int op = 0) : l(l), r(r), op(op) {}
	thirty_seven(PII p, int op = 0) : op(op) {l = p.fi; r = p.se;}
};

vector <thirty_seven> rng;
void get_line(int x, int y) {
	vector <thirty_seven> A, B;
	while (top[x] != top[y]) {
		if (dep[top[x]] > dep[top[y]]) {
			if (dep[x] - dep[top[x]] >= K)
				A.emplace_back(id[hp[top[x]][K]], id[x], 1), x = fa[hp[top[x]][K]];
			else A.emplace_back(id[x], id[x], 1), x = fa[x];
		} else {
			if (dep[y] - dep[top[y]] >= K)
				B.emplace_back(id[hp[top[y]][K]], id[y]), y = fa[hp[top[y]][K]];
			else B.emplace_back(id[y], id[y]), y = fa[y];
		}
	}
	if (dep[x] > dep[y]) {
		if (dep[y] - dep[top[y]] >= K)
			A.emplace_back(id[y], id[x], 1), x = fa[y];
		else if (dep[x] - dep[top[x]] >= K)
			A.emplace_back(id[hp[top[x]][K]], id[x], 1), x = fa[hp[top[x]][K]];
		while (dep[x] >= dep[y]) A.emplace_back(id[x], id[x], 1), x = fa[x];
	} else {
		if (dep[x] - dep[top[x]] >= K)
			B.emplace_back(id[x], id[y]), y = fa[x];
		else if (dep[y] - dep[top[y]] >= K)
			B.emplace_back(id[hp[top[y]][K]], id[y]), y = fa[hp[top[y]][K]];
		while (dep[y] >= dep[x]) B.emplace_back(id[y], id[y]), y = fa[y];
	}
	rng = A;
	while (B.size()) rng.emplace_back(B.back()), B.pop_back();
}

void get_sub(int x) {
	rng.clear();
	rep(i, 0, 1)
		if (sub_rng[x][i].fi <= sub_rng[x][i].se)
			rng.emplace_back(sub_rng[x][i]);
	rep(i, 0, K - 1)
		if (k_rng[x][i].fi <= k_rng[x][i].se)
			rng.emplace_back(k_rng[x][i]);
}

void push_k(int x, int k) {
	if (k < 0) return ;
	if (k_rng[x][k].fi <= k_rng[x][k].se) rng.emplace_back(k_rng[x][k]);
	if (hp[top[x]].size() > dep[x] - dep[top[x]] + k) {
		int u = hp[top[x]][dep[x] - dep[top[x]] + k];
		if (dep[u] - dep[top[x]] >= K) rng.emplace_back(id[u], id[u]);
	}
}
void get_k(int x, int k) {
	rng.clear();
	per(i, k, 0) {
		push_k(x, i); push_k(x, i - 1);
		x = fa[x];
	}
}

posted @ 2025-07-06 19:40  KinNa_Sky  阅读(130)  评论(2)    收藏  举报