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;
bool gr[N][N], ing[N][N], mem[N][N];
int 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);
}
ans -= w[x] * (w[x] + 1);
ans -= w[y] * (w[y] + 1);
// cout << w[x] * (w[x] + 1) + w[y] * (w[y] + 1) << "\n";
for (int i = 0; i < n; i++) {
if (ing[x][i] && !mem[x][i]) {
ans += w[x];
}
if (ing[y][i] && !mem[y][i]) {
ans += w[y];
}
if (mem[y][i])
mem[x][i] = true;
if (ing[y][i]) {
ing[x][i] = true;
}
}
w[x] += w[y];
w[y] = x;
ans += w[x] * (w[x] + 1);
for (int i = 0; i < n; i++) {
if (ing[x][i] && !mem[x][i]) {
ans -= w[x];
}
}
for (int i = 0; i < n; i++) {
if (gr[i][y])
gr[i][x] = true;
if (gr[y][i])
gr[x][i] = 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) {
y = find_set(y);
if (!ing[y][x]) {
ans -= w[y];
ing[y][x] = true;
}
x = find_set(x);
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);
for (int i = 0; i < n; i++) {
mem[i][i] = true;
}
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
x--, y--;
add_edge(x, y);
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... |