이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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), col(maxn, -1), my(maxn, 0), stk;
vector<pair<int, int> > e;
int n, m, timer = 0, cnt = 0; 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])
{
e.push_back({ u, v });
while (!stk.empty())
{
int vr = stk.back();
col[vr] = cnt, my[cnt]++, stk.pop_back();
if (vr == v) break;
}
cnt++;
}
}
}
if (p == -1)
{
for (int i : stk) col[i] = cnt, my[cnt]++;
cnt++; stk.clear();
}
return siz;
}
void dfs2(int S, int u, int p = -1)
{
siz[u] = my[u];
for (int v : g2[u]) if (v != p)
{
dfs2(S, v, u); siz[u] += siz[v];
ans -= my[u] * 1ll * siz[v] * 1ll * (siz[v] - 1);
}
ans -= my[u] * 1ll * (S - siz[u]) * 1ll * (S - siz[u] - 1);
ans -= (my[u] - 1) * 2ll * (S - my[u]);
}
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);
for (int i = 0; i < n; i++) if (tin[i] == -1)
{
e.clear();
ll vr = dfs1(i);
ans += vr * (vr - 1) * (vr - 2);
for (pair<int, int> j : e) g2[col[j.first]].push_back(col[j.second]), g2[col[j.second]].push_back(col[j.first]);
dfs2(vr, col[i]);
}
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... |