Untitled

mail@pastecode.io avatar
unknown
plain_text
5 months ago
3.3 kB
1
Indexable
Never
#include "bits/stdc++.h"
using namespace std;

#ifdef LOCAL
#include "debug.h"
#else
#define debug(x...)
#endif

constexpr int K = 5;
constexpr int N = 1e5;
int dp[K][N];

vector <int> g[300010];
vector <int> v;
int n, m;

int stupSolve() {
    int ans = 0;
    for (int i = 0; i < n; i++) {
        for (int &u : g[i]) {
            ans += (v[u] != v[i]);
        }
    }
    return ans / 2;
}

void solve() {
    //cin >> n >> m;
    n = 10;
    m = 20;
    v.resize(n);
    for (int i = 0; i < n; i++) g[i].clear();
    for (int &i : v) {
        //cin >> i;
        i = rand() % 10 + 1;
        i--;
    }
    int ans = 0;
    for (int i = 0; i < m; i++) {
        int a, b;
        //cin >> a >> b;
        a = rand() % n + 1;
        b = rand() % n + 1;
        while (b == a) b = rand() % n + 1;
        a--; b--;
        if (v[a] != v[b]) {
            ans++;
        }
        g[a].push_back(b);
        g[b].push_back(a);
    }
    auto is_heavy = [&](int a) {
        return (int)g[a].size() >= K;
    };
    int cur = 0;
    int mp[n];
    for (int i = 0; i < n; i++) {
        sort(g[i].begin(), g[i].end(), [&](int lhs, int rhs) {
            return g[lhs].size() > g[rhs].size();
        });
        if (is_heavy(i)) {
            mp[i] = cur++;
            fill(dp[mp[i]], dp[mp[i]] + N, 0);
        }
    }
    for (int i = 0; i < n; i++) {
        if (is_heavy(i)) {
            for (int &u : g[i]) {
                if (!is_heavy(u)) {
                    dp[mp[i]][v[u]]++;
                }
            }
        }
    }
    int q;
    //cin >> q;
    while (q--) {
        int C, V;
        //cin >> C >> V;
        C = rand() % n + 1;
        V = rand() % 20 + 1;
        C--; V--;
        if (v[C] == V) {
            //cout << ans << " " << stupSolve() << '\n';
            if (ans != stupSolve()) cout << "FAIL" << endl;
            continue;
        }
        if (!is_heavy(C)) {
            for (int &u : g[C]) {
                if (!is_heavy(u)) {
                    if (v[u] == v[C]) {
                        ans++;
                    }
                    if (v[u] == V) {
                        ans--;
                    }
                } else {
                    dp[mp[u]][v[C]]--;
                    dp[mp[u]][V]++;
                    if (v[u] == v[C]) {
                        ans++;
                    }
                    if (v[u] == V) {
                        ans--;
                    }
                }
            }
        } else {
            for (int &u : g[C]) {
                if (!is_heavy(u)) break;
                if (v[u] == v[C]) {
                    ans++;
                }
                if (v[u] == V) {
                    ans--;
                }
            }
            ans -= dp[mp[C]][v[C]];
            ans += dp[mp[C]][V];
        }
        v[C] = V;
        //stupSolve();
        if (ans != stupSolve()) cout << "FAIL" << endl;
        //cout << ans << " " << stupSolve() << '\n';
    }
}

int main() {
    //ios::sync_with_stdio(false);
    //cin.tie(NULL);
    int tests = 1000;
    // cin >> tests;
    while (tests--) {
        solve();
    }
    return 0;
}