Submission #478759

#TimeUsernameProblemLanguageResultExecution timeMemory
478759KarliverBridges (APIO19_bridges)C++17
60 / 100
2687 ms41376 KiB
    
#include <bits/stdc++.h>

#define FIXED_FLOAT(x)  std::fixed <<std::setprecision(20) << (x)
#define all(v) (v).begin(), (v).end()
using namespace  std;
#define forn(i,n) for (int i = 0; i < (n); ++i)
#define rforn(i, n) for(int i = (n) - 1;i >= 0;--i)
#define sz(x) (int)x.size()

using ll = long long;
int mod = (ll)1e9 + 7;
#define PI acos(-1)
typedef pair<int, int> pairs;

const int INF = 1e9 + 1;
const int N = 1e5 + 100;
const double eps = 1e-7;

template <class T> using V = vector<T>;  
template <class T> using VV = V<V<T>>;  
template<class T, size_t SZ> using AR = array<T, SZ>;
template<class T> using PR = pair<T, T>;
template <typename XPAX>
bool ckma(XPAX &x, XPAX y) {
    return (x < y ? x = y, 1 : 0);
}
template <typename XPAX>
bool ckmi(XPAX &x, XPAX y) {
    return (x > y ? x = y, 1 : 0);
}

const int B = 600;
int n, m, q;
stack<int> st;

int SI[N], R[N];

void reset() {
    iota(R + 1, R + n + 1, 1);
    fill(SI + 1, SI + n + 1, 1);
}

inline int root(int x) {
    while(x != R[x])x = R[x];
    return x;
}

void merge(int x, int y) {
    x = root(x), y = root(y);
    if(x == y)
        return ;
    if(SI[x] > SI[y])
        swap(x, y);

    st.push(x);
    SI[y] += SI[x];
    R[x] = y;
}

void rollback(int p) {
    while(sz(st) > p) {
        int x = st.top();
        st.pop();
        SI[R[x]] -= SI[x];
        R[x]= x;
    }
}
int u[N], v[N], w[N], t[N], x[N], y[N];
V<int> to_join[B];
bool changed[N];
int ans[N];
void solve() {


    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 >> t[i] >> x[i] >> y[i];

    for(int l = 1;l <= q;l += B) {
        int r = min(q + 1, l + B);
        reset();
        fill(changed + 1, changed + n + 1, false);

        V<int> upd, ask, unchanged;

        for(int i = l;i < r;++i) {
            if(t[i] == 1) {
                changed[x[i]] = true;
                upd.push_back(i);
            }
            else ask.push_back(i);
        }
        for(int i = 1;i <= m;++i)if(!changed[i])unchanged.push_back(i);

        for(int i = l;i < r;++i) {
            if(t[i] == 1)w[x[i]] = y[i];
            else {
                to_join[i - l].clear();
                for(int j : upd) if(w[x[j]] >= y[i])to_join[i - l].push_back(x[j]);
            }
        }

        sort(all(ask), [&](int a, int b) {return y[a] > y[b];});
        sort(all(unchanged), [&](int a, int b) {return w[a] > w[b];});

        int pt = 0;
        for(auto i : ask) {
            while(pt < sz(unchanged) && w[unchanged[pt]] >= y[i]) {
                merge(u[unchanged[pt]], v[unchanged[pt]]);
                ++pt;
            }

            int pr = sz(st);

            for(auto j : to_join[i - l]) merge(u[j], v[j]);

            ans[i] = SI[root(x[i])];

            rollback(pr);
        }
    }

    for(int i= 1;i <= q;++i)if(t[i] == 2)cout << ans[i] << '\n';



}
void test_case() {
    int t;
    cin >> t;
    forn(p, t) {

        //cout << "Case #" << p + 1 << ": ";
        solve();
    }
}
int main() {

    ios::sync_with_stdio(false);
    cin.tie(0);

    solve();

}
#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...