/* Author : Mychecksdead */
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define cerr if(false) cerr
#define MOD (1000000000+7)
#define MOD1 (998244353)
#define pb push_back
#define all(x) x.begin(), x.end()
#define en cout << '\n'
#define ff first
#define ss second
#define pii pair<int,int>
#define vi vector<int>
const int N = 5000+100, M = 1e5+10, K = 52, MX = 30;
struct Dsu {
vector<int> s, p;
int sz;
Dsu(int n){
sz = n;
s.resize(n+1, 1);
p.resize(n+1);
for(int i = 0; i <= n; ++i) p[i] = i;
}
int find(int v){
if(p[v] == v) return v;
return (p[v] = find(p[v]));
}
void merge(int a, int b){
a = find(a);
b = find(b);
if(a != b){
if(s[a] > s[b]){
swap(a, b);
}
s[b] += s[a];
p[a] = b;
sz--;
}
}
};
int n, m;
vector<pii> E;
bitset<N> is[N];
void solve(){
cin >> n >> m;
Dsu d(n);
for(int i = 1; i <= m; ++i){
int u, v; cin >> u >> v;
bool ok = false;
for(auto [x, y]: E){
if(d.find(x) == d.find(v) && y == d.find(u)){
ok = 1;
break;
}
}
E.pb({u, d.find(v)});
if(ok){
d.merge(u, v);
for(auto &[x, y]: E){
x = d.find(x);
y = d.find(y);
}
}
vector<vi> g(n+1);
for(auto [x, y]: E){
if(d.find(x) != d.find(y)) g[x].pb(d.find(y));
}
int ans = 0;
for(int j = 1; j <= n; ++j){
sort(all(g[j]));
g[j].erase(unique(all(g[j])), g[j].end());
for(int y: g[j]) ans += d.s[y];
if(d.find(j) == j){
ans += d.s[j] * d.s[j] - d.s[j];
}
}
cout << ans << '\n';
}
}
int main(){
cin.tie(0); ios::sync_with_stdio(0);
int tt = 1, aa;
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
while(tt--){
solve();
en;
}
cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" seconds\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... |