高等袋鼠

校内赛 高等袋鼠

题目描述

柴老师在上高等代数,今天老师讲到了矩阵树定理:

定义 Kirchhoff 矩阵为度数矩阵减去邻接矩阵,那么生成树个数就是 Kirchhoff 矩阵删掉第 ii 行第 ii 列后行列式的值…

作为代数学家的柴老师当然会这些啦,所以太过无聊听着听着就进入了梦乡。

在梦里,两幅无向图纠缠在一起,生成树怎么也看不清…

具体的,两幅无向图均为简单连通图,即无重边无自环,两幅图共享了顶点,即两幅图都有 nn 个顶点,编号为$ 1,\cdots,n$ 。

柴老师洋洋洒洒从第一幅图中随机地选出了一棵生成树,但转头一看,图变成了第二幅图!

也就是说,此前选出的生成树中,所有不在第二幅图中的都消失了!

柴老师非常悲伤,想让你帮他算一下,所有可能的情况下他选出的生成树最后剩下的边数的和,答案对 998244353取模。

题目分析

trick:通过把矩阵里的元素变为1+wex{1+w_ex}的多项式可以求出TTreeeTwe\sum_{T\in Tree}\sum_{e\in T}w_e

是trick的板子

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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
//
// Created by mrx on 2022/10/4.
//
#include <vector>
#include <algorithm>
#include <iostream>
#include <array>
#include <functional>
#include <cassert>

using ll = long long;

template<typename T>
T inverse(T a, T b) {
T u = 0, v = 1;
while (a != 0) {
T t = b / a;
b -= t * a;
std::swap(a, b);
u -= t * v;
std::swap(u, v);
}
assert(b == 1);
return u;
}

template<typename T>
T power(T a, int b) {
T ans = 1;
for (; b; a *= a, b >>= 1) {
if (b & 1)ans *= a;
}
return ans;
}

template<int Mod>
class Modular {
public:
using Type = int;

template<typename U>
static Type norm(U& x) {
Type v;
if (-Mod <= x && x < Mod) v = static_cast<Type>(x);
else v = static_cast<Type>(x % Mod);
if (v < 0) v += Mod;
return v;
}

constexpr Modular() : value() {}

int val() const { return value; }

Modular inv() const {
return Modular(inverse(value, Mod));
}

template<typename U>
Modular(const U& x) {
value = norm(x);
}

const Type& operator ()() const {
return value;
}

template<typename U>
explicit operator U() const {
return static_cast<U>(value);
}

Modular& operator +=(const Modular& other) {
if ((value += other.value) >= Mod) value -= Mod;
return *this;
}

Modular& operator -=(
const Modular& other) {
if ((value -= other.value) < 0) value += Mod;
return *this;
}

template<typename U>
Modular& operator +=(const U& other) { return *this += Modular(other); }

template<typename U>
Modular& operator -=(const U& other) { return *this -= Modular(other); }

Modular& operator ++() { return *this += 1; }

Modular& operator --() { return *this -= 1; }

Modular operator ++(int) {
Modular result(*this);
*this += 1;
return result;
}

Modular operator --(int) {
Modular result(*this);
*this -= 1;
return result;
}

Modular operator -() const { return Modular(-value); }

template<class ISTREAM_TYPE>
friend ISTREAM_TYPE& operator >>(ISTREAM_TYPE& is, Modular& rhs) {
ll v;
is >> v;
rhs = Modular(v);
return is;
}

template<class OSTREAM_TYPE>
friend OSTREAM_TYPE& operator <<(OSTREAM_TYPE& os, const Modular& rhs) {
return os << rhs.val();
}

Modular& operator *=(const Modular& rhs) {
value = ll(value) * rhs.value % Mod;
return *this;
}

Modular& operator /=(const Modular& other) { return *this *= Modular(inverse(other.value, Mod)); }

friend const Type& abs(const Modular& x) { return x.value; }

friend bool operator ==(const Modular& lhs, const Modular& rhs) { return lhs.x == rhs.x; }

friend bool operator <(const Modular& lhs, const Modular& rhs) { return lhs.x < rhs.x; }


bool operator ==(const Modular& rhs) { return *this == rhs.value; }

template<typename U>
bool operator ==(U rhs) { return *this == Modular(rhs); }

template<typename U>
friend bool operator ==(U lhs, const Modular& rhs) { return Modular(lhs) == rhs; }

bool operator !=(const Modular& rhs) { return *this != rhs; }

template<typename U>
bool operator !=(U rhs) { return *this != rhs; }

template<typename U>
friend bool operator !=(U lhs, const Modular& rhs) { return lhs != rhs; }

bool operator <(const Modular& rhs) { return this->value < rhs.value; }

Modular operator +(const Modular& rhs) { return Modular(*this) += rhs; }

template<typename U>
Modular operator +(U rhs) { return Modular(*this) += rhs; }

template<typename U>
friend Modular operator +(U lhs, const Modular& rhs) { return Modular(lhs) += rhs; }

Modular operator -(const Modular& rhs) { return Modular(*this) -= rhs; }

template<typename U>
Modular operator -(U rhs) { return Modular(*this) -= rhs; }

template<typename U>
friend Modular operator -(U lhs, const Modular& rhs) { return Modular(lhs) -= rhs; }

Modular operator *(const Modular& rhs) { return Modular(*this) *= rhs; }

template<typename U>
Modular operator *(U rhs) { return Modular(*this) *= rhs; }

template<typename U>
friend Modular operator *(U lhs, const Modular& rhs) { return Modular(lhs) *= rhs; }

Modular operator /(const Modular& rhs) { return Modular(*this) /= rhs; }

template<typename U>
Modular operator /(U rhs) { return Modular(*this) /= rhs; }

template<typename U>
friend Modular operator /(U lhs, const Modular& rhs) { return Modular(lhs) /= rhs; }

private:
Type value;
};

constexpr int mod = 998244353;
using Z = Modular<mod>;

struct node {
Z x, y;

node(Z x, Z y) : x(x), y(y) {}

node() {}

friend node operator +(const node& u, const node& v) {
return node(u.x + v.x, u.y + v.y);
}

friend node operator -(const node& u, const node& v) {
return node(u.x - v.x, u.y - v.y);
}

friend node operator *(const node& u, const node& v) {
return node(u.x * v.y + u.y * v.x, u.y * v.y);
}

friend node operator /(const node& u, const node& v) {
Z inv = v.y.inv();
return node((u.x * v.y - u.y * v.x) * inv * inv, u.y * inv);
}
};

struct matrix {
std::vector<std::vector<node>> mat;

int n, m;

matrix(int n, int m) : mat(n, std::vector<node>(m, node(0, 0))), n(n), m(m) {}

std::vector<node>& operator [](int idx) {
return mat[idx];
}

Z gauss(int nn) {
node res(0, 1);
for (int i = 0; i < nn; ++i) {
int nxt = i;
for (; nxt < nn - 1; ++nxt)if (mat[nxt][i].y)break;
if (mat[nxt][i].y) {
std::swap(mat[nxt], mat[i]);
if (nxt != i)res = res * node(0, -1);
res = (res * mat[i][i]);
node inv = node(0, 1) / mat[i][i];
for (int j = i + 1; j < nn; ++j) {
node div = mat[j][i] * inv;
for (int k = i; k < nn; ++k) mat[j][k] = mat[j][k] - (div * mat[i][k]);
}
} else return 0;
}
return res.x;
}
};

int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);

int n;
std::cin >> n;
std::vector<std::vector<int>> a(n, std::vector<int>(n));
std::vector<std::vector<int>> b(n, std::vector<int>(n));
std::vector<int> deg1(n), deg2(n);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
char x;
std::cin >> x;
a[i][j] = x - '0';
}
}

for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
char x;
std::cin >> x;
b[i][j] = (x - '0');
}
}

matrix mat(n, n);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (a[i][j] == 1 && i >= j) {
mat[i][i] = mat[i][i] + node(b[i][j] == 1, 1);
mat[j][j] = mat[j][j] + node(b[i][j] == 1, 1);
mat[i][j] = mat[i][j] - node(b[i][j] == 1, 1);
mat[j][i] = mat[j][i] - node(b[i][j] == 1, 1);
}
}
}
std::cout << mat.gauss(n - 1) << '\n';

return 0;
}

高等袋鼠
https://mrxyan6.github.io/2022/10/13/jpt-1D/
作者
mrx
发布于
2022年10月13日
许可协议