0%

HDU 6203 ping ping ping LCA + 贪心

有一个\(N+1\)个结点的树\(T\)。 现在有\(Q\)个信息,每条信息为\(q:(u,v)\),表示\(u\)\(v\)的路径不通。 问树\(T\)至少被破坏多少个节点才能达到当前这种局面。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <bits/stdc++.h>
#define DBG(x) cerr << #x << " = " << x << endl

using namespace std;
typedef long long LL;

#define mt(a, b) memset(a, b, sizeof(a))

struct LCA { ///最近公共祖先 build O(V*logV) query O(1)
typedef int typec; ///边权的类型
static const int MV = 100000 + 16; ///点的个数
static const int MR = MV << 1; ///rmq 的个数

int Index, H[MV], dex[MR], a[MR], b[MR], F[MR], ra[MR][20], rb[MR][20];
typec dist[MV];
struct G {
struct E {
int v, next;
typec w;
} e[MV << 1];
int le, head[MV];
void init(int n) {
le = 0;
for(int i = 0; i <= n; i++) head[i] = -1;
}
void add(int u, int v, typec w) {
e[le].v = v;
e[le].w = w;
e[le].next = head[u];
head[u] = le++;
}
} g;
void init_dex() {
dex[0] = -1;
for(int i = 1; i < MR; i++) {
dex[i] = dex[i >> 1] + 1;
}
}
void dfs(int u, int fa, int level) {
a[Index] = u;
b[Index++] = level;
F[u] = fa;
for(int i = g.head[u]; ~i; i = g.e[i].next) {
int v = g.e[i].v;
if(v == fa) continue;
dist[v] = dist[u] + g.e[i].w;
dfs(v, u, level + 1);
a[Index] = u;
b[Index++] = level;
}
}

LCA() {
init_dex();
};
void init(int n) {
g.init(n);
}

void add(int u, int v, typec w) {
g.add(u, v, w);
g.add(v, u, w);
}
void build(int root) { ///传入树根
Index = 0;
dist[root] = 0;
dfs(root, -1, 0);
mt(H, -1);
mt(rb, 0x3f);
for(int i = 0; i < Index; i++) {
int tmp = a[i];
if(H[tmp] == -1) H[tmp] = i;
rb[i][0] = b[i];
ra[i][0] = a[i];
}
for(int i = 1; (1 << i) <= Index; i++) {
for(int j = 0; j + (1 << i) < Index; j++) {
int next = j + (1 << (i - 1));
if(rb[j][i - 1] <= rb[next][i - 1]) {
rb[j][i] = rb[j][i - 1];
ra[j][i] = ra[j][i - 1];
continue;
}
rb[j][i] = rb[next][i - 1];
ra[j][i] = ra[next][i - 1];
}
}
}
int getlca(int l, int r) { ///查最近公共祖先
l = H[l];
r = H[r];
if(l > r) swap(l, r);
int p = dex[r - l + 1];
r -= (1 << p) - 1;
return rb[l][p] <= rb[r][p] ? ra[l][p] : ra[r][p];
}
typec getdist(int id) { ///查根到点最短距离
return dist[id];
}
typec getdist(int l, int r) { ///查两点最短距离
return dist[l] + dist[r] - 2 * dist[getlca(l, r)];
}
} yoshiko;

int n, q;

struct Query {
int u, v, lca, d;
Query(int u, int v, int lca, int d): u(u), v(v), lca(lca), d(d) {}
Query() {}
bool operator < (const Query &rhs) const {
return d > rhs.d;
}
};

bool vis[100000 + 16];

void dfs(int u) {
vis[u] = true;
for(int i = yoshiko.g.head[u]; ~i; i = yoshiko.g.e[i].next) {
int v = yoshiko.g.e[i].v;
if(v != yoshiko.F[u] && !vis[v]) dfs(v);
}
}

int main(int argc, char **argv) {
while(~scanf("%d", &n)) {
++n; yoshiko.init(n);
for(int i = 1; i < n; ++i) {
int u, v; scanf("%d%d", &u, &v);
yoshiko.add(u, v, 1);
}
yoshiko.build(0);
vector<Query> Q;
scanf("%d", &q);
while(q--) {
int u, v; scanf("%d%d", &u, &v);
int lca = yoshiko.getlca(u, v);
int dist = yoshiko.getdist(lca);
Q.push_back(Query(u, v, lca, dist));
}

sort(Q.begin(), Q.end());

int ans = 0;
memset(vis, false, sizeof(vis));
for(auto &query : Q) {
if(!vis[query.u] && !vis[query.v]) {
++ans;
dfs(query.lca);
}
}
printf("%d\n", ans);
}
return 0;
}