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 <iostream>
#include <vector>
#include <algorithm>
#include <map>
typedef long long ll;
using namespace std;
const int maxn = 1e5 + 5;
vector<int> g[maxn], g2[maxn * 2], siz(maxn, -1), tin(maxn, -1), low(maxn, -1), stk;
int n, m, timer = 0, cnt; ll ans = 0;
int dfs1(int u, int p = -1)
{
stk.push_back(u); int siz = 1;
tin[u] = low[u] = timer++;
for (int v : g[u]) if (v != p)
{
if (tin[v] != -1) low[u] = min(low[u], tin[v]);
else
{
siz += dfs1(v, u);
low[u] = min(low[v], low[u]);
if (low[v] >= tin[u]) // nova artikulacia
{
g2[u].push_back(cnt);
while (g2[cnt].empty() || g2[cnt].back() != v)
{
g2[cnt].push_back(stk.back());
stk.pop_back();
}
cnt++;
}
}
}
return siz;
}
void dfs2(int S, int u, int p = -1)
{
siz[u] = (u < n);
for (int v : g2[u]) if (v != p)
{
dfs2(S, v, u); siz[u] += siz[v];
if (u >= n) ans -= (g2[u].size()) * 1ll * siz[v] * 1ll * (siz[v] - 1);
}
if (u >= n) ans -= (g2[u].size()) * 1ll * (S - siz[u]) * 1ll * (S - siz[u] - 1);
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
for (int i = 0, u, v; i < m; i++) cin >> u >> v, g[--u].push_back(--v), g[v].push_back(u);
cnt = n;
for (int i = 0; i < n; i++) if (tin[i] == -1)
{
ll vr = dfs1(i);
ans += vr * (vr - 1) * (vr - 2);
dfs2(vr, i);
}
//for (int i = 0; i < 2 * n; i++) for (int j : g2[i]) cout << i << " " << j << "\n";
cout << ans << "\n";
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... |