답안 #978431

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
978431 2024-05-09T08:17:07 Z vjudge1 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++17
0 / 100
0 ms 348 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);
    map<pair<int,int>, vector<int>> lead;

    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;
        }
        //not b->a
        if(lead.count({la, lb}) == 0){
            // yes a->b 
            if(half[lb].count(a)){
                debug(2);
                cout << ans << '\n';
                continue;
            }

            // no a->b
            lead[{lb, la}].push_back(a);
            ans += t.sz[lb];
            half[lb].insert(a);
            debug(3);
            cout << ans << '\n';
            continue;
        }
        // lb->la == true

        ans -= 1ll * t.sz[la] * len(lead[{la, lb}]);
        for(auto u : lead[{la, lb}]) half[la].erase(u);
        lead[{la, lb}].clear();

        debug(ans);
        ans += 2ll * t.sz[la] * t.sz[lb];
        ans += len(half[lb]) * t.sz[la];
        ans += len(half[la]) * t.sz[lb];

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


        for(auto u : half[la]){
            int lu = t.get(u);
            half[lb].insert(u);
            lead[{lb, lu}].push_back(u);
            lead[{la, lu}].clear();
        }
        half[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:148:10: warning: variable 'start' set but not used [-Wunused-but-set-variable]
  148 |     auto start = chrono::high_resolution_clock::now();
      |          ^~~~~
joitter2.cpp:151:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  151 |         freopen((fileio + ".in").c_str(), "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
joitter2.cpp:152:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  152 |         freopen((fileio + ".out").c_str(), "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -