Submission #1276688

#TimeUsernameProblemLanguageResultExecution timeMemory
1276688wedonttalkanymoreBridges (APIO19_bridges)C++20
16 / 100
3095 ms12248 KiB
#include <bits/stdc++.h>
/*
    Brute to win
*/
using namespace std;
using ll = long long;

#define int long long
#define pii pair<ll, ll>
#define fi first
#define se second

const ll N = 2e5 + 5, inf = 1e18, mod = 1e9 + 7, block = 320, lim = 19;

int n, m, q;
int u[N], v[N], w[N];
struct queries {
    int type, a, b;
};
queries query[N];
int L[N], R[N];
int ptr;
int ans[N];

void preprocess() {
    for (int i = 1; i <= q; i++) {
        int idx = i / block;
        if (L[idx] == 0) L[idx] = i;
        R[idx] = i;
    }
}

struct DSU {
    int par[N], sz[N];
    pii s[N];
    void make() {
        for (int i = 1; i <= n; i++) {
            par[i] = i;
            sz[i] = 1;
        }
    }
    int find(int u) {
        if (u == par[u]) return u;
        return find(par[u]);
    }
    void join(int u, int v) {
    	u = find(u), v = find(v);
        if (u != v) {
            if (sz[u] < sz[v]) swap(u, v);
            s[++ptr] = make_pair(sz[v], v);
            par[v] = u;
            sz[u] += sz[v];
        }
    }
    void rollback(int pos) {
        while(ptr > pos) {
            auto [val, v] = s[ptr];
            int u = par[v];
            sz[u] -= val;
            sz[v] = val;
            par[v] = v;
            --ptr;
        }
    }
};

DSU dsu;

bool cmp(int a, int b) {
    return w[a] > w[b];
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    if (fopen(".inp", "r")) {
        freopen(".inp", "r", stdin);
        freopen(".out", "w", stdout);
    }
    cin >> n >> m;
    for (int i = 1; i <= m; i++) cin >> u[i] >> v[i] >> w[i];
    cin >> q;
    for (int i = 1; i <= q; i++) {
        cin >> query[i].type >> query[i].a >> query[i].b;
    }
    preprocess();
    int bls = q / block;
    for (int i = 0; i <= bls; i++) {
    	ptr = 0;
        dsu.make();
        vector<int> unchange;
        vector<pii> qry;
        vector<int> check(m + 1, 0);
        vector <vector<int> > upd;
        if (L[i] == 0) continue;
        for (int j = L[i]; j <= R[i]; j++) {
            if (query[j].type == 1) {
                int pos = query[j].a;
                check[pos] = 1;
                upd.push_back({pos, query[j].b, j, w[pos]});
            }
            else {
                qry.emplace_back(query[j].b, j);
            }
        }
        for (int j = 1; j <= m; j++) if (!check[j]) unchange.emplace_back(j);
        sort(unchange.begin(), unchange.end(), cmp);
        sort(qry.begin(), qry.end(), greater<pii>());

        int l = 0;
        for (int j = 0; j < qry.size(); j++) {
            int value = qry[j].fi;
            int idx = qry[j].se;
            while(l < unchange.size()) {
                int t = unchange[l];
                if (w[t] < value) break;
                dsu.join(u[t], v[t]);
                l++;
            }
            int pre = ptr;
            for (auto x : upd) {
                int pos = x[0], newval = x[1], id = x[2], orig = x[3];
                int use = (id < idx ? newval : orig);
                if (use >= value) dsu.join(u[pos], v[pos]);
            }
            int st = query[idx].a;
            int parent = dsu.find(st);
            ans[idx] = dsu.sz[parent];
            dsu.rollback(pre);
        }
        for (auto x : upd) w[x[0]] = x[1];
    }
    for (int i = 1; i <= q; i++) if (query[i].type == 2) cout << ans[i] << '\n';
    return 0;
}

Compilation message (stderr)

bridges.cpp: In function 'int main()':
bridges.cpp:77:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   77 |         freopen(".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~
bridges.cpp:78:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   78 |         freopen(".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...