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>
typedef long long ll;
using namespace std;
const int lim = 1e5 + 5;
ll n, m, N, ans, sz[lim << 1];
vector<int> graph[lim], bcc_graph[lim], stck;
int low[lim], tin[lim], timer = 1, bccs = 1;
void dfs(int node, int parent = 0) {
low[node] = tin[node] = timer++;
stck.push_back(node);
N++;
for (int i : graph[node])
if (i != parent) {
if (tin[i]) low[node] = min(low[node], tin[i]);
else {
dfs(i, node);
low[node] = min(low[node], low[i]);
if (low[i] >= tin[node]) {
bcc_graph[node].push_back(n + bccs);
while (!bcc_graph[n + bccs].size() ||
bcc_graph[n + bccs].back() != i) {
bcc_graph[n + bccs].push_back(stck.back());
stck.pop_back();
}
bccs++;
}
}
}
}
void dfs2(int node) {
sz[node] = (node <= n);
for (int i : bcc_graph[node]) {
dfs2(i);
sz[node] += sz[i];
if (node > n) ans -= bcc_graph[node].size() * sz[i] * (sz[i] - 1);
}
if (node > n) ans -= bcc_graph[node].size() * (N - sz[node]) * (N - sz[node] - 1);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
while (m--) {
int u, v;
cin >> u >> v;
graph[u].push_back(v);
graph[v].push_back(u);
}
for(int i = 0; i < n; i++) if (!tin[i]) {
N = 0;
dfs(i);
ans += N * (N - 1) * (N - 2);
dfs2(i);
}
cout << ans;
return 0;
}
# | 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... |