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>
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... |