QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#739870#8148. Middle Point GraphPalbudirWA 0ms3888kbC++202.5kb2024-11-12 23:35:462024-11-12 23:35:46

Judging History

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

  • [2024-11-12 23:35:46]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3888kb
  • [2024-11-12 23:35:46]
  • 提交

answer

#include <bits/stdc++.h>
#include <unordered_map>
using i64 = long long;
constexpr i64 mod = 1e9 + 7;
constexpr int N = 1e3;
i64 pw(i64 x, i64 k) {
    i64 z = 1;
    while (k) {
        if (k & 1) {
            z = z * x % mod;
        }
        x = x * x % mod;
        k >>= 1;
    }
    return z;
}
std::vector<i64> fact(N), invf(N);
i64 C(int n, int m) {
    if (m > n) {
        return 0;
    }
    return fact[n] * invf[m] % mod * invf[n - m] % mod;
}
void work() {
    int n, m;
    std::cin >> n >> m;
    std::vector<int> u(m), v(m), p(n), q(n);
    for (int i = 0; i < n; ++i) {
        p[i] = i;
    }
    std::vector<std::unordered_set<int>> to(n);
    for (int i = 0; i < m; ++i) {
        std::cin >> u[i] >> v[i];
        --u[i], --v[i];
        to[u[i]].insert(v[i]);
        to[v[i]].insert(u[i]);
    }
    sort(p.begin(), p.end(), [&](int x, int y) {
        return to[x].size() == to[y].size() ? x < y : to[x].size() < to[y].size();
    });
    for (int i = 0; i < n; ++i) {
        q[p[i]] = i;
    }

    i64 sum = 1ll * m * (n + m - 3) % mod, cnt = 0;
    for (int i = 0; i < n; ++i) {
        int k = to[i].size();
        sum = (sum + C(k, 2)) % mod;
    }
    std::unordered_set<int> set;
    for (int i = 0; i < m; ++i) {
        int x = u[i], y = v[i];
        if (to[x].size() > to[y].size()) {
            std::swap(x, y);
        }
        set.clear();
        for (auto z : to[x]) {
            if (to[y].count(z)) {
                set.insert(z);
            }
        }
        sum = (sum + C(set.size(), 2) + set.size()) % mod;
        for (auto z : set) {
            if (q[z] > q[x] || q[z] > q[y]) {
                continue;
            }
            for (auto w : to[z]) {
                if (q[w] > q[x] || q[w] > q[y]) {
                    continue;
                }
                if (set.count(w)) {
                    ++cnt;
                }
            }
        }
    }
    sum = (sum - cnt / 2 * 3 + mod) % mod;
    std::cout << sum << "\n";
    return;
}
void init() {
    fact[0] = 1;
    for (int i = 1; i < N; ++i) {
        fact[i] = fact[i - 1] * i % mod;
    }
    invf[N - 1] = pw(fact[N - 1], mod - 2);
    for (int i = N - 1; i; --i) {
        invf[i - 1] = invf[i] * i % mod;
    }
}
int main() {
    init();
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr), std::cout.tie(nullptr);
    int t = 1;
    std::cin >> t;
    while (t--) {
        work();
    }
    return 0;
}
/*


*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3648kb

input:

3

7 18
2 1
2 3
3 4
2 5
6 4
7 5
6 5
3 1
1 5
1 7
7 3
2 6
2 7
4 5
5 3
4 2
6 7
6 3

5 7
1 2
2 3
4 2
5 1
1 4
3 5
3 1

1 0

output:

593
88
0

result:

ok 3 number(s): "593 88 0"

Test #2:

score: 0
Accepted
time: 0ms
memory: 3888kb

input:

10

2 1
2 1

1 0

3 3
2 1
1 3
3 2

2 1
2 1

2 1
2 1

1 0

2 1
2 1

2 1
1 2

1 0

4 6
1 2
2 3
1 4
1 3
2 4
3 4

output:

0
0
15
0
0
0
0
0
0
69

result:

ok 10 numbers

Test #3:

score: 0
Accepted
time: 0ms
memory: 3584kb

input:

10

1 0

1 0

2 1
2 1

5 6
1 2
2 3
4 1
1 5
3 1
5 4

3 3
1 2
3 1
3 2

9 8
1 2
2 3
2 4
2 5
5 6
4 7
8 3
6 9

1 0

3 3
1 2
2 3
3 1

4 6
2 1
1 3
4 3
2 4
2 3
1 4

1 0

output:

0
0
0
64
15
122
0
15
69
0

result:

ok 10 numbers

Test #4:

score: 0
Accepted
time: 0ms
memory: 3548kb

input:

1

50 50
2 1
2 3
4 1
1 5
1 6
1 7
2 8
9 2
10 5
2 11
7 12
12 13
2 14
10 15
16 2
12 17
18 3
18 19
20 16
1 21
12 22
23 18
24 16
25 5
8 26
27 10
28 12
21 29
30 27
27 31
32 13
33 32
14 34
35 18
33 36
37 30
38 16
17 39
23 40
8 41
35 42
5 43
32 44
32 45
46 39
47 3
48 11
49 18
39 50
5 45

output:

4954

result:

ok 1 number(s): "4954"

Test #5:

score: -100
Wrong Answer
time: 0ms
memory: 3664kb

input:

1

30 50
2 1
2 3
3 4
5 4
2 6
7 5
4 8
3 9
9 10
8 11
4 12
13 6
14 8
15 12
16 10
5 17
2 18
19 16
6 20
8 21
22 21
7 23
15 24
16 25
13 26
20 27
16 28
29 19
15 30
18 10
17 7
16 13
18 19
15 20
18 28
28 15
21 28
21 3
12 23
4 16
27 26
19 9
2 28
10 7
25 9
5 22
15 22
4 27
10 24
16 12

output:

4014

result:

wrong answer 1st numbers differ - expected: '4025', found: '4014'