题意

nn 个点的有向图,求出以每一个点为根的外向生成树的数量,答案对 109+710^9+7 取模。

n500n\le 500

做法

我们先求出外向生成树的 Laplacian 矩阵 Lin=Din(G)A(G)L^{\text{in}} =D^{\text{in}}(G)-A(G)

所以对于任意的根 kk ,根据矩阵树定理可得:

tleaf(G,k)=detMi,it^{\text{leaf}}(G,k)=\text{det} M_{i,i}

其中 Mi,iM_{i,i} 表示 LinL^{\text{in}} 去掉 iiii 列所得的余子式。

发现暴力做这个事情复杂度是 O(n4)O(n^4) 的,并不能接受,考虑如何快速处理。

先引入 伴随矩阵 的概念:

AA 关于第 ii 行第 jj 列的余子式(记作 Mi,jM_{i,j} )是去掉 AA 的第 ii 行第 jj 列之后得到的 (n1)×(n1)(n − 1)\times(n − 1) 矩阵的行列式。

定义:AA 关于第 ii 行第 jj 列的代数余子式是:

Cij=(1)i+jMij{\displaystyle \mathbf {C} _{ij}=(-1)^{i+j}\mathbf {M} _{ij}}

定义:AA 的余子矩阵是一个 n×nn\times n 的矩阵 CC ,使得其第 ii 行第 jj 列的元素是 AA 关于第 ii 行第 jj 列的代数余子式。

引入以上的概念后,可以定义:矩阵 AA 的伴随矩阵是 AA 的余子矩阵的转置矩阵:

adj(A)=CT{\displaystyle \mathrm {adj} (\mathbf {A} )=\mathbf {C} ^{T}}

对于 rank(Lin)\text{rank}(L^{\text{in}}) 进行讨论:

  • rank(Lin)=n\text{rank}(L^{\text{in}})=n ,那么 LinL^{\text{in}} 可逆,由伴随矩阵的性质可得 adj(A)=det(A)A1\mathrm{adj} (A)=\text{det}(A) A^{-1},那么只需要求一次逆再求一个行列式即可

  • rank(Lin)<n1\text{rank}(L^{\text{in}})\lt n-1 ,那么由定义可以得到 i,j\forall i,j , adj(A)i,j=0\text{adj} (A)_{i,j}=0

  • rank(Lin)=n1\text{rank}(L^{\text{in}})=n-1,可以得到 rank(adj(Lin))=1\text{rank}( \text{adj}(L^{\text{in}}))=1。实际上在 Laplacian 矩阵中有更好的性质,在 LinL^{\text{in}} 中每一行都为 00 ,可以得到在 adj(Lin)\text{adj} (L^{\text{in}}) 中每一列都是相同的。

所以只需要在对 LinL^{\text{in}} 消元的过程中找到出现自由变元的行,然后删去这一行,枚举删去某一列,注意到删去行列后的矩阵为上海森堡矩阵,所以可以 O(n2)O(n^2) 求其行列式,最后的复杂度是 O(n3)O(n^3)

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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#include <bits/stdc++.h>

using namespace std;


using int64 = long long;

template<int MOD>
struct ModInt {
int x;

ModInt() : x(0) {}
ModInt(int64 y) : x(y >= 0 ? y % MOD : (MOD - (-y) % MOD) % MOD) {}

inline int inc(const int &x) {
return x >= MOD ? x - MOD : x;
}
inline int dec(const int &x) {
return x < 0 ? x + MOD : x;
}

ModInt &operator+= (const ModInt &p) {
x = inc(x + p.x);
return *this;
}
ModInt &operator-= (const ModInt &p) {
x = dec(x - p.x);
return *this;
}

ModInt &operator*= (const ModInt &p) {
x = (int)(1ll * x * p.x % MOD);
return *this;
}
ModInt &operator/= (const ModInt &p) {
*this *= p.inverse();
return *this;
}

ModInt operator-() const { return ModInt(-x); }

friend ModInt operator + (const ModInt& lhs, const ModInt& rhs) {
return ModInt(lhs) += rhs;
}
friend ModInt operator - (const ModInt& lhs, const ModInt& rhs) {
return ModInt(lhs) -= rhs;
}
friend ModInt operator * (const ModInt& lhs, const ModInt& rhs) {
return ModInt(lhs) *= rhs;
}
friend ModInt operator / (const ModInt& lhs, const ModInt& rhs) {
return ModInt(lhs) /= rhs;
}

bool operator == (const ModInt &p) const { return x == p.x; }
bool operator != (const ModInt &p) const { return x != p.x; }

ModInt inverse() const {
int a = x, b = MOD, u = 1, v = 0, t;
while(b > 0) {
t = a / b;
swap(a -= t * b, b);
swap(u -= t * v, v);
}
return ModInt(u);
}

ModInt pow(int64 n) const {
ModInt ret(1), mul(x);
while(n > 0) {
if(n & 1) ret *= mul;
mul *= mul;
n >>= 1;
}
return ret;
}

friend ostream &operator<<(ostream &os, const ModInt &p) {
return os << p.x;
}

friend istream &operator>>(istream &is, ModInt &a) {
int64 t;
is >> t;
a = ModInt<MOD>(t);
return (is);
}
static int get_mod() { return MOD; }
};

const int MOD = 1e9 + 7;

using MODint = ModInt<MOD>;
using Varray = vector<vector<MODint>>;

struct Matrix {
int n , m;
Varray v;

void print() const {
cout << "DEBUG:" << endl;
for (int i = 1 ; i <= n ; i++) {
for (int j = 1 ; j <= m ; j++) {
cout << v[i][j] << ' ';
}
cout << endl;
}
cout << endl;
}

Matrix(int _n , int _m) {
n = _n , m = _m;
v = Varray(n + 1 , vector<MODint>(m + 1 , 0));
}

Matrix getT() const {
Matrix ret(m , n);

for (int i = 1 ; i <= n ; i++) {
for (int j = 1 ; j <= m ; j++) {
ret.v[j][i] = v[i][j];
}
}
return ret;
}

Matrix operator *(const MODint &k) const {
Matrix ret = *this;
for (int i = 1 ; i <= n ; i++) {
for (int j = 1 ; j <= m ; j++) {
ret.v[i][j] *= k;
}
}
return ret;
}

Matrix operator *(const Matrix &rhs) const {
Matrix ret(n , rhs.m);
for (int i = 1 ; i <= n ; i++) {
for (int k = 1 ; k <= m ; k++) {
if (v[i][k] == 0) continue;
for (int j = 1 ; j <= rhs.m ; j++) {
ret.v[i][j] += v[i][k] * rhs.v[k][j];
}
}
}
return ret;
}

pair<pair<int , int> , Matrix> gauss(int &swap_cnt) const {
int r = 0 , p = 0;
Matrix cur = *this;
for (int i = 1 ; i <= n ; i++) {
for (int j = i ; j <= n ; j++) {
if (cur.v[j][i] != 0) {
if (i != j) {
swap(cur.v[i] , cur.v[j]);
swap_cnt++;
}
break;
}
}
if (cur.v[i][i] == 0) {
p = i;
continue;
}
r++;
for (int j = i + 1 ; j <= n ; j++) {
if (cur.v[j][i] == 0) {
continue;
}
MODint d = cur.v[j][i] / cur.v[i][i];
for (int k = i ; k <= m ; k++) {
cur.v[j][k] -= d * cur.v[i][k];
}
}
}
return {{r , p} , cur};
}

MODint getval() const {
MODint ans = 1;
Matrix cur(n , m);
for (int i = 1 ; i <= n ; i++) {
for (int j = 1 ; j <= m ; j++) {
cur.v[i][j] = v[i][j];
}
}
for (int i = 1 ; i <= n ; i++) {
for (int j = i ; j <= n ; j++) {
if (cur.v[j][i] != 0) {
swap(cur.v[i] , cur.v[j]);
ans *= -1;
break;
}
}
for (int j = i + 1 ; j <= n ; j++) {
if (cur.v[j][i] == 0) {
continue;
}
MODint d = cur.v[j][i] / cur.v[i][i];
for (int k = i ; k <= n ; k++) {
cur.v[j][k] -= d * cur.v[i][k];
}
}
}
for (int i = 1 ; i <= n ; i++) {
ans *= cur.v[i][i];
}
return ans;
}

Matrix getinv() const {
Matrix cur(n , n + n);
for (int i = 1 ; i <= n ; i++) {
for (int j = 1 ; j <= n ; j++) {
cur.v[i][j] = v[i][j];
}
cur.v[i][i + n] = 1;
}
for (int i = 1 ; i <= n ; i++) {
for (int j = i ; j <= n ; j++) {
if (cur.v[j][i] != 0) {
swap(cur.v[j] , cur.v[i]);
break;
}
}
MODint d = 1 / cur.v[i][i];
for (int j = i ; j <= n + n ; j++) {
cur.v[i][j] *= d;
}
for (int j = 1 ; j <= n ; j++) {
if (i == j) continue;
MODint d = cur.v[j][i];
for (int k = i ; k <= n + n ; k++) {
cur.v[j][k] -= d * cur.v[i][k];
}
}
}
Matrix ret(n , n);
for (int i = 1 ; i <= n ; i++) {
for (int j = 1 ; j <= n ; j++) {
ret.v[i][j] = cur.v[i][j + n];
}
}
return ret;
}

Matrix getR(int r) const {
Matrix ret(n - 1 , m);
int R = 0 , C = 0;
for (int i = 1 ; i <= n ; i++) {
if (i == r) continue;
R++ , C = 0;
for (int j = 1 ; j <= n ; j++) {
C++;
ret.v[R][C] = v[i][j];
}
}
return ret;
}

Matrix getRC(int r , int c) const {
Matrix ret(n - 1 , n - 1);
int R = 0 , C = 0;
for (int i = 1 ; i <= n ; i++) {
if (i == r) continue;
R++ , C = 0;
for (int j = 1 ; j <= n ; j++) {
if (j == c) continue;
C++;
ret.v[R][C] = v[i][j];
}
}
return ret;
}

Matrix getC(int c) const {
Matrix ret(n , m - 1);
for (int i = 1 ; i <= n ; i++) {
int C = 0;
for (int j = 1 ; j <= m ; j++) {
if (j == c) continue;
C++;
ret.v[i][C] = v[i][j];
}
}
return ret;
}

MODint getval_H() const { // Hessenberg
MODint ans = 1;
Matrix cur = *this;
for (int i = 1 ; i + 1 <= n ; i++) {
if (cur.v[i + 1][i] == 0) continue;
if (cur.v[i][i] == 0) {
swap(cur.v[i] , cur.v[i + 1]);
ans *= -1;
}
MODint d = cur.v[i + 1][i] / cur.v[i][i];
for (int j = i ; j <= n ; j++) {
cur.v[i + 1][j] -= d * cur.v[i][j];
}
}
for (int i = 1 ; i <= n ; i++) {
ans *= cur.v[i][i];
}
return ans;
}

Matrix getadj(int type = 0) {
int swap_cnt = 0;
auto P = gauss(swap_cnt);

int r = P.first.first , p = P.first.second;
auto mat = P.second;

if (r < n - 1) {
return Matrix(n , n);
} else if (r == n - 1) {
Matrix ans(n , n);
Matrix t = getR(p);

int swap_cnt2 = 0;
auto PI = t.gauss(swap_cnt2);
auto M = PI.second;
for (int i = 1 ; i <= p ; i++) {
auto M1 = M.getC(i);
ans.v[p][i] = M1.getval_H();
if ((p + i) % 2 == 1) {
ans.v[p][i] = -ans.v[p][i];
}
if (swap_cnt2 % 2 == 1) {
ans.v[p][i] = -ans.v[p][i];
}
}

if (type == 0) { // Laplacian
for (int i = p - 1 ; i >= 1 ; i--) {
for (int j = 1 ; j <= n ; j++) {
ans.v[i][j] = ans.v[i + 1][j];
}
}
ans = ans.getT();
} else {
}
return ans;
} else { // r == n
return getinv() * getval();
}
}
};


int main() {
cin.tie(nullptr)->sync_with_stdio(0);

int T;
cin >> T;

while (T--) {
int n , m;
cin >> n >> m;

vector<vector<int>> a(n + 1 , std::vector<int>(n + 1));
vector<vector<int>> d(n + 1 , std::vector<int>(n + 1));

for (int i = 1 ; i <= m ; i++) {
int u , v;
cin >> u >> v;

a[u][v]++;
d[v][v]++;
}

Matrix cur(n , n);
for (int i = 1 ; i <= n ; i++) {
for (int j = 1 ; j <= n ; j++) {
cur.v[i][j] = d[i][j] - a[i][j];
}
}

if (n == 1) {
cout << "1\n";
continue;
}

auto ans = cur.getadj(0);
for (int i = 1 ; i <= n ; i++) {
cout << ans.v[i][i] << " \n"[i == n];
}
}
return 0;
}