# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1278852 | janson0109 | Bridges (APIO19_bridges) | C++20 | 0 ms | 0 KiB |
#include <bits/stdc++.h>
#define F first
#define S second
#define lol ios::sync_with_stdio(false);cin.tie(NULL);
typedef int ll;
typedef long double ld;
using namespace std;
const ll M = 998244353;
#define p3 tuple<ll,ll,ll>
ll find(ll x, vector<ll> &p, vector<pair<ll,ll>> &ph, bool his) {
if(p[x] == x) return x;
//if(his) ph.push_back({x, p[x]});
return find(p[x], p, ph, his);
//return p[x];
}
bool unite(ll x, ll y, vector<ll> &p, vector<ll> &s, vector<pair<ll,ll>> &ph, vector<pair<ll,ll>> &sh, bool his) {
ll xr = find(x, p, ph, his);
ll yr = find(y, p, ph, his);
if(xr == yr) return false;
if(s[xr] < s[yr]) swap(xr,yr);
if(his) {
sh.push_back({xr, s[xr]});
ph.push_back({yr, p[yr]});
}
s[xr]+=s[yr];
p[yr]=xr;
return true;
}
void rollback(vector<ll> &p, vector<ll> &s, vector<pair<ll,ll>> &ph, vector<pair<ll,ll>> &sh) {
while(!ph.empty()) {
pair<ll,ll> pa = ph.back();
p[pa.F] = pa.S;
ph.pop_back();
}
while(!sh.empty()) {
pair<ll,ll> pa = sh.back();
s[pa.F] = pa.S;
sh.pop_back();
}
}
int main() {
lol
ll n, m;
cin >> n >> m;
vector<p3> e; //set<p3, greater<p3>> es;
for(ll i=0; i<m; i++) {
ll u, v, d;
cin >> u >> v >> d;
u--;v--;
e.push_back({d, u, v});
es.insert(e.back());
}
ll q; cin >> q;
vector<p3> qu(q);
for(ll i=0; i<q; i++) cin >> get<0>(qu[i]) >> get<1>(qu[i]) >> get<2>(qu[i]);
ll b = sqrtl(q);
for(ll t=0; t < q; t+=b) {
ll c = min(b, q-t);
vector<bool> ch(m,0);
for(ll i=0; i<c; i++) if(get<0>(qu[t+i]) == 1) ch[get<1>(qu[t+i]) - 1] = 1;
vector<ll> mod; set<p3> mods;
for(ll i=0; i<m; i++) if(ch[i]) {mod.push_back(i); mods.insert(e[i]);}
ll k = mod.size(); vector<ll> modm(m, -1);
vector<ll> nd(k);
for(ll i=0; i<k; i++) {
nd[i] = get<0>(e[mod[i]]);
modm[mod[i]] = i;
}
vector<vector<ll>> ndm(c);
for(ll i=0; i<c; i++) {
if(get<0>(qu[t+i]) == 1) nd[modm[get<1>(qu[t+i]) - 1]] = get<2>(qu[t+i]);
else ndm[i] = nd;
}
vector<p3> qus;
for(ll i=0; i<c; i++) if(get<0>(qu[t+i]) == 2) qus.push_back({get<2>(qu[t+i]), get<1>(qu[t+i])-1, i});
sort(qus.begin(), qus.end(), greater<p3>());
vector<ll> p(n), s(n,1);
iota(p.begin(), p.end(), 0);
vector<ll> ans(c, -1);
vector<pair<ll,ll>> ph, sh;
vector<p3> uc;
for(auto & x : e) if(mods.find(x) == mods.end()) uc.push_back(x);
sort(uc.begin(), uc.end(), greater<p3>());
auto it = uc.begin();
for(auto & quer : qus) {
while(it != uc.end() && get<0>(quer) <= get<0>(*it)) {
unite(get<1>(*it), get<2>(*it), p, s, ph, sh, 0); it++;
}
for(ll j=0; j<k; j++) if(get<0>(quer) <= ndm[get<2>(quer)][j]) unite(get<1>(e[mod[j]]), get<2>(e[mod[j]]), p, s, ph, sh, 1);
ans[get<2>(quer)] = s[find(get<1>(quer), p, ph, 1)];
rollback(p, s, ph, sh);
}
for(ll i=0; i<c; i++) if(get<0>(qu[t+i]) == 1) {
//es.erase(es.find(e[get<1>(qu[t+i]) - 1]));
get<0>(e[get<1>(qu[t+i]) - 1]) = get<2>(qu[t+i]);
//es.insert(e[get<1>(qu[t+i]) - 1]);
}
for(ll i=0; i<c; i++) if(ans[i] != -1) cout << ans[i] << '\n';
}
return 0;
}