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<iii> elist(edges + 1);
for (int i = 1; i <= edges; i++){
int a, b, w; cin >> a >> b >> w;
elist[i] = {w, a, b};
}
int queries; cin >> queries;
vector<iii> qlist(queries);
for (int i = 0; i < queries; i++){
int typ, ind, w; cin >> typ >> ind >> w;
qlist[i] = {w, typ, ind};
}
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, w; tie(w, 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; tie(w, typ, x) = qlist[i];
if (typ == 1) get<0>(elist[x]) = w;
else{
qv.push_back(i);
for (int eind : chv)
if (get<0>(elist[eind]) >= w)
dyna[i - l].push_back(eind);
}
}
sort(stat.begin(), stat.end(), [&](int a, int b){return elist[a] > elist[b];});
sort(qv.begin(), qv.end(), [&](int a, int b){return qlist[a] > qlist[b];});
int stind = -1;
for (int qind : qv){
int w, x, typ; tie(w, typ, x) = qlist[qind];
while (stind != stat.size() - 1 && get<0>(elist[stat[stind + 1]]) >= w){
stind++;
int a, b, lim; tie(lim, a, b) = elist[stat[stind]];
join(a, b);
}
jst.clear();
for (int eind : dyna[qind - l]){
int a, b, lim; tie(lim, 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:77:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
77 | while (stind != stat.size() - 1 && get<0>(elist[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... |