题意
给你两个树,求一个最大集合,要求集合内的任意两个点在第一个树上,比如是祖先关系,在第二棵树,不能存在祖先关系
分析
某人吐槽我的题解写的太简单了,然后我觉得。。。承认错误死不悔改 这道题如果分开来求的话,一个是求树的直径,一个是树上DP,都比较简单,如果当他们在一起应该怎么处理呢,我们考虑维护两个指针,因为在树上两点之间的路径肯定是维护,所以肯定是符合第一个条件的,这样我们就要考虑怎么去处理第二个条件了 考虑在第二棵树上求
d
f
s
dfs
dfs序,我们每次一个点加入进来,就把这个点及其子树所在的区间+1,因为这个点被选中代表这个子树都不能被选中,最后判断合法性,如果存在大于1的节点,说明不符合条件,上面的指针需要向下移动
代码
#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define debug(x) cout<<#x<<":"<<x<<endl;
#define dl(x) printf("%lld\n",x);
#define di(x) printf("%d\n",x);
#define _CRT_SECURE_NO_WARNINGS
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef vector<int> VI;
const int INF = 0x3f3f3f3f;
const int N = 3e5 + 10, M = N * 2;
const ll mod = 1000000007;
const double eps = 1e-9;
const double PI = acos(-1);
template<typename T>inline void read(T &a) {
char c = getchar(); T x = 0, f = 1; while (!isdigit(c)) {if (c == '-')f = -1; c = getchar();}
while (isdigit(c)) {x = (x << 1) + (x << 3) + c - '0'; c = getchar();} a = f * x;
}
int gcd(int a, int b) {return (b > 0) ? gcd(b, a % b) : a;}
int h1[N], e1[M], ne1[M], idx1;
int h2[N], e2[M], ne2[M], idx2;
int in[N], out[N], cnt;
deque<int>q1;
int n, ans;
struct Node {
int l, r;
int x, add;
} tr[N * 4];
void init() {
q1.clear();
for (int i = 1; i <= n; i++) h1[i] = h2[i] = -1,in[i] = 0,out[i] = 0;
idx1 = idx2 = 0;
cnt = ans = 0;
}
void push(int u) {
tr[u].x = max(tr[u << 1].x , tr[u << 1 | 1].x);
}
void down(int u) {
if (tr[u].add) {
int k = tr[u].add;
tr[u].add = 0;
tr[u << 1].x += k;
tr[u << 1].add += k;
tr[u << 1 | 1].x += k;
tr[u << 1 | 1].add += k;
}
}
void build(int u, int l, int r) {
tr[u].l = l, tr[u].r = r,tr[u].add = 0;
if (l == r) {
tr[u].x = 0, tr[u].add = 0;
return;
}
int mid = (l + r) >> 1;
build(u << 1, l, mid), build(u << 1 | 1, mid + 1, r);
push(u);
}
void modify(int u, int l, int r, int k) {
if (tr[u].l >= l && tr[u].r <= r) {
tr[u].x += k;
tr[u].add += k;
return;
}
down(u);
int mid = (tr[u].l + tr[u].r) >> 1;
if (l <= mid) modify(u << 1, l, r, k);
if (r > mid) modify(u << 1 | 1, l, r, k);
push(u);
}
void add1(int a, int b) {
ne1[idx1] = h1[a], e1[idx1] = b, h1[a] = idx1++;
}
void add2(int a, int b) {
ne2[idx2] = h2[a], e2[idx2] = b, h2[a] = idx2++;
}
void dfs(int u, int fa) {
in[u] = ++cnt;
for (int i = h2[u]; ~i; i = ne2[i]) {
int j = e2[i];
if (j == fa) continue;
dfs(j, u);
}
out[u] = cnt;
}
void dfs1(int x, int fa) {
deque<int>q2;
q1.push_back(x);
modify(1,in[x], out[x], 1);
while (tr[1].x >= 2) {
modify(1, in[q1.front()], out[q1.front()], -1), q2.push_back(q1.front()), q1.pop_front();
}
ans = max(ans, (int)q1.size());
for (int i = h1[x]; i != -1; i = ne1[i]) {
int j = e1[i];
if(j == fa) continue;
dfs1(j,x);
}
while (q2.size()) {
modify(1,in[q2.back()], out[q2.back()], 1);
q1.push_front(q2.back());
q2.pop_back();
}
modify(1,in[q1.back()], out[q1.back()], -1);
q1.pop_back();
}
int main() {
int T;
read(T);
while (T--) {
read(n);
init();
for (int i = 1; i < n; i++) {
int a, b;
read(a), read(b);
add1(a, b), add1(b, a);
}
for (int i = 1; i < n; i++) {
int a, b;
read(a), read(b);
add2(a, b), add2(b, a);
}
build(1,1,n);
dfs(1, -1);
dfs1(1, -1);
di(ans);
}
return 0;
}
|