QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#8718 | #559. 原力 | LCGUO | 100 ✓ | 224ms | 35536kb | C++20 | 3.0kb | 2021-04-03 20:50:20 | 2021-12-19 10:52:54 |
Judging History
answer
#include<cassert>
#include<set>
#include<map>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 50005, M = 200005 << 1, MOD = 1000000007, BAR = 250;
int inverse(int a) {
return a == 1 ? a : (long long)(MOD - MOD / a) * inverse(MOD % a) % MOD;
}
int n, m;
set<pair<int, int> > edges;
map<pair<int, int>, int> ets[3];
#define next NEXT
int dgr[N];
int top, head[N], from[M], to[M], next[M], c[3][M];
void addEdge(int u, int v, int c0, int c1, int c2) {
++dgr[u], ++dgr[v];
from[top] = u, to[top] = v, next[top] = head[u], c[0][top] = c0, c[1][top] = c1, c[2][top] = c2, head[u] = top++;
from[top] = v, to[top] = u, next[top] = head[v], c[0][top] = c0, c[1][top] = c1, c[2][top] = c2, head[v] = top++;
}
inline bool isSmall(int u) {
return dgr[u] <= BAR;
}
inline int calTri(int i, int j, int k) {
int ret = (((long long)c[1][i] * c[2][j] + (long long)c[2][i] * c[1][j]) % MOD * c[0][k]
+ ((long long)c[0][i] * c[2][j] + (long long)c[2][i] * c[0][j]) % MOD * c[1][k]
+ ((long long)c[0][i] * c[1][j] + (long long)c[1][i] * c[0][j]) % MOD * c[2][k]) % MOD;
return ret;
}
int stamp, vis[N], eid[N];
int solveLarge(int u) {
int ret = 0;
++stamp;
for (int i = head[u]; ~i; i = next[i]) {
int v = to[i];
vis[v] = stamp;
eid[v] = i;
}
int sum[3] = {0, 0, 0};
for (int i = 0; i < top; i += 2) {
int v = from[i], w = to[i];
if (vis[v] == stamp && vis[w] == stamp) {
int cntSmall = isSmall(v) + isSmall(w),
delta = calTri(eid[v], i, eid[w] ^ 1);
if ((sum[cntSmall] += delta) >= MOD) {
sum[cntSmall] -= MOD;
}
}
}
ret = ((long long)sum[0] * inverse(3) + (long long)sum[1] * inverse(2) + (long long)sum[2]) % MOD;
return ret;
}
int solveSmall(int u) {
int ret = 0;
++stamp;
for (int i = head[u]; ~i; i = next[i]) {
int v = to[i];
if (isSmall(v)) {
vis[v] = stamp;
eid[v] = i;
for (int j = head[v]; ~j; j = next[j]) {
int w = to[j];
if (vis[w] == stamp) {
if ((ret += calTri(i, j, eid[w] ^ 1)) >= MOD) {
ret -= MOD;
}
}
}
}
}
ret = (long long)ret * inverse(3) % MOD;
return ret;
}
int main() {
memset(head, -1, sizeof(head));
scanf("%d%d", &n, &m);
for (int i = 0; i < m; ++i) {
int u, v, w, c = -1;
char s[2];
scanf("%d%d%d%s", &u, &v, &w, s);
--u, --v;
if (s[0] == 'R') {
c = 0;
} else if (s[0] == 'G') {
c = 1;
} else if (s[0] == 'B') {
c = 2;
}
if (u > v) {
swap(u, v);
}
(ets[c][make_pair(u, v)] += w) %= MOD;
edges.insert(make_pair(u, v));
}
for (set<pair<int, int> >::iterator it = edges.begin(); it != edges.end(); ++it) {
addEdge((*it).first, (*it).second, ets[0][*it], ets[1][*it], ets[2][*it]);
}
int ans = 0;
for (int i = 0; i < n; ++i) {
if (isSmall(i)) {
(ans += solveSmall(i)) %= MOD;
} else {
(ans += solveLarge(i)) %= MOD;
}
}
printf("%d\n", ans);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 10
Accepted
time: 5ms
memory: 12312kb
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 10 B 7 15 9 B 11 33 7 R 19 45 2 B 39 26 8 B 40 50 7 G 38 11 7 G 57 68 10 G 41 60 9 R 1 54 5 B 83 11 10 G 24 49 1 B 55 40 9 B 53 21 2 G 19 33 2 R 31 8 5 G 75 15 10 G 32 5 2 R 9 75 5 G 15 72 10 G 35 62...
output:
90693
result:
ok 1 number(s): "90693"
Test #2:
score: 10
Accepted
time: 19ms
memory: 13096kb
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 9 41 25 G 26 82 96 R 75 93 56 B 70 6 30 R 29 9 48 G 13 41 51 B 86 45 58 B 76 69 19 G 29 84 23 B 4 85 69 R 5 41 78 R 74 8 61 G 12 48 96 B 62 49 40 R 47 42 26 B 48 53 100 G 28 45 64 R 88 71 12 G 19 82 ...
output:
34773089
result:
ok 1 number(s): "34773089"
Test #3:
score: 10
Accepted
time: 50ms
memory: 13208kb
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 G 88 53 766732 G 90 67 486531 R 1 79 586992 G 23 89 700265 R 16 86 170860 G 86 4 792398 G 24 62 119012 G 59 14 322340 G 61 14 653133 G 82 14 678844 B 93 53 243054 B 91 79 40517 R 73 77 858230 R 16 15...
output:
300572702
result:
ok 1 number(s): "300572702"
Test #4:
score: 10
Accepted
time: 74ms
memory: 22252kb
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 1883 939 5 B 1695 4478 2 R 735 1425 1 B 3398 4774 8 G 3285 4774 6 R 1759 3858 6 B 1492 4993 1 G 1759 2632 4 B 2425 1829 10 G 3073 1346 7 R 1393 1358 9 R 2819 625 2 G 1749 2156 8 B 906 3602 1 G 3813 1...
output:
7585569
result:
ok 1 number(s): "7585569"
Test #5:
score: 10
Accepted
time: 73ms
memory: 22980kb
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 6958 45 R 5146 5364 94 G 1161 4183 68 G 7594 5596 97 B 7431 4491 1 B 6276 6417 10 G 6950 2479 82 B 7891 628 7 R 8519 6005 6 B 6417 4481 26 R 3996 5060 68 B 9376 3729 31 B 6417 7586 87 R 5455 3732 53 R ...
output:
305858005
result:
ok 1 number(s): "305858005"
Test #6:
score: 10
Accepted
time: 70ms
memory: 23008kb
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 17187 11296 180284 R 18725 25188 301656 G 24815 28030 744278 R 23983 24276 359591 B 20333 18679 537724 B 836 25188 786720 R 16566 20333 244794 R 11296 32597 704595 R 29020 20333 946240 R 1811 22024 75...
output:
910002950
result:
ok 1 number(s): "910002950"
Test #7:
score: 10
Accepted
time: 203ms
memory: 32852kb
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 215 R 12683 6795 214 B 3868 15854 924 G 1897 8924 804 R 6408 13008 470 G 17782 13008 887 G 920 13008 159 B 978 4798 762 R 678 4798 841 R 15982 8836 502 B 11519 4798 349 G 2858 8032 401 B 18195 3834 38...
output:
74652463
result:
ok 1 number(s): "74652463"
Test #8:
score: 10
Accepted
time: 224ms
memory: 35488kb
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 639 G 5471 25492 1624 B 27406 14160 834 B 11521 16671 292 G 2468 29984 1627 R 17711 2025 1107 G 28948 22169 1365 B 14750 15570 1723 G 17011 20449 144 R 17011 14588 108 R 12286 14040 1509 R 6163 26387 ...
output:
600759360
result:
ok 1 number(s): "600759360"
Test #9:
score: 10
Accepted
time: 176ms
memory: 32948kb
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 83588 R 34947 11357 39856 G 116 21347 44870 R 11357 481 44625 B 27868 13265 96533 B 18447 12978 24529 R 131 11357 75024 G 11357 8310 26711 B 5914 7453 14528 G 31946 7453 59038 B 11357 19797 95515 B 2320...
output:
773736079
result:
ok 1 number(s): "773736079"
Test #10:
score: 10
Accepted
time: 209ms
memory: 35536kb
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 19964 42534 447433 G 3888 25157 400747 R 13499 30149 682647 G 25769 24850 943015 R 23319 46896 144049 B 25540 17722 109200 R 37822 38172 716867 B 41381 13786 950406 G 23187 26795 332235 G 21854 41167 4512...
output:
674393235
result:
ok 1 number(s): "674393235"