您的位置:首页 > 理论基础 > 计算机网络

【BZOJ4538】【HNOI2016】网络

2018-03-21 19:31 288 查看
【题目链接】
点击打开链接

【思路要点】
对树进行DFS序标号,令节点\(i\)的DFS序为\(dfn_i\),\(i\)子树内的节点DFS序范围为\([dfn_i,rit_i]\)。
对于路径\((a,b)\),节点\(x\)不影响\((a,b)\)的条件是满足以下之一:
1、\(dfn_{lca(a,b)}\in(dfn_x,rit_x]\)。
2、\(dfn_a\notin[dfn_x,rit_x]\)且\(dfn_b\notin[dfn_x,rit_x]\)。
1可以用线段树+堆维护,2可以用KDTree维护。
时间复杂度\(O(M*\sqrt{N})\),空间复杂度\(O(N+M)\)。
由于常数较大,BZOJ上这份代码会TLE,但该代码在DBZOJ上已经测试通过。
【代码】
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 200005;
const int MAXLOG = 20;
template <typename T> void chkmax(T &x, T y) {x = max(x, y); }
template <typename T> void chkmin(T &x, T y) {x = min(x, y); }
template <typename T> void read(T &x) {
x = 0; int f = 1;
char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
x *= f;
}
template <typename T> void write(T x) {
if (x < 0) x = -x, putchar('-');
if (x > 9) write(x / 10);
putchar(x % 10 + '0');
}
template <typename T> void writeln(T x) {
write(x);
puts("");
}
struct Heap {
priority_queue <int> Heap, Del;
void push(int x) {Heap.push(x); }
void pop(int x) {Del.push(x); }
int query() {
while (!Heap.empty() && !Del.empty() && Heap.top() == Del.top()) {
Heap.pop();
Del.pop();
}
if (Heap.empty()) return -1;
else return Heap.top();
}
};
struct SegmentTree {
struct Node {
int lc, rc;
int Max;
} a[MAXN * 2];
Heap b[MAXN];
int root, size, n;
void update(int root) {
a[root].Max = max(a[a[root].lc].Max, a[a[root].rc].Max);
}
void build(int &root, int l, int r) {
root = ++size;
a[root].Max = -1;
if (l == r) return;
int mid = (l + r) / 2;
build(a[root].lc, l, mid);
build(a[root].rc, mid + 1, r);
}
void init(int x) {
n = x;
root = size = 0;
build(root, 1, n);
}
void update(int root, int l, int r, int pos) {
if (l == r) {
a[root].Max = b[l].query();
return;
}
int mid = (l + r) / 2;
if (mid >= pos) update(a[root].lc, l, mid, pos);
else update(a[root].rc, mid + 1, r, pos);
update(root);
}
void push(int x, int val) {
b[x].push(val);
update(root, 1, n, x);
}
void pop(int x, int val) {
b[x].pop(val);
update(root, 1, n, x);
}
int query(int root, int l, int r, int ql, int qr) {
if (l == ql && r == qr) return a[root].Max;
int mid = (l + r) / 2, ans = -1;
if (mid >= ql) chkmax(ans, query(a[root].lc, l, mid, ql, min(mid, qr)));
if (mid + 1 <= qr) chkmax(ans, query(a[root].rc, mid + 1, r, max(mid + 1, ql), qr));
return ans;
}
int query(int l, int r) {
if (l > r) return -1;
else return query(root, 1, n, l, r);
}
} ST;
int n, m, timer;
int dfn[MAXN], rit[MAXN];
int depth[MAXN], father[MAXN][MAXLOG];
vector <int> a[MAXN];
void work(int pos, int fa) {
dfn[pos] = ++timer;
father[pos][0] = fa;
depth[pos] = depth[fa] + 1;
for (int i = 1; i < MAXLOG; i++)
father[pos][i] = father[father[pos][i - 1]][i - 1];
for (unsigned i = 0; i < a[pos].size(); i++)
if (a[pos][i] != fa) work(a[pos][i], pos);
rit[pos] = timer;
}
int lca(int x, int y) {
if (depth[x] < depth[y]) swap(x, y);
for (int i = MAXLOG - 1; i >= 0; i--)
if (depth[father[x][i]] >= depth[y]) x = father[x][i];
if (x == y) return x;
for (int i = MAXLOG - 1; i >= 0; i--)
if (father[x][i] != father[y][i]) {
x = father[x][i];
y = father[y][i];
}
return father[x][0];
}
struct point {int x, y; }; bool mode;
bool operator < (point a, point b) {
if (mode) {
if (a.x == b.x) return a.y < b.y;
else return a.x < b.x;
} else {
if (a.y == b.y) return a.x < b.x;
else return a.y < b.y;
}
}
struct info {point pos; int home; };
bool operator < (info a, info b) {return a.pos < b.pos; }
struct KDTree {
struct Node {
int lc, rc, fa;
int index, Max;
point pos, l, r;
} a[MAXN];
int root, tot, size, pos[MAXN];
info tmp[MAXN];
void updaterange(int root) {
a[root].l = a[root].r = a[root].pos;
int lc = a[root].lc, rc = a[root].rc;
if (lc) {
chkmin(a[root].l.x, a[lc].l.x);
chkmin(a[root].l.y, a[lc].l.y);
chkmax(a[root].r.x, a[lc].r.x);
chkmax(a[root].r.y, a[lc].r.y);
}
if (rc) {
chkmin(a[root].l.x, a[rc].l.x);
chkmin(a[root].l.y, a[rc].l.y);
chkmax(a[root].r.x, a[rc].r.x);
chkmax(a[root].r.y, a[rc].r.y);
}
}
void build(int &root, int l, int r, bool now) {
root = ++size;
a[root].index = a[root].Max = -1;
int mid = (l + r) / 2; mode = now;
nth_element(tmp + l, tmp + mid, tmp + r + 1);
a[root].pos = tmp[mid].pos;
pos[tmp[mid].home] = root;
if (mid > l) {
build(a[root].lc, l, mid - 1, now ^ true);
a[a[root].lc].fa = root;
}
if (mid < r) {
build(a[root].rc, mid + 1, r, now ^ true);
a[a[root].rc].fa = root;
}
updaterange(root);
}
void add(int x, int y, int home) {tmp[++tot] = (info) {(point) {x, y}, home}; }
void init() {
size = root = 0;
build(root, 1, tot, 0);
}
void updateMax(int root) {
a[root].Max = a[root].index;
if (a[root].lc) chkmax(a[root].Max, a[a[root].lc].Max);
if (a[root].rc) chkmax(a[root].Max, a[a[root].rc].Max);
}
void modify(int home, int val) {
int now = pos[home];
a[now].index = val;
updateMax(now);
while (now != root) {
now = a[now].fa;
updateMax(now);
}
}
int query(int root, point ql, point qr) {
if (root == 0 || a[root].Max == -1) return -1;
if (ql.x <= a[root].l.x && ql.y <= a[root].l.y && qr.x >= a[root].r.x && qr.y >= a[root].r.y) return a[root].Max;
if (ql.x > a[root].r.x || ql.y > a[root].r.y || qr.x < a[root].l.x || qr.y < a[root].l.y) return -1;
int ans = -1;
if (a[root].pos.x >= ql.x && a[root].pos.x <= qr.x && a[root].pos.y >= ql.y && a[root].pos.y <= qr.y) ans = a[root].index;
chkmax(ans, query(a[root].lc, ql, qr));
chkmax(ans, query(a[root].rc, ql, qr));
return ans;
}
int query(int lx, int rx, int ly, int ry) {
return query(root, (point) {lx, ly}, (point) {rx, ry});
}
} KDT;
int type[MAXN], x[MAXN], y[MAXN], z[MAXN];
int main() {
read(n), read(m);
for (int i = 1; i <= n - 1; i++) {
int x, y;
scanf("%d%d", &x, &y);
a[x].push_back(y);
a[y].push_back(x);
}
work(1, 0);
for (int i = 1; i <= m; i++) {
read(type[i]), read(x[i]);
if (type[i] == 0) {
read(y[i]), read(z[i]);
KDT.add(dfn[x[i]], dfn[y[i]], i);
}
}
KDT.init(); ST.init(n);
for (int i = 1; i <= m; i++) {
if (type[i] == 0) {
int Lca = lca(x[i], y[i]);
ST.push(dfn[Lca], z[i]);
KDT.modify(i, z[i]);
}
if (type[i] == 1) {
int j = x[i], Lca = lca(x[j], y[j]);
ST.pop(dfn[Lca], z[j]);
KDT.modify(j, -1);
}
if (type[i] == 2) {
int tmp = x[i];
int ans = ST.query(dfn[tmp] + 1, rit[tmp]);
chkmax(ans, KDT.query(1, dfn[tmp] - 1, rit[tmp] + 1, n));
chkmax(ans, KDT.query(1, dfn[tmp] - 1, 1, dfn[tmp] - 1));
chkmax(ans, KDT.query(rit[tmp] + 1, n, 1, dfn[tmp] - 1));
chkmax(ans, KDT.query(rit[tmp] + 1, n, rit[tmp] + 1, n));
writeln(ans);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: