This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// In the name of Allah
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define all(x) (x).begin(),(x).end()
#define len(x) ((ll) (x).size())
#define F first
#define S second
#define pb push_back
#define sep ' '
#define endl '\n'
#define Mp make_pair
#define kill(x) cout << x << '\n', exit(0)
#define set_dec(x) cout << fixed << setprecision(x);
#define file_io(x,y) freopen(x, "r", stdin); freopen(y, "w", stdout);
int n, q;
const int maxn = 50 + 4;
bool M[maxn][maxn];
int p[maxn], sz[maxn]; vector<pii> E;
int get(int a) {
return (p[a] == a) ? a : p[a] = get(p[a]);
}
void merge(int a, int b) {
a = get(a); b = get(b);
if (a == b) return ;
p[a] = b; sz[b] += sz[a]; sz[a] = 0;
}
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n >> q;
for (int i = 0; i < n; i++) {
p[i] = i; sz[i] = 1;
}
for (int i = 0; i < q; i++) {
int u1, v1;
cin >> u1 >> v1; u1--; v1--;
E.pb(Mp(u1, v1));
while (true) {
pii res = Mp(-1, -1);
for (auto f : E) {
int u = f.F, v = f.S;
u = get(u); v = get(v);
if (u == v) continue;
M[u][v] = 1;
if (M[v][u]) {
res = Mp(u, v);
}
}
for (auto f : E) {
int u = f.F, v = f.S;
u = get(u); v = get(v);
if (u == v) continue;
M[u][v] = 0;
}
if (res.F == -1) break;
merge(res.F, res.S);
}
ll res = 0;
for (int i = 0; i < n; i++) res += 1ll * sz[i] * (sz[i] - 1);
for (auto f : E) {
int u1 = f.F, v1 = f.S;
int u = get(u1), v = get(v1);
if (u == v) continue;
if (!M[u1][v]) res += sz[v];
M[u1][v] = 1;
}
for (auto f : E) {
int u1 = f.F, v1 = f.S;
int u = get(u1), v = get(v1);
if (u == v) continue;
M[u1][v] = 0;
}
cout << res << endl;
}
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... |