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 <bits/stdc++.h>
#define forr(_a,_b,_c) for(_a = (_b); _a <= (_c); ++_a)
#define ford(_a,_b,_c) for(_a = (_b) + 1; _a --> (_c);)
#define forf(_a,_b,_c) for(_a = (_b); _a < (_c); ++_a)
#define st first
#define nd second
#define ll long long
#define ull unsigned long long
#define pii pair <int,int>
#define pll pair <ll,ll>
#define piii pair <int,pii>
#define vi vector <int>
#define pb push_back
#define mp make_pair
#define all(x) begin(x),end(x)
#define file "test"
using namespace std;
const int N = 1e5 + 5;
const ll oo = 1e9;
const ll mod = 1e9 + 7;
int n, m, i, u, v, w, q, t[N], x[N], y[N], uwu = 600, par[N], sz[N], mk[N], ans[N], j;
piii edge[N];
vi uc, ask, upd, qq[N];
stack <int> s;
int get (int u){
while (par[u] != u) u = par[u];
return u;
}
void update (int u, int v){
u = get(u);
v = get(v);
//cout << u << " " << v << "\n";
if (u == v) return;
if (sz[u] < sz[v]) swap(u, v);
s.push(v);
par[v] = u;
sz[u] += sz[v];
}
void rollback (int pv){
while (s.size() > pv){
int u = s.top();
s.pop();
sz[par[u]] -= sz[u];
par[u] = u;
}
}
void rs (){
ask.clear();
upd.clear();
uc.clear();
memset(mk, 0, sizeof mk);
for (int h = 1; h <= n; h++)
par[h] = h, sz[h] = 1;
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef hqm
freopen(file".inp", "r", stdin); freopen(file".out", "w", stdout);
#endif
cin >> n >> m;
forr (i, 1, m){
cin >> u >> v >> w;
edge[i] = {w, {u, v}};
}
cin >> q;
forr (i, 1, q)
cin >> t[i] >> x[i] >> y[i];
for (int i = 1; i <= q; i += uwu){
int lmt = min (i + uwu - 1, q);
rs();
forr (j, i, lmt)
if (t[j] == 1)
upd.pb(j);
forr (j, i, lmt)
if (t[j] == 1)
mk[x[j]] = 1, edge[x[j]].st = y[j];
else {
ask.pb(j);
for (int v : upd)
if (edge[x[v]].st >= y[j]) qq[j].pb(v);
}
forr (j, 1, m){
if (mk[j]) continue;
uc.pb(j);
}
sort(all(ask), [&](int u, int v) {return y[u] > y[v]; });
sort(all(uc), [&](int u, int v) {return edge[u].st > edge[v].st; });
int it = 0;
for (int u : ask){
while (it < uc.size() && edge[uc[it]].st >= y[u])
update (edge[uc[it]].nd.st, edge[uc[it]].nd.nd), it++;
int prev = s.size();
for (int v : qq[u]){
update(edge[x[v]].nd.st, edge[x[v]].nd.nd);
}
ans[u] = sz[get(x[u])];
rollback(prev);
}
}
forr (i, 1, q)
if (t[i] - 1)
cout << ans[i] << "\n";
return 0;
}
/*
*/
Compilation message (stderr)
bridges.cpp: In function 'void rollback(int)':
bridges.cpp:46:18: warning: comparison of integer expressions of different signedness: 'std::stack<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
46 | while (s.size() > pv){
| ~~~~~~~~~^~~~
bridges.cpp: In function 'int main()':
bridges.cpp:107:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
107 | while (it < uc.size() && edge[uc[it]].st >= y[u])
| ~~~^~~~~~~~~~~
# | 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... |