#include <iostream>
#include <vector>
#include <map>
using namespace std;
map<pair<int, int>, int> mm;
vector<int> g[100005], gg[200005];
int tin[100005], low[100005], dd[100005], da[100005], z = 0, db[100005];
long long int s[200005], f[200005], hh = 0, kk = 0, ha, res = 0;
void dfs(int x, int p) {
tin[x] = low[x] = ++z;
int fl = 0, c = 0;
for (int w : g[x]) {
if (w == p) continue;
if (tin[w] == 0) {
dfs(w, x);
low[x] = min(low[x], low[w]);
if (low[w] >= tin[x]) {
fl = 1;
}
c++;
}
else low[x] = min(low[x], tin[w]);
}
if (p == -1) {
if (c >= 2) dd[x] = 1;
}
else if (fl == 1) dd[x] = 1;
hh += dd[x];
kk++;
}
void dfs2(int x, int h) {
da[x] = h;
s[h]++;
for (int w : g[x]) {
if (da[w] != 0 || dd[w] == 1) continue;
dfs2(w, h);
}
}
void dfs3(int x, int p) {
db[x] = 1;
f[x] = s[x];
for (int w : gg[x]) {
if (w == p) continue;
dfs3(w, x);
f[x] += f[w];
}
}
void dfs4(int x, int p) {
long long int h = 0;
for (int w : gg[x]) {
if (w != p) dfs4(w, x);
long long int k;
if (w == p) k = ha - f[x];
else k = f[w];
res += s[w] * h * k;
res += k * s[w] * (s[w] - 1);
res += s[w] * (s[w] - 1) / 2;
h += k;
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
long long int n, m;
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
for (int i = 1; i <= n; i++) {
if (tin[i] == 0) {
hh = kk = 0;
dfs(i, -1);
if (hh == 0) {
res += kk * (kk - 1) * (kk - 2) / 2;
}
}
}
int zz = 0;
for (int i = 1; i <= n; i++) {
if (dd[i] == 1) {
da[i] = ++zz;
s[zz] = 1;
for (int w : g[i]) {
if (da[w] == 0 && dd[w] == 0) {
zz++;
dfs2(w, zz);
}
}
}
}
for (int i = 1; i <= zz; i++) {
res += s[i] * (s[i] - 1) * (s[i] - 2) / 2;
}
for (int i = 1; i <= n; i++) {
for (int w : g[i]) {
int h = da[i], k = da[w];
if (h == k || mm.count({h, k})) continue;
if (dd[i] == dd[w]) continue;
gg[h].push_back(k);
//cout << h << " " << k << " " << i << " " << w << '\n';
mm[{h, k}] = 1;
}
}
for (int i = 1; i <= zz; i++) {
if (db[i] == 0) {
dfs3(i, -1);
ha = f[i];
dfs4(i, -1);
}
}
cout << res * 2;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
7504 KB |
Output is correct |
2 |
Correct |
9 ms |
7504 KB |
Output is correct |
3 |
Correct |
6 ms |
7504 KB |
Output is correct |
4 |
Correct |
8 ms |
7504 KB |
Output is correct |
5 |
Correct |
5 ms |
7520 KB |
Output is correct |
6 |
Correct |
9 ms |
7504 KB |
Output is correct |
7 |
Incorrect |
6 ms |
7504 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
7504 KB |
Output is correct |
2 |
Correct |
9 ms |
7504 KB |
Output is correct |
3 |
Correct |
6 ms |
7504 KB |
Output is correct |
4 |
Correct |
8 ms |
7504 KB |
Output is correct |
5 |
Correct |
5 ms |
7520 KB |
Output is correct |
6 |
Correct |
9 ms |
7504 KB |
Output is correct |
7 |
Incorrect |
6 ms |
7504 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
22020 KB |
Output is correct |
2 |
Correct |
44 ms |
21740 KB |
Output is correct |
3 |
Incorrect |
48 ms |
19016 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
7504 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
117 ms |
23636 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
10496 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
105 ms |
23992 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
7504 KB |
Output is correct |
2 |
Correct |
9 ms |
7504 KB |
Output is correct |
3 |
Correct |
6 ms |
7504 KB |
Output is correct |
4 |
Correct |
8 ms |
7504 KB |
Output is correct |
5 |
Correct |
5 ms |
7520 KB |
Output is correct |
6 |
Correct |
9 ms |
7504 KB |
Output is correct |
7 |
Incorrect |
6 ms |
7504 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
7504 KB |
Output is correct |
2 |
Correct |
9 ms |
7504 KB |
Output is correct |
3 |
Correct |
6 ms |
7504 KB |
Output is correct |
4 |
Correct |
8 ms |
7504 KB |
Output is correct |
5 |
Correct |
5 ms |
7520 KB |
Output is correct |
6 |
Correct |
9 ms |
7504 KB |
Output is correct |
7 |
Incorrect |
6 ms |
7504 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |