#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, m, i, j, x, y, ans, sz[200001], rt[200001];
bool u[200001], cyc, cyc2[200001];
vector<int> g[200001];
vector<pair<int, int>> v;
void dfs(int x, int pr, int r) {
sz[x] = 1;
u[x] = 1;
rt[x] = r;
for (auto y : g[x]) {
if (y == pr) continue;
if (u[y]) {
cyc = 1;
continue;
}
dfs(y, x, r);
sz[x] += sz[y];
}
}
signed main() {
cin.tie(0)->sync_with_stdio(0);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif
cin >> n >> m;
while (m--) {
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
for (i = 1; i <= n; i++) {
if (!u[i]) {
cyc = 0;
dfs(i, i, i);
v.push_back({i, cyc});
}
}
for (auto i : v) {
if (!i.second) ans -= (sz[i.first]) * (sz[i.first] - 1);
else {
cyc2[i.first] = 1;
ans += (sz[i.first]) * (sz[i.first] - 1) * (sz[i.first] - 2);
}
}
for (i = 1; i <= n; i++) {
if (!cyc2[rt[i]]) {
ans += 2 * (sz[i] - 1) * (sz[rt[i]] - sz[i]);
ans += 2 * (sz[rt[i]] - sz[i]);
}
}
cout << ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
7000 KB |
Output is correct |
2 |
Correct |
1 ms |
6748 KB |
Output is correct |
3 |
Correct |
2 ms |
6748 KB |
Output is correct |
4 |
Correct |
1 ms |
6748 KB |
Output is correct |
5 |
Correct |
1 ms |
6748 KB |
Output is correct |
6 |
Correct |
1 ms |
6748 KB |
Output is correct |
7 |
Incorrect |
1 ms |
6748 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
7000 KB |
Output is correct |
2 |
Correct |
1 ms |
6748 KB |
Output is correct |
3 |
Correct |
2 ms |
6748 KB |
Output is correct |
4 |
Correct |
1 ms |
6748 KB |
Output is correct |
5 |
Correct |
1 ms |
6748 KB |
Output is correct |
6 |
Correct |
1 ms |
6748 KB |
Output is correct |
7 |
Incorrect |
1 ms |
6748 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
47 ms |
19592 KB |
Output is correct |
2 |
Correct |
35 ms |
19540 KB |
Output is correct |
3 |
Correct |
32 ms |
15708 KB |
Output is correct |
4 |
Correct |
38 ms |
17744 KB |
Output is correct |
5 |
Correct |
33 ms |
14428 KB |
Output is correct |
6 |
Correct |
31 ms |
14420 KB |
Output is correct |
7 |
Correct |
31 ms |
13404 KB |
Output is correct |
8 |
Correct |
40 ms |
13908 KB |
Output is correct |
9 |
Correct |
29 ms |
12624 KB |
Output is correct |
10 |
Correct |
30 ms |
13396 KB |
Output is correct |
11 |
Correct |
25 ms |
12168 KB |
Output is correct |
12 |
Correct |
26 ms |
12248 KB |
Output is correct |
13 |
Correct |
24 ms |
12496 KB |
Output is correct |
14 |
Correct |
24 ms |
12444 KB |
Output is correct |
15 |
Correct |
19 ms |
11800 KB |
Output is correct |
16 |
Correct |
18 ms |
12112 KB |
Output is correct |
17 |
Correct |
5 ms |
9644 KB |
Output is correct |
18 |
Correct |
5 ms |
9688 KB |
Output is correct |
19 |
Correct |
5 ms |
9460 KB |
Output is correct |
20 |
Correct |
5 ms |
9940 KB |
Output is correct |
21 |
Correct |
5 ms |
9428 KB |
Output is correct |
22 |
Correct |
5 ms |
9684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6748 KB |
Output is correct |
2 |
Correct |
2 ms |
6748 KB |
Output is correct |
3 |
Correct |
2 ms |
6748 KB |
Output is correct |
4 |
Correct |
2 ms |
6748 KB |
Output is correct |
5 |
Correct |
3 ms |
6748 KB |
Output is correct |
6 |
Correct |
2 ms |
6748 KB |
Output is correct |
7 |
Correct |
2 ms |
6748 KB |
Output is correct |
8 |
Correct |
2 ms |
6744 KB |
Output is correct |
9 |
Correct |
2 ms |
6748 KB |
Output is correct |
10 |
Correct |
2 ms |
6748 KB |
Output is correct |
11 |
Correct |
2 ms |
6744 KB |
Output is correct |
12 |
Correct |
2 ms |
6744 KB |
Output is correct |
13 |
Correct |
2 ms |
6744 KB |
Output is correct |
14 |
Correct |
2 ms |
6748 KB |
Output is correct |
15 |
Correct |
2 ms |
6748 KB |
Output is correct |
16 |
Correct |
2 ms |
6748 KB |
Output is correct |
17 |
Correct |
2 ms |
6748 KB |
Output is correct |
18 |
Correct |
2 ms |
6748 KB |
Output is correct |
19 |
Correct |
2 ms |
6748 KB |
Output is correct |
20 |
Correct |
2 ms |
6876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
12368 KB |
Output is correct |
2 |
Correct |
28 ms |
12380 KB |
Output is correct |
3 |
Correct |
29 ms |
12380 KB |
Output is correct |
4 |
Correct |
32 ms |
12460 KB |
Output is correct |
5 |
Correct |
33 ms |
12380 KB |
Output is correct |
6 |
Correct |
32 ms |
15952 KB |
Output is correct |
7 |
Correct |
35 ms |
15184 KB |
Output is correct |
8 |
Correct |
30 ms |
14424 KB |
Output is correct |
9 |
Correct |
32 ms |
13660 KB |
Output is correct |
10 |
Correct |
35 ms |
12484 KB |
Output is correct |
11 |
Correct |
28 ms |
12436 KB |
Output is correct |
12 |
Correct |
26 ms |
12436 KB |
Output is correct |
13 |
Correct |
28 ms |
12372 KB |
Output is correct |
14 |
Correct |
25 ms |
12636 KB |
Output is correct |
15 |
Correct |
41 ms |
12512 KB |
Output is correct |
16 |
Correct |
17 ms |
11732 KB |
Output is correct |
17 |
Correct |
22 ms |
12752 KB |
Output is correct |
18 |
Correct |
35 ms |
12612 KB |
Output is correct |
19 |
Correct |
23 ms |
12732 KB |
Output is correct |
20 |
Correct |
23 ms |
12504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6748 KB |
Output is correct |
2 |
Correct |
2 ms |
6748 KB |
Output is correct |
3 |
Incorrect |
2 ms |
6748 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
12372 KB |
Output is correct |
2 |
Correct |
38 ms |
12320 KB |
Output is correct |
3 |
Incorrect |
34 ms |
13072 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
7000 KB |
Output is correct |
2 |
Correct |
1 ms |
6748 KB |
Output is correct |
3 |
Correct |
2 ms |
6748 KB |
Output is correct |
4 |
Correct |
1 ms |
6748 KB |
Output is correct |
5 |
Correct |
1 ms |
6748 KB |
Output is correct |
6 |
Correct |
1 ms |
6748 KB |
Output is correct |
7 |
Incorrect |
1 ms |
6748 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
7000 KB |
Output is correct |
2 |
Correct |
1 ms |
6748 KB |
Output is correct |
3 |
Correct |
2 ms |
6748 KB |
Output is correct |
4 |
Correct |
1 ms |
6748 KB |
Output is correct |
5 |
Correct |
1 ms |
6748 KB |
Output is correct |
6 |
Correct |
1 ms |
6748 KB |
Output is correct |
7 |
Incorrect |
1 ms |
6748 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |