QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#276784 | #7009. Rikka with Intersections of Paths | ucup-team1209# | AC ✓ | 1014ms | 66860kb | C++20 | 2.5kb | 2023-12-06 10:44:36 | 2023-12-06 10:44:38 |
Judging History
answer
#include <bits/stdc++.h>
#define cs const
#define pb push_back
using namespace std;
cs int N = 3e5 + 5;
cs int mod = 1e9 + 7;
int add(int a, int b) {
return a + b >= mod ? a + b - mod : a + b;
}
int dec(int a, int b) {
return a - b < 0 ? a - b + mod : a - b;
}
int mul(int a, int b) {
return 1ll * a * b % mod;
}
int ksm(int a, int b) {
int c = 1;
for(; b; b >>= 1, a = mul(a, a))
if(b & 1) c = mul(c, a);
return c;
}
int n, m, k, T;
vector <int> e[N];
int mn[20][N], dfn[N], indx, fa[N];
int a[N], b[N];
int fc[N], ifc[N];
int C(int n, int m) {
if(n < 0 || m < 0 || n < m) return 0;
return mul(fc[n], mul(ifc[n - m], ifc[m]));
}
void fc_init(int n) {
fc[0] = ifc[0] = 1;
for(int i = 1; i <= n; i++) fc[i] = mul(fc[i - 1], i);
ifc[n] = ksm(fc[n], mod - 2);
for(int i = n- 1; i; i--) ifc[i] = mul(ifc[i + 1], i + 1);
}
int ck(int x, int y) {
return dfn[x] < dfn[y] ? x : y;
}
void dfs(int u, int f) {
mn[0][indx] = f;
fa[u] = f;
dfn[u] = ++ indx;
for(int v : e[u]) if(v != f) {
dfs(v, u);
}
}
int lca(int u, int v) {
if(u == v) return u;
u = dfn[u], v = dfn[v];
if(u > v) swap(u, v);
int x = __lg(v - u);
return ck(mn[x][u], mn[x][v - (1 << x)]);
}
int ans = 0;
void dfs2(int u, int f) {
for(int v : e[u])
if(v != f) {
dfs2(v, u);
a[u] += a[v];
b[u] += b[v];
}
if(f) {
ans = dec(ans, C(b[u], k));
}
ans = add(ans, C(a[u], k));
}
int main() {
#ifdef zqj
freopen("1.in","r",stdin);
#endif
ios :: sync_with_stdio(0), cin.tie(0);
cin >> T;
while(T--) {
cin >> n >> m >> k;
fc_init(m);
for(int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
e[u].pb(v);
e[v].pb(u);
}
dfs(1, 0);
for(int i = 1; i <= 19; i++)
for(int j = 1; j + (1 << i) <= n; j++)
mn[i][j] = ck(mn[i - 1][j], mn[i - 1][j + (1 << (i - 1))]);
for(int i = 1; i <= m; i++) {
int u, v;
cin >> u >> v;
int l = lca(u, v);
++ a[u], ++ a[v];
-- a[l], -- a[fa[l]];
++ b[u], ++ b[v], b[l] -= 2;
}
ans = 0;
dfs2(1, 0);
cout << ans << '\n';
for(int i= 0; i <= n; i++) {
a[i] = b[i] = 0;
e[i].clear();
dfn[i] = 0;
}
indx = 0;
}
}
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 10516kb
input:
1 3 6 2 1 2 1 3 1 1 2 2 3 3 1 2 1 3 2 3
output:
10
result:
ok single line: '10'
Test #2:
score: 0
Accepted
time: 1014ms
memory: 50292kb
input:
108 2000 2000 52 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12 12 13 13 14 14 15 15 16 16 17 17 18 18 19 19 20 20 21 21 22 22 23 23 24 24 25 25 26 26 27 27 28 28 29 29 30 30 31 31 32 32 33 33 34 34 35 35 36 36 37 37 38 38 39 39 40 40 41 41 42 42 43 43 44 44 45 45 46 46 47 47 48 48 49 49 50 50 51 ...
output:
887021006 410694359 325103243 670233279 72813801 947351730 883070706 311794222 998954559 996232156 569968667 505502006 778426774 220584560 246359125 260714580 11417533 351222718 490813635 444958907 207271238 791034394 734465853 472937949 826448869 646757384 776063725 826971663 959125943 459469914 30...
result:
ok 108 lines
Test #3:
score: 0
Accepted
time: 910ms
memory: 66860kb
input:
6 300000 300000 43 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12 12 13 13 14 14 15 15 16 16 17 17 18 18 19 19 20 20 21 21 22 22 23 23 24 24 25 25 26 26 27 27 28 28 29 29 30 30 31 31 32 32 33 33 34 34 35 35 36 36 37 37 38 38 39 39 40 40 41 41 42 42 43 43 44 44 45 45 46 46 47 47 48 48 49 49 50 50 5...
output:
690220121 895677607 370155943 510259168 589689421 548994023
result:
ok 6 lines