Submission #978395

# Submission time Handle Problem Language Result Execution time Memory
978395 2024-05-09T07:51:32 Z vjudge1 Making Friends on Joitter is Fun (JOI20_joitter2) C++17
0 / 100
0 ms 344 KB
//#pragma GCC optimize("Ofast,O3")
//#pragma GCC target("avx,avx2")
#include <bits/stdc++.h>
using namespace std;



#ifdef LOCAL
#include "debugger.cpp"
#else
#define debug(...)
#endif
template<class T>
int len(T &a){
    return a.size();
}

using ll = long long;
using pii = pair<int,int>;
#define all(a) (a).begin(), (a).end()
#define ff first
#define ss second
string fileio = "";

mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
const int MOD = 1e9 + 7;
const int inf = 1e9;
const ll infl = 1e18;
const int maxn = 1e5 + 1;



struct DSU{
    vector<int> par, sz; int n;
    DSU (int _n){
        n = _n;
        par.resize(n);
        iota(all(par), 0);
        sz.assign(n, 1);
    }
    DSU (){}
    int get(int a){
        return (par[a] == a ? a : par[a] = get(par[a]));
    }

    void init(int _n){
        n = _n;
        par.resize(n);
        iota(all(par), 0);
        sz.assign(n, 1);
    }

    void unite(int a, int b){
        a = get(a);
        b = get(b);
        if(a == b) return;
        if(sz[a] < sz[b]) swap(a,b);
        sz[a] += sz[b];
        par[b] = a;
    }

    void link(int a, int b){
        a = get(a); b = get(b);
        if(a == b) return;
        sz[b] += sz[a];
        par[a] = b;
    }
};

void solve(){
    int n, m; cin >> n >> m;
    #ifdef LOCAL
    m = 1000;
    n = 100;
    #endif

    ll ans = 0;

    vector<set<int>> half(n);
    vector<set<pair<int,int>>> lead(n);

    DSU t(n);

    while(m --){
        #ifdef LOCAL
        cout << endl;
        #endif
        int a, b; cin >> a >> b; a --; b --;
        int lb = t.get(b), la = t.get(a);

        //lb<->la == true

        if(la == lb){
            debug(1);
            cout << ans << '\n';
            continue;
        }
        auto it = lead[la].lower_bound({lb, -1});
        //not b->a
        if(it == lead[la].end() || it->ff != lb){
            // yes a->b 
            if(half[lb].count(a)){
                debug(2);
                cout << ans << '\n';
                continue;
            }

            // no a->b
            it = lead[lb].lower_bound({la, -1});
            int cnt = 1;
            if(it != lead[lb].end()){
                cnt = it->ss + 1;
                lead[lb].erase(it);
            }
            lead[lb].insert({la, cnt});
            ans += t.sz[lb];
            half[lb].insert(a);
            debug(3);
            cout << ans << '\n';
            continue;
        }
        // lb->la == true

        ans -= 1ll * t.sz[la] * it->ss;
        ans += 2ll * t.sz[la] * t.sz[lb];

        if(len(half[la]) > len(half[lb])){
            swap(la, lb);
        }


        for(auto u : half[la]){
            int lu = t.get(u);
            if(lu == lb || lu == la) continue;
            half[lb].insert(u);
            ans += t.sz[lb];
            it = lead[lb].lower_bound({lu, -1});
            int cnt = 1;
            if(it != lead[lb].end() && it->ff == lu){
                cnt = it->ss + 1;
                lead[lb].erase(it);
            }

            lead[lb].insert({lu, cnt});
        }
        half[la].clear();
        lead[la].clear();
        t.link(la, lb);
        debug(4);
        cout << ans << '\n';
    }

}

int main(){
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    auto start = chrono::high_resolution_clock::now();
    #ifndef LOCAL
    if(fileio.size()){
        freopen((fileio + ".in").c_str(), "r", stdin);
        freopen((fileio + ".out").c_str(), "w", stdout);
    }
    #endif
    int testcases = 1;
    #ifdef Tests
    cin >> testcases;
    #endif
    while(testcases --){
        solve();
        if(testcases) cout << '\n';
        #ifdef LOCAL
        else cout << '\n';
        cout << "_________________________" << endl;
        #endif
    }
    #ifdef LOCAL
    auto duration = chrono::duration_cast<chrono::microseconds>(chrono::high_resolution_clock::now() - start);
    
    cout << "time: " << (double)duration.count()/1000.0 << " milliseconds" << endl;
    #endif
    return 0;
}       

Compilation message

joitter2.cpp: In function 'int main()':
joitter2.cpp:157:10: warning: variable 'start' set but not used [-Wunused-but-set-variable]
  157 |     auto start = chrono::high_resolution_clock::now();
      |          ^~~~~
joitter2.cpp:160:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  160 |         freopen((fileio + ".in").c_str(), "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
joitter2.cpp:161:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  161 |         freopen((fileio + ".out").c_str(), "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 344 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 344 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 344 KB Output isn't correct
2 Halted 0 ms 0 KB -