clj在某场hihoCoder比赛中的一道题,表示clj的数学题实在6,这道图论貌似还算可以。。。
题目链接:http://hihocoder.com/problemset/problem/1167
由于是中文题目,题意不再赘述。
对于任意两条小精灵的活动路径a和b,二者相交的判断条件为b的两个端点的LCA在a的路径上;那么我们可以首先将每个活动路径端点的LCA离线预处理出来,对每个节点LCA值+1。
然后以某个节点(我选择的是节点1)为根进行深搜,算出一条从节点1到节点x的LCA值和,那么任意路径a(假设其两端点分别是A和B)上的节点个数就是sum[A] + sum[B] - 2 * sum[LCA(A,B)]。
最后,对于某些点,如果它是不止一条路径的LCA,那么我们只需要对最终答案乘以C(LCAnum, 2)的组合数就好。
【PS:clj给出的题解中,采用了点分治+LCA的方式,虽然看懂了题意,但是表示对递归分治之后的路径,如何求出其上的LCAnum,并没有多好的想法,还望巨巨能指点一下,Thx~】
AC代码:
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
|
#include <cstdio>
#include <iostream>
#include <cstring>
using
namespace
std;
typedef
long
long
LL;
#define MAXN 100010
struct
Edge {
int
to, next;
} edge[MAXN << 1];
struct
Node {
int
to, next, num;
} Query[MAXN << 1];
struct
node {
int
u, v, lca;
} input[MAXN];
int
totEdge, totQuery, n, m;
int
headEdge[MAXN], headQuery[MAXN];
int
ancestor[MAXN], father[MAXN], LCAnum[MAXN], sum[MAXN];
bool
vis[MAXN];
void
addEdge(
int
from,
int
to) {
edge[totEdge].to = to;
edge[totEdge].next = headEdge[from];
headEdge[from] = totEdge++;
}
void
addQuery(
int
from,
int
to,
int
x) {
Query[totQuery].to = to;
Query[totQuery].num = x;
Query[totQuery].next = headQuery[from];
headQuery[from] = totQuery++;
}
void
init() {
memset
(headEdge, -1,
sizeof
(headEdge));
memset
(headQuery, -1,
sizeof
(headQuery));
memset
(father, -1,
sizeof
(father));
memset
(vis,
false
,
sizeof
(vis));
memset
(sum, 0,
sizeof
(sum));
memset
(LCAnum, 0,
sizeof
(LCAnum));
totEdge = totQuery = 0;
}
int
find_set(
int
x) {
if
(x == father[x])
return
x;
else
return
father[x] = find_set(father[x]);
}
void
union_set(
int
x,
int
y) {
x = find_set(x); y = find_set(y);
if
(x != y) father[y] = x;
}
void
Tarjan(
int
u) {
father[u] = u;
for
(
int
i = headEdge[u]; i != -1; i = edge[i].next) {
int
v = edge[i].to;
if
(father[v] != -1)
continue
;
Tarjan(v);
union_set(u, v);
}
for
(
int
i = headQuery[u]; i != -1; i = Query[i].next) {
int
v = Query[i].to;
if
(father[v] == -1)
continue
;
input[Query[i].num].lca = find_set(v);
}
}
void
DFS(
int
u,
int
pre) {
vis[u] = 1;
sum[u] = sum[pre] + LCAnum[u];
for
(
int
i = headEdge[u]; i != -1; i = edge[i].next) {
int
v = edge[i].to;
if
(vis[v])
continue
;
DFS(v, u);
}
}
int
main() {
init();
scanf
(
"%d%d"
, &n, &m);
for
(
int
i = 0; i < n - 1; i++) {
int
a, b;
scanf
(
"%d%d"
, &a, &b);
addEdge(a, b); addEdge(b, a);
}
for
(
int
i = 0; i < m; i++) {
int
a, b;
scanf
(
"%d%d"
, &a, &b);
input[i].u = a, input[i].v = b;
addQuery(a, b, i); addQuery(b, a, i);
}
Tarjan(1);
for
(
int
i = 0; i < m; i++)
LCAnum[input[i].lca]++;
DFS(1, 0);
LL ans = 0;
for
(
int
i = 0; i < m; i++) {
ans += (sum[input[i].u] + sum[input[i].v] - 2 * sum[input[i].lca]);
}
for
(
int
i = 1; i <= n; i++) {
ans += (LL)LCAnum[i] * (LCAnum[i] - 1) / 2;
}
printf
(
"%lld\n"
, ans);
return
0;
}
|