#include <bits/stdc++.h>
using namespace std;
int n, m, nodes, vid, found;
vector<vector<int>> adj;
vector<int> vis;
long long ans;
void dfs (int u, int t, int k, bool ok = 0) {
if (found) {
return;
}
vis[u] = vid;
if (u == k) {
found = ok;
ans += ok;
}
ok |= u == t;
for (int v : adj[u]) {
if (vis[v] != vid) {
dfs(v, t, k, ok);
}
}
vis[u] = 0;
}
bool cycle;
void dfs2 (int u, int p = -1) {
vis[u] = 1;
++nodes;
for (int v : adj[u]) {
if (!vis[v]) {
dfs2(v, u);
}
else if (v ^ p) {
cycle = 1;
}
}
}
int dfs3 (int u, int d = 0) {
vis[u] = 1;
int s = 0;
for (int v : adj[u]) {
if (!vis[v]) {
int f = dfs3(v, d);
s += f;
ans -= f * 1LL * f;
}
}
ans += s * 2LL * (nodes - s - 1);
ans += s * 1LL * s;
return s + 1;
}
void dfs4 (int u, int p = -1) {
++nodes;
for (int v : adj[u]) {
if (v ^ p) {
dfs4(v, u);
}
}
}
int main (){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// freopen("SuhaibSawalha1","r",stdin);
cin >> n >> m;
adj.resize(n);
while (m--) {
int u, v;
cin >> u >> v, --u, --v;
adj[u].push_back(v);
adj[v].push_back(u);
}
vis.resize(n);
if (n <= 10) {
for (int u = 0; u < n; ++u) {
for (int v = 0; v < n; ++v) {
for (int k = 0; k < n; ++k) {
if (set<int>{u, v, k}.size() == 3) {
++vid;
found = 0;
dfs(u, v, k);
}
}
}
}
cout << ans;
return 0;
}
if (all_of(adj.begin(), adj.end(), [](auto &v) {return v.size() <= 2;})) {
for (int u = 0; u < n; ++u) {
if (!vis[u]) {
nodes = cycle = 0;
dfs2(u);
if (cycle) {
ans += nodes * 1LL * (nodes - 1) * (nodes - 2);
}
else {
ans += nodes * 1LL * (nodes - 1) * (nodes - 2) / 3;
}
}
}
cout << ans;
return 0;
}
for (int u = 0; u < n; ++u) {
if (!vis[u]) {
nodes = 0;
dfs4(u);
dfs3(u);
}
}
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
4 ms |
204 KB |
Output is correct |
9 |
Correct |
24 ms |
308 KB |
Output is correct |
10 |
Correct |
369 ms |
288 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Correct |
1 ms |
204 KB |
Output is correct |
23 |
Correct |
1 ms |
204 KB |
Output is correct |
24 |
Correct |
1 ms |
204 KB |
Output is correct |
25 |
Correct |
1 ms |
204 KB |
Output is correct |
26 |
Correct |
1 ms |
204 KB |
Output is correct |
27 |
Correct |
1 ms |
204 KB |
Output is correct |
28 |
Correct |
1 ms |
204 KB |
Output is correct |
29 |
Correct |
1 ms |
204 KB |
Output is correct |
30 |
Correct |
1 ms |
204 KB |
Output is correct |
31 |
Correct |
1 ms |
204 KB |
Output is correct |
32 |
Correct |
1 ms |
204 KB |
Output is correct |
33 |
Correct |
1 ms |
204 KB |
Output is correct |
34 |
Correct |
1 ms |
204 KB |
Output is correct |
35 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
4 ms |
204 KB |
Output is correct |
9 |
Correct |
24 ms |
308 KB |
Output is correct |
10 |
Correct |
369 ms |
288 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Correct |
1 ms |
204 KB |
Output is correct |
23 |
Correct |
1 ms |
204 KB |
Output is correct |
24 |
Correct |
1 ms |
204 KB |
Output is correct |
25 |
Correct |
1 ms |
204 KB |
Output is correct |
26 |
Correct |
1 ms |
204 KB |
Output is correct |
27 |
Correct |
1 ms |
204 KB |
Output is correct |
28 |
Correct |
1 ms |
204 KB |
Output is correct |
29 |
Correct |
1 ms |
204 KB |
Output is correct |
30 |
Correct |
1 ms |
204 KB |
Output is correct |
31 |
Correct |
1 ms |
204 KB |
Output is correct |
32 |
Correct |
1 ms |
204 KB |
Output is correct |
33 |
Correct |
1 ms |
204 KB |
Output is correct |
34 |
Correct |
1 ms |
204 KB |
Output is correct |
35 |
Correct |
1 ms |
204 KB |
Output is correct |
36 |
Correct |
0 ms |
204 KB |
Output is correct |
37 |
Correct |
1 ms |
204 KB |
Output is correct |
38 |
Runtime error |
903 ms |
1048580 KB |
Execution killed with signal 9 |
39 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
83 ms |
10808 KB |
Output is correct |
2 |
Correct |
66 ms |
10772 KB |
Output is correct |
3 |
Correct |
82 ms |
8408 KB |
Output is correct |
4 |
Correct |
75 ms |
9572 KB |
Output is correct |
5 |
Correct |
73 ms |
7748 KB |
Output is correct |
6 |
Correct |
70 ms |
7748 KB |
Output is correct |
7 |
Correct |
66 ms |
7076 KB |
Output is correct |
8 |
Correct |
64 ms |
7528 KB |
Output is correct |
9 |
Correct |
73 ms |
6596 KB |
Output is correct |
10 |
Correct |
87 ms |
6980 KB |
Output is correct |
11 |
Correct |
64 ms |
6096 KB |
Output is correct |
12 |
Correct |
57 ms |
5964 KB |
Output is correct |
13 |
Correct |
37 ms |
6032 KB |
Output is correct |
14 |
Correct |
45 ms |
5912 KB |
Output is correct |
15 |
Correct |
31 ms |
5520 KB |
Output is correct |
16 |
Correct |
41 ms |
5448 KB |
Output is correct |
17 |
Correct |
4 ms |
3020 KB |
Output is correct |
18 |
Correct |
3 ms |
3028 KB |
Output is correct |
19 |
Correct |
4 ms |
3020 KB |
Output is correct |
20 |
Correct |
3 ms |
3020 KB |
Output is correct |
21 |
Correct |
3 ms |
3020 KB |
Output is correct |
22 |
Correct |
3 ms |
3020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
272 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
312 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
74 ms |
6304 KB |
Output is correct |
2 |
Correct |
81 ms |
6288 KB |
Output is correct |
3 |
Correct |
75 ms |
6288 KB |
Output is correct |
4 |
Correct |
74 ms |
6164 KB |
Output is correct |
5 |
Correct |
81 ms |
6316 KB |
Output is correct |
6 |
Correct |
69 ms |
8724 KB |
Output is correct |
7 |
Correct |
94 ms |
7880 KB |
Output is correct |
8 |
Correct |
81 ms |
7396 KB |
Output is correct |
9 |
Correct |
91 ms |
6960 KB |
Output is correct |
10 |
Correct |
65 ms |
6244 KB |
Output is correct |
11 |
Correct |
70 ms |
7492 KB |
Output is correct |
12 |
Correct |
69 ms |
7500 KB |
Output is correct |
13 |
Correct |
57 ms |
7488 KB |
Output is correct |
14 |
Correct |
60 ms |
7264 KB |
Output is correct |
15 |
Correct |
60 ms |
7236 KB |
Output is correct |
16 |
Correct |
28 ms |
6008 KB |
Output is correct |
17 |
Correct |
40 ms |
7740 KB |
Output is correct |
18 |
Correct |
37 ms |
7764 KB |
Output is correct |
19 |
Correct |
51 ms |
7764 KB |
Output is correct |
20 |
Correct |
45 ms |
7708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Execution timed out |
1058 ms |
1048580 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
61 ms |
6212 KB |
Output is correct |
2 |
Correct |
60 ms |
6220 KB |
Output is correct |
3 |
Execution timed out |
1163 ms |
915340 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
4 ms |
204 KB |
Output is correct |
9 |
Correct |
24 ms |
308 KB |
Output is correct |
10 |
Correct |
369 ms |
288 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Correct |
1 ms |
204 KB |
Output is correct |
23 |
Correct |
1 ms |
204 KB |
Output is correct |
24 |
Correct |
1 ms |
204 KB |
Output is correct |
25 |
Correct |
1 ms |
204 KB |
Output is correct |
26 |
Correct |
1 ms |
204 KB |
Output is correct |
27 |
Correct |
1 ms |
204 KB |
Output is correct |
28 |
Correct |
1 ms |
204 KB |
Output is correct |
29 |
Correct |
1 ms |
204 KB |
Output is correct |
30 |
Correct |
1 ms |
204 KB |
Output is correct |
31 |
Correct |
1 ms |
204 KB |
Output is correct |
32 |
Correct |
1 ms |
204 KB |
Output is correct |
33 |
Correct |
1 ms |
204 KB |
Output is correct |
34 |
Correct |
1 ms |
204 KB |
Output is correct |
35 |
Correct |
1 ms |
204 KB |
Output is correct |
36 |
Correct |
0 ms |
204 KB |
Output is correct |
37 |
Correct |
1 ms |
204 KB |
Output is correct |
38 |
Runtime error |
903 ms |
1048580 KB |
Execution killed with signal 9 |
39 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
4 ms |
204 KB |
Output is correct |
9 |
Correct |
24 ms |
308 KB |
Output is correct |
10 |
Correct |
369 ms |
288 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Correct |
1 ms |
204 KB |
Output is correct |
23 |
Correct |
1 ms |
204 KB |
Output is correct |
24 |
Correct |
1 ms |
204 KB |
Output is correct |
25 |
Correct |
1 ms |
204 KB |
Output is correct |
26 |
Correct |
1 ms |
204 KB |
Output is correct |
27 |
Correct |
1 ms |
204 KB |
Output is correct |
28 |
Correct |
1 ms |
204 KB |
Output is correct |
29 |
Correct |
1 ms |
204 KB |
Output is correct |
30 |
Correct |
1 ms |
204 KB |
Output is correct |
31 |
Correct |
1 ms |
204 KB |
Output is correct |
32 |
Correct |
1 ms |
204 KB |
Output is correct |
33 |
Correct |
1 ms |
204 KB |
Output is correct |
34 |
Correct |
1 ms |
204 KB |
Output is correct |
35 |
Correct |
1 ms |
204 KB |
Output is correct |
36 |
Correct |
0 ms |
204 KB |
Output is correct |
37 |
Correct |
1 ms |
204 KB |
Output is correct |
38 |
Runtime error |
903 ms |
1048580 KB |
Execution killed with signal 9 |
39 |
Halted |
0 ms |
0 KB |
- |