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>
using namespace std;
typedef tuple<int, int, int> iii;
const int MAXN = 50005;
int par[MAXN], sz[MAXN];
vector<pair<int, int>> jst;
int root(int x){
if (par[x] == -1) return x;
else return root(par[x]);
}
void join(int a, int b){
int ra = root(a), rb = root(b);
if (ra == rb) return;
if (sz[ra] < sz[rb]) swap(ra, rb); // ra larger
par[rb] = ra;
sz[ra] += sz[rb];
jst.push_back({ra, rb});
}
void rollback(){
while (!jst.empty()){
int ra, rb; tie(ra, rb) = jst.back(); jst.pop_back();
par[rb] = -1;
sz[ra] -= sz[rb];
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int nodes, edges; cin >> nodes >> edges;
vector<pair<int, int>> elist(edges + 1);
vector<int> ew(edges + 1);
for (int i = 1; i <= edges; i++){
int a, b, w; cin >> a >> b >> w;
elist[i] = {a, b};
ew[i] = w;
}
int queries; cin >> queries;
vector<pair<int, int>> qlist(queries);
vector<int> qw(queries);
for (int i = 0; i < queries; i++){
int typ, ind, w; cin >> typ >> ind >> w;
qlist[i] = {typ, ind};
qw[i] = w;
}
vector<int> ans(queries, -1);
int bsz = sqrt(queries);
for (int l = 0; l < queries; l += bsz){
int r = min(l + bsz, queries) - 1;
for (int i = 1; i <= nodes; i++){
par[i] = -1;
sz[i] = 1;
}
vector<bool> ch(edges + 1, 0);
vector<int> qv, stat, chv;
vector<vector<int>> dyna(r - l + 1);
for (int i = l; i <= r; i++){
int typ, x; tie(typ, x) = qlist[i];
if (typ == 1) {
ch[x] = 1;
chv.push_back(x);
}
}
for (int i = 1; i <= edges; i++)
if (!ch[i])
stat.push_back(i);
for (int i = l; i <= r; i++){
int typ, x, w = qw[i]; tie(typ, x) = qlist[i];
if (typ == 1) ew[x] = w;
else{
qv.push_back(i);
for (int eind : chv)
if (ew[eind] >= w)
dyna[i - l].push_back(eind);
}
}
sort(stat.begin(), stat.end(), [&](int a, int b){return ew[a] > ew[b];});
sort(qv.begin(), qv.end(), [&](int a, int b){return qw[a] > qw[b];});
int stind = -1;
for (int qind : qv){
int w = qw[qind], x, typ; tie(typ, x) = qlist[qind];
while (stind != stat.size() - 1 && ew[stat[stind + 1]] >= w){
stind++;
int a, b; tie(a, b) = elist[stat[stind]];
join(a, b);
}
jst.clear();
for (int eind : dyna[qind - l]){
int a, b; tie(a, b) = elist[eind];
join(a, b);
}
ans[qind] = sz[root(x)];
rollback();
}
}
for (int i = 0; i < queries; i++)
if (ans[i] != -1)
cout << ans[i] << '\n';
return 0;
}
Compilation message (stderr)
bridges.cpp: In function 'int main()':
bridges.cpp:81:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
81 | while (stind != stat.size() - 1 && ew[stat[stind + 1]] >= w){
| ~~~~~~^~~~~~~~~~~~~~~~~~
# | 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... |