QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#786474 | #7040. Connected Subgraphs | Adorable | AC ✓ | 7909ms | 46700kb | C++23 | 10.1kb | 2024-11-26 21:41:54 | 2024-11-26 21:41:56 |
Judging History
answer
// あの花はまだどこかに咲いているに違いない
// 我们会慢慢长大成人,在不断流逝的季节之中,盛开在路旁的花儿们,也在渐渐变化,盛开在那个季节里的花朵什么名字呢,在风中小小摇曳,伸手触碰便被扎得疼痛,把脸凑近,就能闻到一股淡淡的青涩的太阳芳香,那股芳香渐渐消散,而我们渐渐长大,但是,那朵花一定依旧盛开在某处。
// 十年后的八月,我们还会相见,我们总会以为那是永远,
// 殊不知有一天,当我们回头时,才发现原来我们早已相隔天边.
// 你是信的开头诗的内容,童话的结尾。你是理所当然的奇迹,你是月色真美。你是圣诞老人送给我,好孩子的礼物。你是三千美丽世界里,我的一瓢水。
// 所以让我再靠近一点点,因为你太温暖。我会再变得坚强一点点,因为你太柔软。交换无名指金色的契约,给彼此岁月。说好从今以后都牵着手,因为要走很远。
// 你是我万水千山的冒险,要找的标记点。你是分割我人生的线,又将它们相连。你是前世千次的回眸,虔诚牵的手。你是其余所有的一切,是我的世界。
// 所以请你再闪亮一点点,尽管我太平凡。我会再变得柔软一点点,因为你太敏感。交换无名指金色的契约,给彼此岁月。说好从今以后都牵着手,不管要走多远。
// 让我再靠近一点点,因为你太温暖。我会再变得坚强一点点,因为你太柔软。交换无名指金色的契约,给彼此岁月。说好从今以后都牵着手,因为要走很远。
// Code by ttq012.
// Date: 26/11/2024
#pragma GCC diagnostic error "-std=c++11"
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define eb emplace_back
#define int long long
using namespace std;
const int N = 1000100;
const int mod = 1e9 + 7;
const int base = 256;
const int inf = 0x0d000721ll << 2 | 0xAC66666;
namespace ttq012 {
int read() {
int o = 1, x = 0;
char ch;
while (!isdigit(ch = getchar())) {
if (ch == '-') {
o = -o;
}
}
x = ch & 15;
while (isdigit(ch = getchar())) {
x = (x << 3) + (x << 1) + (ch & 15);
}
return x * o;
}
int exgcd(int a, int b, int &x, int &y) {
if (!b) {
x = 1, y = 0;
return a;
}
int g, xp, yp;
g = exgcd(b, a % b, xp, yp);
x = yp, y = xp - yp * (a / b);
return g;
}
int ksm(int a, int b, int c) {
int ans = 1;
while (b) {
if (b & 1) {
ans = ans * a % c;
}
a = a * a % c;
b >>= 1;
}
return ans;
}
int Inv(int a, int Mod) {
return ksm(a, Mod - 2, Mod);
}
int gcd(int a, int b) {
return b ? gcd(b, a % b) : a;
}
int lcm(int a, int b) {
return a / gcd(a, b) * b;
}
int highbit(int x) {
return 1ull << (63 - __builtin_clzll(x));
}
void swap(int &a, int &b) {
if (a != b) a ^= b ^= a ^= b;
} } // using namespace ttq012;
mt19937_64 mt(time(0));
namespace ttq012 {
vector<int> z[N], zz[N];
int deg[N], n, m, vis[N];
pair<int, int> edge[N];
int visit[10];
int cycle_3() {
if (visit[0] != -1) return visit[0];
fill(deg + 1, deg + n + 1, 0ll);
for (int i = 1; i <= m; ++i) {
int x = edge[i].first, y = edge[i].second;
++deg[x], ++deg[y];
edge[i] = {x, y};
}
for (int i = 1; i <= n; ++i) z[i].clear();
for (int i = 1; i <= m; ++i) {
int x = edge[i].first, y = edge[i].second;
if (deg[x] < deg[y] || deg[x] == deg[y] && x < y) z[x].eb(y);
else z[y].eb(x);
}
int cnt = 0;
for (int i = 1; i <= n; ++i) {
for (auto &j : z[i]) vis[j] = 1;
for (auto &j : z[i]) for (auto &k : z[j])
if (vis[k]) ++cnt;
for (auto &j : z[i]) vis[j] = 0;
}
return visit[0] = cnt % mod;
}
int buc[N];
int cycle_4() {
if (visit[1] != -1) return visit[1];
fill(deg + 1, deg + n + 1, 0ll);
for (int i = 1; i <= n; ++i) z[i].clear(), zz[i].clear();
for (int i = 1; i <= m; ++i) {
int x = edge[i].first, y = edge[i].second;
++deg[x], ++deg[y];
z[x].eb(y), z[y].eb(x);
}
for (int i = 1; i <= m; ++i) {
int x = edge[i].first, y = edge[i].second;
if (deg[x] < deg[y] || deg[x] == deg[y] && x < y) zz[y].eb(x);
else zz[x].eb(y);
}
int cnt = 0;
for (int i = 1; i <= n; ++i) {
for (auto &j : zz[i])
for (auto &k : z[j])
if (deg[i] > deg[k] || deg[i] == deg[k] && i > k)
cnt += buc[k]++, cnt %= mod;
for (auto &j : zz[i])
for (auto &k : z[j])
buc[k] = 0;
}
return visit[1] = cnt % mod;
}
inline int binom4(int x) {
return x * (x - 1) % mod * (x - 2) % mod * (x - 3) % mod * Inv(24, mod) % mod;
}
// 菊花(十字)
int count_flowers() {
if (visit[2] != -1) return visit[2];
fill(deg + 1, deg + n + 1, 0ll);
for (int i = 1; i <= m; ++i) {
int x = edge[i].first, y = edge[i].second;
++deg[x], ++deg[y];
}
int cnt = 0;
for (int i = 1; i <= n; ++i)
cnt += binom4(deg[i]);
return visit[2] = cnt;
}
// 三元环外加一个点
int cycle_3_with_1() {
if (visit[3] != -1) return visit[3];
fill(deg + 1, deg + n + 1, 0ll);
for (int i = 1; i <= m; ++i) {
int x = edge[i].first, y = edge[i].second;
++deg[x], ++deg[y];
}
for (int i = 1; i <= n; ++i) z[i].clear();
for (int i = 1; i <= m; ++i) {
int x = edge[i].first, y = edge[i].second;
if (deg[x] < deg[y] || deg[x] == deg[y] && x < y) z[x].eb(y);
else z[y].eb(x);
}
int cnt = 0;
for (int i = 1; i <= n; ++i) {
for (auto &j : z[i]) vis[j] = 1;
for (auto &j : z[i]) for (auto &k : z[j])
if (vis[k]) cnt += (deg[i] + deg[j] + deg[k] - 6), cnt %= mod;
for (auto &j : z[i]) vis[j] = 0;
}
return visit[3] = cnt;
}
int binom2(int x) {
return x * (x - 1) % mod * Inv(2, mod) % mod;
}
// 四个点的链外加一个点
int link_4_with_1() {
if (visit[4] != -1) return visit[4];
fill(deg + 1, deg + n + 1, 0ll);
for (int i = 1; i <= n; ++i) z[i].clear();
for (int i = 1; i <= m; ++i) {
int x = edge[i].first, y = edge[i].second;
++deg[x], ++deg[y];
z[x].eb(y), z[y].eb(x);
}
int cnt = 0;
for (int x = 1; x <= n; ++x)
if (deg[x] >= 2)
for (auto &y : z[x])
if (x != y && deg[y] >= 3)
cnt += (deg[x] - 1) * binom2(deg[y] - 1), cnt %= mod;
return visit[4] = (cnt + mod + mod - cycle_3_with_1() * 2) % mod;
}
// 五个点的链
int link_5() {
if (visit[5] != -1) return visit[5];
fill(deg + 1, deg + n + 1, 0ll);
for (int i = 1; i <= m; ++i) {
int x = edge[i].first, y = edge[i].second;
++deg[x], ++deg[y];
}
int cnt = 0;
for (int i = 1; i <= n; ++i)
if (deg[i] >= 2) {
int pref_sum = 0;
for (int i0 = 0; i0 < z[i].size(); ++i0) {
int x = z[i][i0];
if (deg[x] >= 2)
cnt = (cnt + (deg[x] - 1) * pref_sum % mod) % mod, pref_sum = (pref_sum + deg[x] - 1 + mod) % mod;
}
}
return visit[5] = (cnt - 3 * cycle_3() - 4 * cycle_4() - 2 * cycle_3_with_1() + mod * 9) % mod;
}
void run() {
// freopen("1.in", "r", stdin);
int T = read(), ca = 1, tc = T, n1;
while (T--) {
cerr << (ca++) << '\n';
memset(visit, -1, sizeof visit);
n = read(), m = read();
if (ca == 1) n1 = n;
for (int i = 1; i <= m; ++i) {
int x = read(), y = read();
edge[i] = {x, y};
}
int x1 = cycle_4();
int x2 = count_flowers();
int x3 = link_5();
int x4 = link_4_with_1();
int x5 = cycle_3_with_1();
cout << (x1 + x2 + x3 + x4 + x5) % mod << '\n';
}
cerr << clock() << '\n';
}
}
signed main() {
ttq012::run();
return 0;
}
/*
1
4 6
1 2
1 3
1 4
2 3
2 4
3 4
*/
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 4ms
memory: 9788kb
input:
2 4 4 1 2 2 3 3 4 4 1 4 6 1 2 1 3 1 4 2 3 2 4 3 4
output:
1 15
result:
ok 2 lines
Test #2:
score: 0
Accepted
time: 1296ms
memory: 46700kb
input:
10 3296 174079 484 736 80 3275 914 1609 611 2069 1111 3027 749 1443 568 1878 1180 2788 1072 2156 171 797 106 1226 1749 2331 758 3080 772 2017 1381 2393 1945 2943 269 1431 1640 1805 1924 2161 1720 2123 1551 2940 286 1226 460 1462 1807 1823 40 638 18 707 589 849 1857 1987 914 2360 2446 3129 24 910 113...
output:
192810800 284251171 771081766 673113026 227774506 23760227 44050220 47307815 414063001 939220437
result:
ok 10 lines
Test #3:
score: 0
Accepted
time: 7909ms
memory: 22980kb
input:
10 693 200000 442 134 485 423 447 184 544 230 46 401 224 156 684 285 227 141 321 647 126 62 644 253 246 509 278 318 104 340 171 116 187 434 554 574 344 285 619 640 240 634 132 193 415 441 130 427 132 134 206 53 102 460 560 312 530 291 479 113 231 455 501 238 666 288 10 422 330 415 240 2 163 302 96 5...
output:
262595832 325062112 160521108 950636215 426755446 561024458 49082359 743032532 582488571 687278985
result:
ok 10 lines
Test #4:
score: 0
Accepted
time: 936ms
memory: 32968kb
input:
10 100000 200000 1 50000 2 50000 3 50000 4 50000 5 50000 6 50000 7 50000 8 50000 9 50000 10 50000 11 50000 12 50000 13 50000 14 50000 15 50000 16 50000 17 50000 18 50000 19 50000 20 50000 21 50000 22 50000 23 50000 24 50000 25 50000 26 50000 27 50000 28 50000 29 50000 30 50000 31 50000 32 50000 33 5...
output:
427268331 405969056 287464978 480992480 583899102 346872966 503167381 491791864 482789441 414972531
result:
ok 10 lines
Test #5:
score: 0
Accepted
time: 942ms
memory: 35716kb
input:
10 100000 200000 50000 50001 1 50001 2 50001 3 50001 4 50001 5 50001 6 50001 7 50001 8 50001 9 50001 10 50001 11 50001 12 50001 13 50001 14 50001 15 50001 16 50001 17 50001 18 50001 19 50001 20 50001 21 50001 22 50001 23 50001 24 50001 25 50001 26 50001 27 50001 28 50001 29 50001 30 50001 31 50001 3...
output:
318946949 387994406 758360868 579574960 687516111 966394852 864355459 29275977 871937345 134220714
result:
ok 10 lines