This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <queue>
#include <stack>
#include <algorithm>
#include <string.h>
#include <functional>
#define task ""
#define size() size() * 1ll
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define pii pair<int, int>
#define fi first
#define se second
#define MASK(x) (1LL << (x))
#define BIT(x,i) (((x) >> (i)) & 1)
#define numbit(x) __builtin_popcountll(x)
using namespace std;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef long long ll;
template<class t>
bool mini(t &x,t y) {
if (y < x) {
x = y;
return 1;
}
return 0;
}
template<class t>
bool maxi(t &x,t y) {
if (x < y) {
x = y;
return 1;
}
return 0;
}
const int N = 1e6 + 1;
const int M = 1e3 + 1;
const long long mod = 1e9 + 7;
const long long oo = 1e18 + 7;
int n, m, q;
int u[N], v[N], w[N], t[N], id[N], z[N], lab[N], ans[N];
vi b[N];
bool changed[N];
stack<array<int, 3>> st;
int root(int u) {
return (lab[u] < 0 ? u : root(lab[u]));
}
bool join(int u, int v) {
if ((u = root(u)) == (v = root(v))) return 0;
if (lab[u] > lab[v]) swap(u, v);
st.push({u, v, lab[v]});
lab[u] += lab[v];
lab[v] = u;
return 1;
}
void roll_back(int sz) {
while (st.size() > sz) {
int u = st.top()[0];
int v = st.top()[1];
int w = st.top()[2];
st.pop();
lab[u] -= w;
lab[v] = w;
}
}
void solve(int test = -1) {
memset(lab, -1, sizeof lab);
memset(ans, -1, sizeof ans);
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] >> id[i] >> z[i];
}
int blsz = 1000;
for (int l = 1; l <= q; l += blsz) {
int r = min(q, l + blsz - 1);
vi calc, upd, unchanged;
for (int i = l; i <= r; i++) {
if (t[i] == 1) {
changed[id[i]] = 1;
upd.pb(i);
} else {
calc.pb(i);
}
}
for (int i = 1; i <= m; i++) if (!changed[i]) unchanged.pb(i);
sort(all(unchanged), [&](const int &x, const int &y) {
return w[x] > w[y];
});
sort(all(calc), [&](const int &x, const int &y) {
return z[x] > z[y];
});
for (int i = l; i <= r; i++) {
if (t[i] == 1) {
w[id[i]] = z[i];
changed[id[i]] = 0;
} else {
b[i - l].clear();
for (int x: upd) if (w[id[x]] >= z[i]) b[i - l].pb(x);
}
}
for (int j = 0, i = 0; i < calc.size(); i++) {
while (j < unchanged.size() && w[unchanged[j]] >= z[calc[i]]) {
join(u[unchanged[j]], v[unchanged[j]]);
j++;
}
int sz = st.size();
for (int x: b[calc[i] - l]) {
join(u[id[x]], v[id[x]]);
}
ans[calc[i]] = -lab[root(id[calc[i]])];
roll_back(sz);
}
roll_back(0);
}
for (int i = 1; i <= q; i++) if (ans[i] != -1) cout << ans[i] << '\n';
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
// freopen(task".inp", "r", stdin);
// freopen(task".out", "w", stdout);
int T = 1;
// cin >> T;
for (int i = 1; i <= T; i++) {
solve(i);
}
return 0;
}
/*
*/
Compilation message (stderr)
bridges.cpp: In function 'void roll_back(int)':
bridges.cpp:63:22: warning: comparison of integer expressions of different signedness: 'long long unsigned int' and 'int' [-Wsign-compare]
63 | while (st.size() > sz) {
| ~~~~~~~~~~^~~~
bridges.cpp: In function 'void solve(int)':
bridges.cpp:112:34: warning: comparison of integer expressions of different signedness: 'int' and 'long long unsigned int' [-Wsign-compare]
112 | for (int j = 0, i = 0; i < calc.size(); i++) {
| ^
bridges.cpp:113:22: warning: comparison of integer expressions of different signedness: 'int' and 'long long unsigned int' [-Wsign-compare]
113 | while (j < unchanged.size() && w[unchanged[j]] >= z[calc[i]]) {
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |