Problem - 1609D - Codeforceshttps://codeforces.com/problemset/problem/1609/D
int pre[MAXN];
int n, k;
bool vis[MAXN];
int sum[MAXN];
inline void init() {
for (int i = 1; i <= n; i++) {
pre[i] = i;
sum[i] = 1;
}
}
int find(int x) {
if (x == pre[x])
return x;
return pre[x] = find(pre[x]);
}
inline bool same(int x, int y) {
return find(x) == find(y);
}
int main() {
int x, y;
scanf("%d %d", &n, &k);
init();
int cnt = 0;
priority_queue<int> que;
// int mx=0;
for (int i = 1; i <= k; i++) {
// scanf("%d %d",&s[i].x,&s[i].y);
scanf("%d %d", &x, &y);
// vis[x]=1,vis[y]=1;
if (same(x, y)) {
cnt++;
} else {
x = find(x);
y = find(y);
pre[y] = x;
sum[x] += sum[y];
}
while (!que.empty()) {
que.pop();
// printf("===\n");
}
for (int i = 1; i <= n; i++) {
if (i == find(i)) {
que.push(sum[i]);
}
}
int ans = 0;
ans += que.top();
que.pop();
for (int i = 1; i <= cnt; i++) {
ans += que.top();
que.pop();
}
printf("%d\n", ans - 1);
}
return 0;
}