이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <vector>
using namespace std;
pair<int, int> e[200005];
vector<pair<int, int>> g[200005], gg[200005];
long long int dd[200005], a[200005], b[200005], tin[200005], f[200005], s[200005], z = 0;
vector<int> v;
void dfs(int x, int p) {
tin[x] = f[x] = ++z;
for (auto w : g[x]) {
if (w.first == p) continue;
if (tin[w.first]) {
f[x] = min(f[x], tin[w.first]);
}
else {
dfs(w.first, x);
f[x] = min(f[x], f[w.first]);
if (tin[x] < f[w.first]) {
dd[w.second] = 1;
}
}
}
}
void dfs2(int x, int h) {
b[x] = h;
a[h]++;
for (auto w : g[x]) {
if (b[w.first] || dd[w.second]) continue;
dfs2(w.first, h);
}
}
long long int res = 0, d = 0;
void dfs3(int x, int p) {
v.push_back(x);
s[x] = a[x];
d += a[x];
tin[x] = 1;
long long int h = 0;
for (auto w : gg[x]) {
if (w.first == p) continue;
dfs3(w.first, x);
s[x] += s[w.first];
}
}
long long int ff[200005];
void dfs4(int x, int p) {
long long int h = 0;
for (auto w : gg[x]) {
long long int k;
if (w.first == p) {
k = d - s[x];
}
else {
dfs4(w.first, x);
k = s[w.first];
}
res += h * k * a[x];
res += k * (1 - a[x]) * ff[w.second];
ff[w.second] += k;
h += k;
//cout << x << " " << a[x] << " " << res << " " << w.second << " " << ff[w.second] << " " << k << " " << h << '\n';
}
for (auto w : gg[x]) ff[w.second] = 0;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n, m;
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int u, v;
cin >> u >> v;
//cout << u << " " << v << '\n';
e[i] = {u, v};
g[u].push_back({v, i});
g[v].push_back({u, i});
}
for (int i = 1; i <= n; i++) {
if (tin[i] == 0) {
dfs(i, -1);
}
}
z = 0;
for (int i = 1; i <= n; i++) {
if (b[i] == 0) {
z++;
dfs2(i, z);
}
}
for (int i = 1; i <= m; i++) {
if (dd[i] == 1) {
gg[b[e[i].first]].push_back({b[e[i].second], e[i].first});
gg[b[e[i].second]].push_back({b[e[i].first], e[i].second});
}
}
for (int i = 1; i <= z; i++) tin[i] = 0;
for (int i = 1; i <= z; i++) {
if (tin[i] == 0) {
v.clear();
d = 0;
dfs3(i, -1);
dfs4(i, -1);
for (int w : v) {
//cout << a[w] << " " << res << '\n';
res += (a[w] - 1) * a[w] * a[w] / 2;
res += d - a[w];
res += (a[w] - 1) * a[w] * (d - a[w]);
}
res -= d * (d - 1);
}
}
cout << res * 2;
}
컴파일 시 표준 에러 (stderr) 메시지
count_triplets.cpp: In function 'void dfs3(int, int)':
count_triplets.cpp:38:19: warning: unused variable 'h' [-Wunused-variable]
38 | long long int h = 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... |