QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#403097#559. 原力GoatGirl98100 ✓40ms12628kbC++143.2kb2024-05-01 20:49:222024-05-01 20:49:22

Judging History

This is the latest submission verdict.

  • [2024-05-01 20:49:22]
  • Judged
  • Verdict: 100
  • Time: 40ms
  • Memory: 12628kb
  • [2024-05-01 20:49:22]
  • Submitted

answer

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <vector>
#include <array>
#include <algorithm>
#include <unordered_map>
#define getchar getchar_unlocked
#define putchar putchar_unlocked
int rd_op()
{
    char c = getchar();
    while (c < 'A' || c > 'Z')
        c = getchar();
    return (c == 'R') ? 0 : (c == 'G') ? 1 : 2;
}
int rd()
{
    int k = 0, f = 1;
    char c = getchar();
    while (c < '0' || c > '9')
    {
        if (c == '-')
            f = 0;
        c = getchar();
    }
    while (c >= '0' && c <= '9')
    {
        k = (k << 1) + (k << 3) + (c ^ 48);
        c = getchar();
    }
    return f ? k : -k;
}
void wr(int x)
{
    if (x < 0)
        putchar('-'), x = -x;
    if (x > 9)
        wr(x / 10);
    putchar(x % 10 + '0');
}
using namespace std;
typedef long long i64;
const int N = 50010;
const int M = 100010;
const int mod = 1000000007;
int add(int a, int b) { return (a + b >= mod) ? (a + b - mod) : (a + b); }
int mul(int a, int b) { return 1ll * a * b % mod; }

struct edge
{
    int u, v;
    edge(int _u = 0, int _v = 0) : u(_u), v(_v) { if (u > v) std::swap(u, v); }
    bool operator==(const edge& o) const { return (u == o.u) && (v == o.v); }
};
struct edge_hash
{
    size_t operator()(const edge& a) const { return hash<int>()(a.u) * 611953 + hash<int>()(a.v) * 7919; }
};
using unmap = std::unordered_map<edge, std::array<int, 3>, edge_hash>;
const std::array<int, 3> init = {0, 0, 0};
unmap mp;

int n, m, ans;

void add_edge(int u, int v, int w, int ty)
{
    edge info = edge(u, v);
    if (mp.find(info) == mp.end())
        mp[info] = init;
    int ori = mp[info][ty];
    mp[info][ty] = add(ori, w);
}

int from[M << 1], to[M << 1], ecnt;
int deg[N];
std::vector<int> g[N];
int vis[N];

void build_g()
{
    for (unmap::iterator it = mp.begin(); it != mp.end(); ++it)
        from[++ecnt] = it->first.u, to[ecnt] = it->first.v, ++deg[from[ecnt]], ++deg[to[ecnt]];
    for (int i = 1; i <= ecnt; ++i)
    {
        if (deg[from[i]] > deg[to[i]] || (deg[from[i]] == deg[to[i]] && from[i] > to[i]))
            swap(from[i], to[i]);
        g[from[i]].push_back(to[i]);
    }
}

void enumerate_triangles()
{
    for (int i = 1; i <= n; ++i)
    {
        for (int j : g[i])
            vis[j] = 1;
        for (int j : g[i])
            for (int k : g[j])
                if (vis[k])
                {
                    std::array<int, 3> ij = mp[edge(i, j)], jk = mp[edge(j, k)], ki = mp[edge(k, i)];
                    ans = add(ans, mul(ij[0], mul(jk[1], ki[2])));
                    ans = add(ans, mul(ij[0], mul(jk[2], ki[1])));
                    ans = add(ans, mul(ij[1], mul(jk[0], ki[2])));
                    ans = add(ans, mul(ij[1], mul(jk[2], ki[0])));
                    ans = add(ans, mul(ij[2], mul(jk[0], ki[1])));
                    ans = add(ans, mul(ij[2], mul(jk[1], ki[0])));
                }
        for (int j : g[i])
            vis[j] = 0;
    }
}

int main()
{
    n = rd(), m = rd();
    for (int i = 1; i <= m; ++i)
    {
        int u = rd(), v = rd(), w = rd(), ty = rd_op();
        add_edge(u, v, w, ty);
    }
    build_g();
    enumerate_triangles();
    wr(ans);
}

詳細信息

Test #1:

score: 10
Accepted
time: 1ms
memory: 4732kb

input:

85 1024
26 70 3 B
75 47 7 G
23 81 7 R
70 68 7 B
51 26 9 R
41 72 8 R
31 46 2 G
82 7 5 G
6 15 2 B
50 44 4 R
41 17 3 G
58 41 8 B
82 22 4 R
9 39 8 B
54 61 7 R
23 29 6 G
68 52 5 R
28 57 7 G
36 39 4 G
74 83 2 B
58 14 4 G
30 39 1 R
38 76 5 G
19 8 3 R
36 48 5 B
71 76 4 B
33 65 8 R
53 81 10 B
28 72 8 G
21 52...

output:

90693

result:

ok 1 number(s): "90693"

Test #2:

score: 10
Accepted
time: 9ms
memory: 5040kb

input:

95 16384
73 45 5 B
57 95 31 G
52 94 52 R
54 45 98 G
81 40 96 R
37 1 15 R
81 91 13 B
66 4 81 B
89 93 43 R
40 23 27 G
30 68 24 G
85 32 98 G
82 86 21 B
24 18 39 R
60 39 29 R
62 74 32 B
77 15 59 R
77 13 59 G
10 53 98 R
24 32 99 G
80 70 52 R
77 49 29 G
19 76 88 G
25 2 55 B
61 92 18 B
44 50 53 R
70 75 1 B...

output:

34773089

result:

ok 1 number(s): "34773089"

Test #3:

score: 10
Accepted
time: 16ms
memory: 4984kb

input:

99 100000
14 25 53839 G
6 22 555681 G
14 58 127533 B
93 30 685857 G
86 68 720946 G
14 59 657691 B
40 57 159703 R
59 83 309777 G
7 99 151251 B
57 77 769566 G
11 4 406698 R
14 60 739074 G
18 51 934158 R
39 14 237859 B
59 70 810047 G
14 35 733152 R
38 19 549269 R
37 79 413532 B
87 7 30363 B
83 7 795910...

output:

300572702

result:

ok 1 number(s): "300572702"

Test #4:

score: 10
Accepted
time: 15ms
memory: 7920kb

input:

4999 50000
3515 625 2 G
1458 3602 1 G
2400 3744 8 R
625 4677 10 G
3602 4385 5 B
4716 2535 7 B
1057 1829 7 R
3691 3073 7 R
1583 1852 7 G
4427 153 10 G
4774 1298 6 R
836 2610 3 G
2022 2207 4 G
3481 4450 6 R
1477 3602 3 G
2399 4851 7 G
3073 3532 9 G
795 2764 9 B
3053 2908 6 G
3979 3239 8 R
4877 863 4 G...

output:

7585569

result:

ok 1 number(s): "7585569"

Test #5:

score: 10
Accepted
time: 13ms
memory: 7964kb

input:

9999 50000
8342 1822 1 R
2454 7542 91 R
4747 6417 54 G
6114 790 56 G
2423 3828 71 G
9534 6417 66 G
6907 9508 93 B
7593 2449 100 B
7037 6417 73 B
8149 1505 10 G
728 5819 91 R
6417 5132 58 B
8046 5108 25 G
9320 4005 13 G
2577 6417 92 G
1154 5596 45 B
3115 8046 61 G
6417 3191 14 B
6417 2556 92 R
6417 6...

output:

305858005

result:

ok 1 number(s): "305858005"

Test #6:

score: 10
Accepted
time: 17ms
memory: 8512kb

input:

39999 50000
711 20333 781142 R
23113 14256 146398 G
36974 20333 525277 R
36111 33004 752115 B
8782 20662 280729 G
25188 9984 379947 B
20333 35283 24241 R
29867 20333 316858 G
17063 20333 204821 B
32466 11296 757600 G
18780 39492 833820 R
20333 32132 963038 G
6250 38375 764880 R
11296 35435 774020 G
...

output:

910002950

result:

ok 1 number(s): "910002950"

Test #7:

score: 10
Accepted
time: 37ms
memory: 10676kb

input:

20000 100000
16355 4798 940 G
13008 13907 349 G
4798 17727 621 R
13008 5194 836 G
13008 18776 111 G
4739 13008 599 B
4798 7237 850 G
13008 6737 228 G
13008 14079 199 B
2532 208 645 B
4798 15196 267 B
8734 18039 668 G
17823 17093 141 G
16325 15294 171 R
13008 15936 950 G
13806 13008 855 R
4104 11188 ...

output:

74652463

result:

ok 1 number(s): "74652463"

Test #8:

score: 10
Accepted
time: 40ms
memory: 12240kb

input:

30000 100000
3757 12254 1505 B
7935 28786 71 B
15119 2468 960 G
12418 15335 468 G
3378 220 534 B
14160 5855 494 B
20502 10202 284 G
1655 11330 36 R
11744 14165 388 R
10444 6281 1463 R
18863 14050 847 G
5311 511 1317 B
6734 75 1224 B
21061 7652 1846 R
22703 14160 1073 B
11890 6592 1002 B
25235 21600 ...

output:

600759360

result:

ok 1 number(s): "600759360"

Test #9:

score: 10
Accepted
time: 25ms
memory: 11944kb

input:

40000 100000
677 7952 47133 B
26245 25217 20330 B
27073 5097 79391 B
24358 23616 90279 G
32570 33531 95478 B
11357 21106 34680 R
11974 27868 17701 R
12937 16890 53472 G
6967 7513 35198 G
27987 11357 58966 B
35641 7203 95467 G
11357 19348 86505 R
27597 34548 53863 R
31334 11357 28148 G
11303 23560 83...

output:

773736079

result:

ok 1 number(s): "773736079"

Test #10:

score: 10
Accepted
time: 33ms
memory: 12628kb

input:

50000 100000
6823 20867 761119 R
15633 41381 594787 G
15521 17722 827117 B
22016 5255 111472 G
33162 21404 102291 G
20232 39357 226738 G
31698 23330 668274 R
41381 11664 404107 B
4517 4277 115972 B
185 719 279579 G
16463 47896 310079 B
3434 32893 16111 B
44769 6560 943760 G
44874 41381 183542 B
1996...

output:

674393235

result:

ok 1 number(s): "674393235"