隐藏
「bsoj5092」计数题 - Kruskal+trie树 | Bill Yang's Blog

路终会有尽头,但视野总能看到更远的地方。

0%

「bsoj5092」计数题 - Kruskal+trie树

题目大意

给定一个长度为$n$的数组$a[]$,有一幅完全图,满足$(u,v)$的边权为$a[u]\,xor\,a[v]$。
求边权和最小的生成树,你需要输出边权和(不取模)还有方案数对1e9+7取模的值


题目分析

本题有两个思路来源:

  • 题目是求异或的最小生成树,求完全图最小生成树时间复杂度为$O(n^2)$,而$n\le100000$,说明此题必须要利用异或的性质。
    异或有些什么性质?
    • 交换律
    • 结合律
    • 消去律
    • 按位运算
  • 很显然前三个性质对本题没有帮助,而题目也描述了$0\le a[i]\lt2^{30}$,很显然要运用到按位运算的性质。

  • 我们可以利用最小生成树从下至上重构出树,我们也可以使用Kruskal重构树从上至下来求出最小生成树。
    怎么求呢?
    若是一般的最小生成树,是无法作出这个逆过程的,原因是我们从上至下就需要分割集合,但是并不了解如何分割集合,若任意分割集合,显然集合之间可能有$1$条以上的边。

    但是因为是权值异或的最小生成树,故我们可以按照某一位为$0/1$来划分集合。

    这样我们可以保证集合之间仅有一条边,因为若有两条边,把其他的边替换为集合内部的边可以使得权值最小(最高位为1必定更大)。

有了这两个思路,我们大概可以定出算法步骤。
首先按照最高位划分集合,然后选出连接两个集合的最小边,递归划分后的集合。

如何选出连接两个集合的最小边?也就是说在两个集合中分别选出两个点使得异或值最小。
这显然是使用trie树解决的问题,按照任意一个集合建立trie树,枚举另一个集合的点在trie树中查询最小边即可。

似乎还需要一点卡常的小技巧。


代码

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
#include<algorithm>
#include<iostream>
#include<iomanip>
#include<cstring>
#include<cstdlib>
#include<vector>
#include<cstdio>
#include<cmath>
#include<queue>
using namespace std;
typedef long long LL;
inline const LL Get_Int() {
LL num=0,bj=1;
char x=getchar();
while(x<'0'||x>'9') {
if(x=='-')bj=-1;
x=getchar();
}
while(x>='0'&&x<='9') {
num=num*10+x-'0';
x=getchar();
}
return num*bj;
}
const int maxt=100005*30;
struct Tree {
int child[2],cnt;
void clear() {
child[0]=child[1]=cnt=0;
}
};
struct Trie {
Tree tree[maxt];
#define ch(x,i) tree[x].child[i]
int cnt,root;
void init() {
cnt=0;
tree[1].clear();
root=++cnt;
}
void insert(int num) {
int x=root;
for(int i=30; i>=0; i--) {
bool y=num&(1<<i);
if(ch(x,y)==0) {
ch(x,y)=++cnt;
tree[ch(x,y)].clear();
}
x=ch(x,y);
}
tree[x].cnt++;
}
pair<int,int>find(int num) {
int x=root,ans=0;
for(int i=30; i>=0; i--) {
bool y=num&(1<<i),chose;
if(ch(x,y))chose=y;
else chose=!y;
ans|=chose<<i;
x=ch(x,chose);
}
return make_pair(ans^num,tree[x].cnt);
}
} trie;
const int maxn=100005;
const LL mod=1e9+7;
LL n,a[maxn],l[maxn],r[maxn],ans=0,ans2=1;
LL Quick_Pow(LL a,LL b) {
LL ans=1;
while(b) {
if(b&1)ans=ans*a%mod;
a=a*a%mod;
b>>=1;
}
return ans;
}
void Binary(int Left,int Right,int depth) {
if(Left>=Right)return;
if(depth<0) {
ans2=ans2*Quick_Pow(Right-Left+1,Right-Left-1)%mod;
return;
}
int lpos=0,rpos=0;
for(int i=Left; i<=Right; i++)
if(a[i]&(1<<depth))l[++lpos]=a[i];
else r[++rpos]=a[i];
for(int i=1; i<=lpos; i++)a[Left+i-1]=l[i];
for(int i=1; i<=rpos; i++)a[Left+lpos+i-1]=r[i];
if(lpos&&rpos) {
LL sum=0x7fffffff/2,cnt=0;
trie.init();
for(int i=1; i<=lpos; i++)trie.insert(l[i]);
for(int i=1; i<=rpos; i++) {
pair<int,int>tmp=trie.find(r[i]);
if(tmp.first<sum) {
sum=tmp.first;
cnt=tmp.second;
} else if(tmp.first==sum)cnt+=tmp.second;
}
ans+=sum;
ans2=ans2*cnt%mod;
}
Binary(Left,Left+lpos-1,depth-1);
Binary(Left+lpos,Right,depth-1);
}
int main() {
n=Get_Int();
for(int i=1; i<=n; i++)a[i]=Get_Int();
Binary(1,n,30);
printf("%lld\n%lld\n",ans,ans2);
return 0;
}
姥爷们赏瓶冰阔落吧~