This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
int n, m, subsz[N], dp[N];
vector<int> g[N], comp;
bool vis[N], possible[105][105][105];
void dfs_brute(int v, int y, int z){
if (v == z) return;
vis[v] = 1;
for (int u : g[v])
if (!vis[u]) dfs_brute(u, y, z);
}
void dfs(int v, int p = -1){
vis[v] = 1;
comp.push_back(v);
subsz[v] = 1;
for (int u : g[v]){
if (vis[u]) continue;
dfs(u, v);
subsz[v] += subsz[u];
}
int cur = 0;
for (int u : g[v]){
if (u == p) continue;
dp[v] += subsz[u] * cur;
cur += subsz[u];
}
}
void dfs_up(int v, int p = -1){
for (int u : g[v]){
if (u == p) continue;
dfs_up(u, v);
}
dp[v] += (comp.size() - subsz[v]) * (subsz[v] - 1);
}
int main(){
cin >> n >> m;
for (int i = 0; i < m; i ++){
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
int total = n * (n - 1) * (n - 2);
if (n <= 100 and m <= 100){
for (int u = 1; u <= n; u ++){
for (int v = 1; v <= n; v ++){
for (int e = 1; e <= n; e ++){
for (int x = 1; x <= n; x ++)
vis[x] = 0;
dfs_brute(u, v, e);
if (vis[v])
possible[u][v][e] = 1;
}
}
}
for (int c = 1; c <= n; c ++){
for (int s = 1; s <= n; s ++){
for (int f = 1; f <= n; f ++){
if (c == s or c == f or s == f) continue;
bool bad = 0;
for (int v = 1; v <= n; v ++){
if (v == c) continue;
if (!possible[s][c][v] and !possible[c][f][v])
bad = 1;
}
total -= bad;
}
}
}
cout << total << endl;
return 0;
}
ll ans = 0;
for (int v = 1; v <= n; v ++){
if (vis[v]) continue;
comp.clear();
dfs(v);
int edges = 0;
for (int x : comp)
edges += g[x].size();
edges /= 2;
ll nodes = comp.size();
if (nodes == edges){
ans += nodes * (nodes - 1) * (nodes - 2);
continue;
}
dfs_up(v);
for (int u : comp)
ans += dp[u] * 2;
}
cout << ans << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |