Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Problem: D. Social Network
- // Contest: Codeforces - Deltix Round, Autumn 2021 (open for everyone, rated, Div. 1 + Div. 2)
- // Memory Limit: 256 MB
- // Time Limit: 2000 ms
- // Date / Time: 2021-11-28 17:36:41
- // Author: cosenza
- // всё что ни делается - всё к лучшему
- // check list -> long long, special cases, array size, mod (a*b%p*c%p not a*b*c%p , (a-b+p)%p not a-b )
- //
- // Powered by CP Editor (https://cpeditor.org)
- //#pragma GCC optimize("Ofast")
- //#pragma GCC optimize ("unroll-loops")
- //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
- //THESE ARE LAST DITCH EFFORTS!!!
- #include <bits/stdc++.h>
- using namespace std;
- struct DSU {
- int getSize(int x) { return -par[getPar(x)]; }
- int getPar(int x) {
- while(par[x] >= 0) {
- x = par[x];
- }
- return x;
- }
- bool makeUnion(int a, int b) {
- a = getPar(a), b = getPar(b);
- if(a == b) return false;
- if(par[a] > par[b]) std::swap(a, b);
- op.emplace_back(a, par[a]);
- op.emplace_back(b, par[b]);
- par[a] += par[b];
- par[b] = a;
- return true;
- }
- void init(int n) {
- par.resize(n);
- for(int i = 0; i < n; i++) {
- par[i] = -1;
- }
- op.clear();
- }
- void rollBack() {
- par[op.back().first] = op.back().second;
- op.pop_back();
- }
- std::vector<int> par;
- std::vector<std::pair<int, int>> op;
- };
- int main() {
- ios_base::sync_with_stdio(false);
- cin.tie(0);
- int n, d;
- cin >> n >> d;
- DSU dsu;
- dsu.init(n);
- int k = 1;
- while(d--) {
- int x, y;
- vector<int> s;
- cin >> x >> y;
- x--; y--;
- if(!dsu.makeUnion(x, y)) {
- k++;
- }
- for(int i = 0; i < n; i++) {
- if(dsu.getPar(i) == i) {
- s.push_back(dsu.getSize(i));
- }
- }
- sort(s.begin(), s.end(), std::greater<int>());
- int ans = 0;
- for(int i = 0; i < k and i < s.size(); i++) {
- ans += s[i];
- }
- cout << --ans << "\n";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement