이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(aaa) aaa.begin(), aaa.end()
const int N = 2005;
vector<int> outgoing_edges[N], ingoing_edges[N];
int gr[N][N], w[N];
ll ans = 0;
int n, m;
int find_set(int x) {
return w[x] < 0 ? x : w[x] = find_set(w[x]);
}
void mrg(int x, int y) {
x = find_set(x);
y = find_set(y);
if (x == y)
return;
if (w[x] > w[y]) {
swap(x, y);
}
w[x] += w[y];
w[y] = x;
for (int i = 0; i < n; i++) {
if (gr[y][i])
gr[x][i] = true;
if (gr[i][y])
gr[i][x] = true;
}
for (int i = 0; i < n; i++) {
if (gr[x][i] && gr[i][x])
mrg(x, i);
}
}
void add_edge(int x, int y) {
x = find_set(x);
y = find_set(y);
if (x == y || gr[x][y])
return;
gr[x][y] = true;
if (gr[y][x])
mrg(x, y);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
fill(w, w + n, -1);
vector<pair<int, int>> v;
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
x--, y--;
add_edge(x, y);
v.push_back({x, y});
for (auto &p : v) {
p.first = find_set(p.first);
p.second = find_set(p.second);
}
sort(all(v));
v.erase(unique(all(v)), v.end());
ll ans = 0;
for (auto &p : v) {
if (p.first != p.second)
ans -= w[p.second];
}
for (int j = 0; j < n; j++) {
if (w[j] < 0)
ans += w[j] * (w[j] + 1);
}
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... |