QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#258182#7009. Rikka with Intersections of PathssupepapupuAC ✓3810ms189204kbC++172.8kb2023-11-19 15:50:382023-11-19 15:50:38

Judging History

你现在查看的是最新测评结果

  • [2023-11-19 15:50:38]
  • 评测
  • 测评结果:AC
  • 用时:3810ms
  • 内存:189204kb
  • [2023-11-19 15:50:38]
  • 提交

answer

#include <bits/stdc++.h>

#define x first
#define y second
#define el '\n'
#define debug(x) cout << #x << ": " << x << el
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int N = 3e5 + 10, INF = 0x3f3f3f3f, mod = 1e9 + 7, MAXP = 18;

ll qmi(ll a, ll b) {
    ll res = 1;
    while (b) {
        if (b & 1) res = res * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return res;
}

ll inv(ll a) { return qmi(a, mod - 2); }

void inc(ll &a, ll b) {
    a += b;
    if (a >= mod) a -= mod;
}

void dec(ll &a, ll b) {
    a -= b;
    if (a < 0) a += mod;
}

ll add(ll a, ll b) {
    a += b;
    return a >= mod ? a - mod : a;
}

ll del(ll a, ll b) {
    a -= b;
    return a < 0 ? a + mod : a;
}

ll fac[N], ifac[N];

ll ans;
int n, m, k;
vector<int> G[N], ban[N];
int pa[N][MAXP], dep[N];
unordered_set<int> S[N];

void dfs0(int u, int fa) {
    pa[u][0] = fa, dep[u] = dep[fa] + 1;
    for (int i = 1; i < MAXP; ++i) pa[u][i] = pa[pa[u][i - 1]][i - 1];
    for (int v: G[u]) 
        if (v != fa) {
            dfs0(v, u);
        }
}

int lca(int a, int b) {
    if (dep[a] < dep[b]) swap(a, b);
    for (int i = MAXP - 1; i >= 0; --i) if (dep[pa[a][i]] >= dep[b]) a = pa[a][i];
    if (a == b) return a;
    for (int i = MAXP - 1; i >= 0; --i) 
        if (pa[a][i] != pa[b][i]) 
            a = pa[a][i], b = pa[b][i];
    return pa[a][0];
}

ll binom(ll a, ll b) {
    return fac[a] * ifac[b] % mod * ifac[a - b] % mod;
}

void dfs(int u, int fa) {
    int sn = u;
    for (int v: G[u]) {
        if (v == fa) continue;
        dfs(v, u);
        if (S[v].size() > S[sn].size()) sn = v;
    }
    S[u].swap(S[sn]);
    for (int v: G[u]) {
        if (v == fa) continue;
        for (int j: S[v]) S[u].insert(j);
        S[v].clear();
    }
    for (int i: ban[u]) {
        S[u].erase(i);
        if (S[u].size() >= k - 1) inc(ans, binom(S[u].size(), k - 1));
    }
}

void solve() {
    ans = 0;
    cin >> n >> m >> k;
    fac[0] = 1;
    for (int i = 1; i <= n; ++i) fac[i] = fac[i - 1] * i % mod;
    ifac[n] = inv(fac[n]);
    for (int i = n; i; --i) ifac[i - 1] = ifac[i] * i % mod;
    for (int i = 1; i <= n; ++i) {
        G[i].clear(), S[i].clear(), ban[i].clear();
    }
    for (int i = 1; i < n; ++i) {
        int a, b; cin >> a >> b;
        G[a].emplace_back(b), G[b].emplace_back(a);
    }
    dfs0(1, 0);
    for (int i = 0; i < m; ++i) {
        int a, b; cin >> a >> b;
        S[a].insert(i), S[b].insert(i);
        ban[lca(a, b)].emplace_back(i);
    }
    // for (int i = 1; i <= n; ++i) cout << sn[i] << " \n"[i == n];
    dfs(1, 0);
    cout << ans << el;
}

int main() {
    ios::sync_with_stdio(0); cin.tie(0);
    int tcase = 1;
    cin >> tcase;
    while (tcase--) solve();
}

这程序好像有点Bug,我给组数据试试?

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 7ms
memory: 39872kb

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: 3643ms
memory: 163884kb

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: 3810ms
memory: 189204kb

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