Submission #1127433

#TimeUsernameProblemLanguageResultExecution timeMemory
1127433wibulordBridges (APIO19_bridges)C++20
100 / 100
1761 ms205724 KiB
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define pb emplace_back
#define ii pair<int, int>
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
#define sz(s) (int)((s).size())
#define all(a) a.begin(), a.end()
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define F0R(i, b) for (int i = 0, _b = (b); i < _b; ++i)
#define FORd(i, a, b) for (int i = (a), _b = (b); i >= _b; --i)
#define F0Rd(i, b) for (int i = (b)-1; i >= 0; i--)
#define debug(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
using namespace std;

template<typename T1,typename T2> bool ckmax(T1 &x,const T2 &y){if(x<y){x=y; return 1;} return 0;}
template<typename T1,typename T2> bool ckmin(T1 &x,const T2 &y){if(y<x){x=y; return 1;} return 0;}

const int MOD = (int)1e9 + 7;
const int mod = 998244353;
const int N = 1e5 + 10, S = 1e3 + 1e2;
const long long INF = (long long)4e18 + 11;

/*
     /\_/\
    (= ._.)
    / >?  \>$
*/

int n, m, q;
int par[N], res[N];
int u[N], v[N], w[N];
int t[N], x[N], y[N];

stack<ii> st;
int Find(int v){
    while(par[v] > 0) v = par[v];
    return v;
}

bool Union(int u, int v){
    u = Find(u), v = Find(v);
    if(u == v) return false;
    if(par[u] > par[v]) swap(u, v);
    st.push({u, par[u]});
    st.push({v, par[v]});
    par[u] += par[v], par[v] = u;
    return true;
}

void rollback(int tms){
    while(sz(st) > tms){
        auto [u, parU] = st.top(); st.pop();
        par[u] = parU;
        auto [v, parV] = st.top(); st.pop();
        par[v] = parV;
    }
}

bool mark[N];
vector<int> Q;
vector<int> changed;
vector<int> rem, adj[N];

void sol(void){
    cin >> n >> m;
    FOR(i, 1, m) cin >> u[i] >> v[i] >> w[i];
    cin >> q;
    FOR(i, 1, q) cin >> t[i] >> x[i] >> y[i];

    FOR(i, 1, n) par[i] = -1;
    for(int l = 1; l <= q; l += S){
        int r = min(q, l + S - 1);
//        cout << l << ' ' << r << '\n';
        Q.clear(); changed.clear(); rem.clear();
        FOR(i, l, r){
            if(t[i] == 1){
                if(mark[x[i]] == false){
                    mark[x[i]] = true;
                    changed.push_back(x[i]);
                }
            }
            else Q.push_back(i);
        }
        FOR(i, 1, m) if(mark[i] == false) rem.push_back(i);
        FOR(i, l, r){
            if(t[i] == 1) w[x[i]] = y[i];
            else{
                for(int id : changed) if(w[id] >= y[i]) adj[i].push_back(id);
            }
        }
        sort(all(rem), [&](int a, int b){
            return w[a] > w[b];
        });
        sort(all(Q), [&](int a, int b){
             return y[a] > y[b];
        });
        int j = 0;
        for(int i : Q){
            while(j < sz(rem) && w[rem[j]] >= y[i]){
                Union(u[rem[j]], v[rem[j]]);
                j++;
            }
            int old = sz(st);
            for(int id : adj[i]) Union(u[id], v[id]);
            res[i] = -par[Find(x[i])];
            rollback(old);
            adj[i].clear();
        }
        rollback(0);
        FOR(i, l, r) if(t[i] == 1) mark[x[i]] = false;
    }
    FOR(i, 1, q) if(t[i] == 2) cout << res[i] << '\n';
}

signed main(void){
    #define TASK "nhap"
    if(fopen(TASK".inp", "r")){
        freopen(TASK".inp", "r", stdin);
        freopen(TASK".out", "w", stdout);
    }
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    int t = 1;
//    cin >> t;
    while(t--) sol();
    cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << " ms\n";
}

Compilation message (stderr)

bridges.cpp: In function 'int main()':
bridges.cpp:121:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  121 |         freopen(TASK".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
bridges.cpp:122:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  122 |         freopen(TASK".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...